blobselectshape.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #pragma execution_character_set("utf-8")
  2. #include "blobselectshape.h"
  3. #include "ui_blobselectshape.h"
  4. #include "../ToolDialog.h"
  5. class ToolDialogImpl;
  6. BlobSelectShape::BlobSelectShape(QWidget *parent , ToolDialogImpl* p)
  7. : ToolInterface(parent), ui(new Ui::BlobSelectShape)
  8. {
  9. ui->setupUi(this);
  10. m_pToolDialogImpl = p;
  11. ui->tableWidget->verticalHeader()->setVisible(false);
  12. ui->tableWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  13. ui->tableWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  14. ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
  15. ui->tableWidget->setFocusPolicy(Qt::NoFocus);
  16. ui->tableWidget->setShowGrid(false);
  17. ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
  18. QMenu *menu = new QMenu();
  19. QMenu* shuxing = menu->addMenu("属性");
  20. shuxing->addAction("面积", this, SLOT(ActionSlot()));
  21. shuxing->addAction("X", this, SLOT(ActionSlot()));
  22. shuxing->addAction("Y", this, SLOT(ActionSlot()));
  23. shuxing->addAction("宽度", this, SLOT(ActionSlot()));
  24. shuxing->addAction("高度", this, SLOT(ActionSlot()));
  25. shuxing->addAction("角度", this, SLOT(ActionSlot()));
  26. QMenu *xingtai = menu->addMenu("形态");
  27. xingtai->addAction("圆度", this, SLOT(ActionSlot()));
  28. xingtai->addAction("紧密度", this, SLOT(ActionSlot()));
  29. xingtai->addAction("凸度", this, SLOT(ActionSlot()));
  30. xingtai->addAction("矩形度", this, SLOT(ActionSlot()));
  31. xingtai->addAction("最大内接", this, SLOT(ActionSlot()));
  32. ui->BTN_Add->setMenu(menu);
  33. ui->comboBox->setView(new QListView());
  34. ui->comboBox_index->setView(new QListView());
  35. if(m_pToolDialogImpl->m_LinkProcess.size() > 0)
  36. {
  37. ui->comboBox_index->setVisible(true);
  38. ui->label_index->setVisible(true);
  39. }
  40. else
  41. {
  42. ui->comboBox_index->setVisible(false);
  43. ui->label_index->setVisible(false);
  44. }
  45. for(int i = 0; i < m_pToolDialogImpl->m_LinkProcess.size(); i++)
  46. {
  47. ui->comboBox_index->addItem(QString::number(i));
  48. }
  49. m_NamesMap.insert("面积", "area");
  50. m_NamesMap.insert("X", "column");
  51. m_NamesMap.insert("Y", "row");
  52. m_NamesMap.insert("宽度", "width");
  53. m_NamesMap.insert("高度", "height");
  54. m_NamesMap.insert("角度", "rect2_phi");
  55. m_NamesMap.insert("紧密度", "compactness");
  56. m_NamesMap.insert("凸度", "convexity");
  57. m_NamesMap.insert("矩形度", "rectangularity");
  58. m_NamesMap.insert("最大内接", "max_diameter");
  59. }
  60. void BlobSelectShape::ActionSlot()
  61. {
  62. QAction* action = (QAction*)sender();
  63. QString strName = action->text();
  64. AddTool(strName, 0 , 999999 );
  65. }
  66. BlobSelectShape::~BlobSelectShape()
  67. {
  68. delete ui;
  69. }
  70. bool BlobSelectShape::Serialized(QDataStream& ar, bool bIsOut)
  71. {
  72. int paranum;//参数数量
  73. if (bIsOut)//保存参数流程
  74. {
  75. paranum = 3;//以下参数的数量
  76. ar << paranum;//先保存参数数量
  77. ar << (UINT)m_AllScreening.size();
  78. for (UINT i = 0; i < m_AllScreening.size(); i++)
  79. {
  80. Screening nScreening = m_AllScreening[i];
  81. ar << (int)1 << nScreening.strName;
  82. ar << (int)2 << nScreening.nMin;
  83. ar << (int)3 << nScreening.nMax;
  84. }
  85. }
  86. else//加载参数流程,参数加载顺序一定要跟保存顺序一致
  87. {
  88. m_AllScreening.clear();
  89. ui->tableWidget->clear();
  90. UINT nToolSize = 0;
  91. ar >> paranum;//读取参数数量
  92. ar >> nToolSize;
  93. for (UINT i = 0; i < nToolSize; i++)
  94. {
  95. int nToolID = 0;
  96. QString strName;
  97. int nMin;
  98. int nMax;
  99. int nPpara;
  100. for (int j = 0; j < paranum; j++)
  101. {
  102. ar >> nPpara;
  103. switch (nPpara)
  104. {
  105. case 1: ar >> strName; break;
  106. case 2: ar >> nMin; break;
  107. case 3: ar >> nMax; break;
  108. default:
  109. return false;
  110. }
  111. }
  112. AddTool(strName, nMin, nMax);
  113. }
  114. emit updateParameter(ShowParameter());
  115. }
  116. return true;
  117. }
  118. QString BlobSelectShape::ShowParameter()
  119. {
  120. m_Key.Clear();
  121. m_MinValue.Clear();
  122. m_MaxValue.Clear();
  123. for (int i = 0; i < m_AllScreening.size(); i++)
  124. {
  125. Screening nScreening = m_AllScreening[i];
  126. QString strName = m_NamesMap.value(nScreening.strName);
  127. int nMin = nScreening.nMin;
  128. int nMax = nScreening.nMax;
  129. m_Key[i] = HTuple(strName.toStdString().c_str());
  130. m_MinValue[i] = HTuple(nMin);
  131. m_MaxValue[i] = HTuple(nMax);
  132. // if (i != 0)
  133. // {
  134. // m_Key.Append();
  135. //
  136. // m_MinValue.Append(HTuple(nMin));
  137. // m_MaxValue.Append(HTuple(nMax));
  138. // }
  139. // else
  140. // {
  141. // m_Key = HTuple(strName.toStdString().c_str());
  142. // m_MinValue = HTuple(nMin);
  143. // m_MaxValue = HTuple(nMax);
  144. // }
  145. }
  146. // SelectShape(ho_ConnectedRegions, &ho_SelectedRegions, (HTuple("width").Append("height")), "and", (HTuple(1000).Append(200)), (HTuple(2000).Append(1000)));
  147. return ui->comboBox->currentText();
  148. }
  149. int BlobSelectShape::Execute()
  150. {
  151. int result = 1;
  152. try
  153. {
  154. if (m_AllScreening.size() == 0)
  155. {
  156. m_OutImage = m_InImage;
  157. }
  158. else
  159. {
  160. if (ui->comboBox_index->currentIndex() == 0)
  161. {
  162. SelectShape(m_InImage, &m_OutImage, m_Key, "and", m_MinValue, m_MaxValue);
  163. }
  164. else
  165. {
  166. SelectShape(m_InImage, &m_OutImage, m_Key, "or", m_MinValue, m_MaxValue);
  167. }
  168. }
  169. }
  170. catch(...)
  171. {
  172. vWarning() << "Error";
  173. }
  174. return result;
  175. }
  176. void BlobSelectShape::AddTool(QString strName, int nMin, int nMax)
  177. {
  178. int nIndex = m_AllScreening.size();
  179. Screening nScreening;
  180. nScreening.nIndex = nIndex;
  181. nScreening.strName = strName;
  182. nScreening.nMin = nMin;
  183. nScreening.nMax = nMax;
  184. m_AllScreening.push_back(nScreening);
  185. ui->tableWidget->setRowCount(nIndex + 1);
  186. ui->tableWidget->setItem(nIndex, 0, new QTableWidgetItem(strName));
  187. ui->tableWidget->item(nIndex, 0)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  188. ui->tableWidget->setItem(nIndex, 1, new QTableWidgetItem(QString::number(nScreening.nMin)));
  189. ui->tableWidget->item(nIndex, 1)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  190. ui->tableWidget->setItem(nIndex, 2, new QTableWidgetItem(QString::number(nScreening.nMax)));
  191. ui->tableWidget->item(nIndex, 2)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  192. }
  193. void BlobSelectShape::on_comboBox_currentIndexChanged(int index)
  194. {
  195. emit updateParameter(ShowParameter());
  196. }
  197. void BlobSelectShape::on_tableWidget_cellChanged(int row, int column)
  198. {
  199. int rowIndex = ui->tableWidget->currentRow();
  200. if (rowIndex != -1)
  201. { // 更新数据
  202. QTableWidgetItem* item1 = ui->tableWidget->item(row, 1);
  203. if (item1 != nullptr)
  204. {
  205. int nMinValue = item1->text().toInt();
  206. m_AllScreening[row].nMin = nMinValue;
  207. emit updateParameter(ShowParameter());
  208. }
  209. QTableWidgetItem* item2 = ui->tableWidget->item(row, 2);
  210. if (item2 != nullptr)
  211. {
  212. int nMaxValue = item2->text().toInt();
  213. m_AllScreening[row].nMax = nMaxValue;
  214. emit updateParameter(ShowParameter());
  215. }
  216. }
  217. }
  218. void BlobSelectShape::on_BTN_Del_clicked()
  219. {
  220. int rowIndex = ui->tableWidget->currentRow();
  221. if (rowIndex != -1)
  222. {
  223. ui->tableWidget->removeRow(rowIndex);
  224. m_AllScreening.remove(rowIndex);
  225. }
  226. }
  227. void BlobSelectShape::on_comboBox_index_currentIndexChanged(int index)
  228. {
  229. if(index > 0)
  230. {
  231. emit updateParameterIndex(QString::number(index - 1));
  232. }
  233. emit updateParameter(ShowParameter());
  234. }