DynthresholdTool.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #include "DynthresholdTool.h"
  2. #include "ui_DynthresholdTool.h"
  3. dynthreshold::dynthreshold(QWidget *parent) : ToolInterface(parent), ui(new Ui::dynthreshold)
  4. {
  5. ui->setupUi(this);
  6. ui->lineEdit_width->setText("50");
  7. ui->lineEdit_height->setText("50");
  8. ui->lineEdit_offset->setText("50");
  9. ui->lineEdit_width->setValidator(new QIntValidator(1, 16777215, this));
  10. ui->lineEdit_height->setValidator(new QIntValidator(1, 16777215, this));
  11. ui->lineEdit_offset->setValidator(new QIntValidator(1, 16777215, this));
  12. ui->comboBox->setView(new QListView());
  13. }
  14. dynthreshold::~dynthreshold()
  15. {
  16. delete ui;
  17. }
  18. bool dynthreshold::Serialized(QDataStream& ar, bool bIsOut)
  19. {
  20. int paranum;//参数数量
  21. if (bIsOut)//保存参数流程
  22. {
  23. paranum = 5;
  24. ar << paranum;//先保存参数数量
  25. ar << (int)1 << m_nWidth;
  26. ar << (int)2 << m_nHeight;
  27. ar << (int)3 << m_nOffset;
  28. ar << (int)4 << m_nType;
  29. ar << (int)5 << ui->comboBox->currentIndex();
  30. }
  31. else//加载参数流程,参数加载顺序一定要跟保存顺序一致
  32. {
  33. int nCurrentIndex = 0;
  34. int para;
  35. ar >> paranum;//读取参数数量
  36. for (int i = 0; i < paranum; i++)
  37. {
  38. ar >> para;
  39. switch (para)
  40. {
  41. case 1: ar >> m_nWidth; break;
  42. case 2: ar >> m_nHeight; break;
  43. case 3: ar >> m_nOffset; break;
  44. case 4: ar >> m_nType; break;
  45. case 5: ar >> nCurrentIndex; break;
  46. default:
  47. {
  48. qWarning() << "Serialized(In) Error";
  49. return false;
  50. }
  51. break;
  52. }
  53. }
  54. {
  55. ui->comboBox->setCurrentIndex(nCurrentIndex);
  56. ui->lineEdit_width->setText(QString::number(m_nWidth) );
  57. ui->lineEdit_height->setText(QString::number(m_nHeight));
  58. ui->lineEdit_offset->setText(QString::number(m_nOffset));
  59. emit updateParameter(ShowParameter());
  60. }
  61. }
  62. return true;
  63. }
  64. QString dynthreshold::ShowParameter()
  65. {
  66. QString str;
  67. m_nWidth = ui->lineEdit_width->text().toInt();
  68. m_nHeight = ui->lineEdit_height->text().toInt();
  69. m_nOffset = ui->lineEdit_offset->text().toInt();
  70. str += QString::number(m_nWidth);
  71. str += ",";
  72. str += QString::number(m_nHeight);
  73. str += ",";
  74. str += QString::number(m_nOffset);
  75. str += ",";
  76. str += ui->comboBox->currentText();
  77. return str;
  78. }
  79. int dynthreshold::Execute()
  80. {
  81. int result = 1;
  82. m_nWidth = ui->lineEdit_width->text().toInt();
  83. m_nHeight = ui->lineEdit_height->text().toInt();
  84. m_nOffset = ui->lineEdit_offset->text().toInt();
  85. try
  86. {
  87. HTuple Channels;
  88. CountChannels(m_InImage, &Channels);
  89. if(Channels == 1)
  90. {
  91. HTuple hv_Width, hv_Height;
  92. HObject ho_Regions, ho_Mean;
  93. GetImageSize(m_InImage, &hv_Width, &hv_Height);
  94. MeanImage(m_InImage, &ho_Mean, m_nWidth, m_nHeight);
  95. if(m_nType == 0)
  96. {
  97. DynThreshold(m_InImage, ho_Mean, &ho_Regions, m_nOffset, "light");
  98. }
  99. if(m_nType == 1)
  100. {
  101. DynThreshold(m_InImage, ho_Mean, &ho_Regions, m_nOffset, "dark");
  102. }
  103. if(m_nType == 2)
  104. {
  105. DynThreshold(m_InImage, ho_Mean, &ho_Regions, m_nOffset, "equal");
  106. }
  107. if(m_nType == 3)
  108. {
  109. DynThreshold(m_InImage, ho_Mean, &ho_Regions, m_nOffset, "not_equal");
  110. }
  111. RegionToBin(ho_Regions, &m_OutImage, 255, 0, hv_Width, hv_Height);
  112. }
  113. }
  114. catch(...)
  115. {
  116. }
  117. return result;
  118. }
  119. void dynthreshold::on_comboBox_currentIndexChanged(int index)
  120. {
  121. m_nType = index;
  122. emit updateParameter(ShowParameter());
  123. }
  124. void dynthreshold::on_BTN_Add1_clicked()
  125. {
  126. ui->lineEdit_width->setText(QString::number(ui->lineEdit_width->text().toInt() + 1));
  127. emit updateParameter(ShowParameter());
  128. }
  129. void dynthreshold::on_BTN_Sub1_clicked()
  130. {
  131. if(ui->lineEdit_width->text().toInt() >= 2)
  132. {
  133. ui->lineEdit_width->setText(QString::number(ui->lineEdit_width->text().toInt() - 1));
  134. emit updateParameter(ShowParameter());
  135. }
  136. }
  137. void dynthreshold::on_BTN_Add2_clicked()
  138. {
  139. ui->lineEdit_height->setText(QString::number(ui->lineEdit_height->text().toInt() + 1));
  140. emit updateParameter(ShowParameter());
  141. }
  142. void dynthreshold::on_BTN_Sub2_clicked()
  143. {
  144. if(ui->lineEdit_height->text().toInt() >= 2)
  145. {
  146. ui->lineEdit_height->setText(QString::number(ui->lineEdit_height->text().toInt() - 1));
  147. emit updateParameter(ShowParameter());
  148. }
  149. }
  150. void dynthreshold::on_BTN_Add3_clicked()
  151. {
  152. ui->lineEdit_offset->setText(QString::number(ui->lineEdit_offset->text().toInt() + 1));
  153. emit updateParameter(ShowParameter());
  154. }
  155. void dynthreshold::on_BTN_Sub3_clicked()
  156. {
  157. if(ui->lineEdit_offset->text().toInt() >= 2)
  158. {
  159. ui->lineEdit_offset->setText(QString::number(ui->lineEdit_offset->text().toInt() - 1));
  160. emit updateParameter(ShowParameter());
  161. }
  162. }
  163. void dynthreshold::on_lineEdit_width_editingFinished()
  164. {
  165. emit updateParameter(ShowParameter());
  166. }
  167. void dynthreshold::on_lineEdit_height_editingFinished()
  168. {
  169. emit updateParameter(ShowParameter());
  170. }
  171. void dynthreshold::on_lineEdit_offset_editingFinished()
  172. {
  173. emit updateParameter(ShowParameter());
  174. }