ToolDialog.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. #include "ToolDialog.h"
  2. #include <QImage>
  3. #include <QFileInfo>
  4. #include <QFileDialog>
  5. #include <QElapsedTimer>
  6. #include <QGridLayout>
  7. #include <QCloseEvent>
  8. ToolDialogImpl::ToolDialogImpl(QWidget* parent)
  9. : DllToolDialog(parent)
  10. {
  11. ui.setupUi(this);
  12. this->setWindowFlags(Qt::Dialog | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint /*| Qt::WindowStaysOnTopHint*/);
  13. m_nIndex = 0;
  14. m_nAmplitudeThreshold = 40;
  15. m_fSigmat = 5.0;
  16. ui.comboModes->addItem("边沿模式");
  17. ui.comboModes->addItem("卡尺模式");
  18. ui.comboModes->addItem("预测模式");
  19. ui.spinBoxAmplitudeThreshold->setRange(0, 255);
  20. ui.spinBoxAmplitudeThreshold->setValue(m_nAmplitudeThreshold);
  21. ui.spinBoxSigmat->setRange(0.4, 32.0);
  22. ui.spinBoxSigmat->setValue(m_fSigmat);
  23. // ui.sliderSigmat->setMinimum(0.4);
  24. // ui.sliderSigmat->setMaximum(32.0);
  25. // ui.sliderSigmat->setValue(m_fSigmat);
  26. ui.comboSelect;
  27. ui.comboTransition;
  28. connect(this, SIGNAL(sigUpdateUI()), this, SLOT(on_UpdateUI()));
  29. // 显示控件初始化
  30. hwndUnit = new HWndUnit(this);
  31. Util::ShowUnitInWidget(ui.Image_widget, hwndUnit);
  32. hwndUnit->getHWndCtrl()->useROIController(&m_RoiController);
  33. connect(&m_RoiController, SIGNAL(ROIChange(const ViewMessage)), this, SLOT(on_ROIChange(const ViewMessage)));
  34. ROIRect2* roi = new ROIRect2();
  35. roi->setTitle("ROI");
  36. m_RoiController.setROISign(ROIOperation::Negative);
  37. m_RoiController.setROIShape(roi);
  38. m_RoiController.mouseDownAction(50, 50);
  39. QString strPath = QCoreApplication::applicationDirPath() + "/Test.jpg";
  40. try
  41. {
  42. m_Image.ReadImage(strPath.toStdString().c_str());
  43. hwndUnit->ShowImage(m_Image);
  44. hwndUnit->Refresh(false);
  45. }
  46. catch (HException& exception)
  47. {
  48. char m_szMsg[2048] = "";
  49. snprintf(m_szMsg, sizeof(m_szMsg), "Error #%u in %s: %s\n", exception.ErrorCode(),
  50. exception.ProcName().TextA(),
  51. exception.ErrorMessage().TextA());
  52. qWarning() << m_szMsg;
  53. }
  54. }
  55. ToolDialogImpl::~ToolDialogImpl()
  56. {
  57. }
  58. VPEnum::RETURN_VALUE ToolDialogImpl::Execute()
  59. {
  60. try
  61. {
  62. hwndUnit->ShowImage(m_Image);
  63. hwndUnit->ShowObj(m_hoROI, QColor(128, 222, 0));
  64. hwndUnit->Refresh();
  65. HTuple hv_MsrHandle_Measure;
  66. HTuple hv_Width, hv_Height;
  67. GetImageSize(m_Image,&hv_Width, &hv_Height);
  68. GenMeasureRectangle2(hv_roiDate[0], hv_roiDate[1], hv_roiDate[2], hv_roiDate[3], hv_roiDate[4],
  69. hv_Width, hv_Height, "bilinear", &hv_MsrHandle_Measure);
  70. HTuple hv_AmplitudeThreshold = ui.spinBoxAmplitudeThreshold->value();
  71. HTuple hv_Sigmat = ui.spinBoxSigmat->value();;
  72. HTuple hv_Phi = hv_roiDate[3];
  73. HTuple hv_Row_Measure, hv_Column_Measure, hv_Amplitude_Measure, hv_Score_Measure, hv_Width_Measure, hv_Distance_Measure;
  74. HTuple hv_Transition;
  75. switch (ui.comboTransition->currentIndex())
  76. {
  77. case 0: hv_Transition = "all"; break;
  78. case 1: hv_Transition = "negative"; break;
  79. case 2: hv_Transition = "positive"; break;
  80. default:
  81. break;
  82. }
  83. switch (ui.comboModes->currentIndex() )
  84. {
  85. case 0:
  86. {
  87. //negative
  88. FuzzyMeasurePos(m_Image, hv_MsrHandle_Measure, hv_Sigmat, hv_AmplitudeThreshold,
  89. 0.6, hv_Transition,
  90. &hv_Row_Measure, &hv_Column_Measure, &hv_Amplitude_Measure, &hv_Score_Measure, &hv_Distance_Measure);
  91. }
  92. break;
  93. case 1:
  94. {
  95. // 根据用户的选择,输出找到的边沿点和点点之间的距离
  96. MeasurePos(m_Image, hv_MsrHandle_Measure, hv_Sigmat, hv_AmplitudeThreshold, hv_Transition, "all",
  97. &hv_Row_Measure, &hv_Column_Measure, &hv_Amplitude_Measure,
  98. &hv_Distance_Measure);
  99. }
  100. break;
  101. default:
  102. break;
  103. }
  104. hwndUnit->ShowMsg(hv_Distance_Measure,QColor(0,255,0), hv_roiDate[0].D(), hv_roiDate[1].D());
  105. hwndUnit->Refresh(true);
  106. return VPEnum::RETURN_VALUE::Success;
  107. // 输出边缘对,可输出宽度,可设置正负边沿和第一个、最后一个(还可输出两个边沿对的中心距离)
  108. HTuple hv_Row1_Measure, hv_Column1_Measure, hv_Amplitude1_Measure;
  109. HTuple hv_Row2_Measure, hv_Column2_Measure, hv_Amplitude2_Measure;
  110. MeasurePairs(m_Image, hv_MsrHandle_Measure, hv_Sigmat, hv_AmplitudeThreshold, "all","all",
  111. &hv_Row1_Measure, &hv_Column1_Measure, &hv_Amplitude1_Measure,
  112. &hv_Row2_Measure, &hv_Column2_Measure, &hv_Amplitude2_Measure,
  113. &hv_Width_Measure, &hv_Distance_Measure);
  114. for (int i = 0; i < hv_Row1_Measure.TupleLength(); i++)
  115. {
  116. HObject ho_Cross1, ho_Cross2, ho_Contour;
  117. GenCrossContourXld(&ho_Cross1, hv_Row1_Measure[i], hv_Column1_Measure[i], 6, hv_Phi);
  118. GenCrossContourXld(&ho_Cross2, hv_Row2_Measure[i], hv_Column2_Measure[i], 6, hv_Phi);
  119. GenContourPolygonXld(&ho_Contour,
  120. HTuple(hv_Row1_Measure[i]).TupleConcat(HTuple(hv_Row2_Measure[i])),
  121. HTuple(hv_Column1_Measure[i]).TupleConcat(HTuple(hv_Column2_Measure[i])));
  122. hwndUnit->ShowObj(ho_Cross1, qRgb(255,0,0 ),3 );
  123. hwndUnit->ShowObj(ho_Cross2, qRgb(255, 0, 0), 3);
  124. hwndUnit->ShowObj(ho_Contour, qRgb(255, 0, 0), 3);
  125. }
  126. if(false)
  127. {
  128. // Local iconic variables
  129. HObject ho_Contour, ho_ContoursAffineTrans;
  130. HObject ho_Contour1, ho_ContoursAffineTrans1;
  131. HObject ho_Contour2, ho_ContoursAffineTrans2;
  132. // Local control variables
  133. HTuple hv_Row1, hv_Column1, hv_Phi1, hv_Length11, hv_Length21;
  134. HTuple hv_HorProjection, hv_VertProjection, hv_XValues;
  135. HTuple hv_Phi, hv_Length1, hv_Length2, hv_HomMat2D, hv_Threshold;
  136. HTuple hv_height;
  137. GrayProjections(m_hoROI, m_Image, "simple", &hv_HorProjection, &hv_VertProjection);
  138. hv_XValues = HTuple::TupleGenSequence(0, (hv_VertProjection.TupleLength()) - 1, 1);
  139. GenContourPolygonXld(&ho_Contour, (255 - hv_VertProjection) / 2, hv_XValues);
  140. SmallestRectangle2Xld(ho_Contour, &hv_Row1, &hv_Column1, &hv_Phi, &hv_Length1, &hv_Length2);
  141. // 外框
  142. hv_height = 255 / 2.0 + 10;
  143. // 曲线 // /*+ (hv_Length1 / 2)) + (hv_roiDate[4] / 2*/
  144. VectorAngleToRigid(180 + hv_roiDate[4], hv_Column1 , 0, hv_roiDate[0], hv_roiDate[1], hv_roiDate[2], &hv_HomMat2D);
  145. AffineTransContourXld(ho_Contour, &ho_ContoursAffineTrans, hv_HomMat2D);
  146. GenContourPolygonXld(&ho_Contour2, (((HTuple(-10).Append(-10)).TupleConcat(hv_height)).TupleConcat(hv_height)).TupleConcat(-10),
  147. ((HTuple(-10).TupleConcat(hv_VertProjection.TupleLength() + 10)).TupleConcat(hv_VertProjection.TupleLength() + 10)).TupleConcat((HTuple(-10).Append(-10))));
  148. AffineTransContourXld(ho_Contour2, &ho_ContoursAffineTrans2, hv_HomMat2D);
  149. HObject ho_ContoursAffineTransRegion;
  150. GenRegionContourXld(ho_ContoursAffineTrans2, &ho_ContoursAffineTransRegion, "filled");
  151. DilationRectangle1(ho_ContoursAffineTransRegion, &ho_ContoursAffineTransRegion, 50, 50);
  152. // 标记线
  153. hv_Threshold = (255 - hv_Amplitude1_Measure[0]) / 2;
  154. GenContourPolygonXld(&ho_Contour1, hv_Threshold.TupleConcat(hv_Threshold), HTuple(0).TupleConcat(hv_VertProjection.TupleLength()));
  155. AffineTransContourXld(ho_Contour1, &ho_ContoursAffineTrans1, hv_HomMat2D);
  156. hwndUnit->ShowObj(ho_ContoursAffineTransRegion,QColor( 0, 0, 0, 200 ));
  157. hwndUnit->ShowObj(ho_ContoursAffineTrans, qRgb(255, 120, 147));
  158. hwndUnit->ShowObj(ho_ContoursAffineTrans1, qRgb(255, 20, 147) );
  159. hwndUnit->ShowObj(ho_ContoursAffineTrans2, qRgb(65, 105, 225));
  160. }
  161. hwndUnit->ShowMsg(hv_Distance_Measure);
  162. //qWarning() << "Value: " << hv_Distance_Measure[0].D();
  163. hwndUnit->Refresh(true);
  164. }
  165. catch (HException& exception)
  166. {
  167. char m_szMsg[2048] = "";
  168. snprintf(m_szMsg, sizeof(m_szMsg),"Error #%u in %s: %s\n", exception.ErrorCode(),
  169. exception.ProcName().TextA(),
  170. exception.ErrorMessage().TextA());
  171. qWarning() << "Execute() " << m_szMsg;
  172. }
  173. //emit sigUpdateUI();
  174. //m_TestClass.SetValue(m_Value3);
  175. return VPEnum::RETURN_VALUE::Success;
  176. }
  177. void ToolDialogImpl::on_ROIChange(const ViewMessage& sign)
  178. {
  179. switch (sign)
  180. {
  181. case ViewMessage::MovingROI:
  182. {
  183. }break;
  184. case ViewMessage::UpdateROI:
  185. {
  186. ROI* roi = m_RoiController.getActiveROI();
  187. if (roi == nullptr)
  188. {
  189. return;
  190. }
  191. hv_roiDate = roi->getROIData();
  192. try
  193. {
  194. GenRectangle2(&m_hoROI, hv_roiDate[0], hv_roiDate[1], hv_roiDate[2], hv_roiDate[3], hv_roiDate[4]);
  195. Execute();
  196. }
  197. catch (...)
  198. {
  199. }
  200. }
  201. break;
  202. case ViewMessage::CreatedROI:
  203. {
  204. ROI* roi = m_RoiController.getActiveROI();
  205. if (roi == nullptr)
  206. {
  207. return;
  208. }
  209. hv_roiDate = roi->getROIData();
  210. try
  211. {
  212. GenRectangle2(&m_hoROI, hv_roiDate[0], hv_roiDate[1], hv_roiDate[2], hv_roiDate[3], hv_roiDate[4]);
  213. }
  214. catch (...)
  215. {
  216. }
  217. }
  218. break;
  219. default:
  220. break;
  221. }
  222. }
  223. void ToolDialogImpl::Running(bool bRun)
  224. {
  225. }
  226. void ToolDialogImpl::timerEvent(QTimerEvent* event)
  227. {
  228. }
  229. bool ToolDialogImpl::Serialized(QDataStream& ar, bool bIsOut)
  230. {
  231. int paranum;//参数数量
  232. if (bIsOut)//保存参数流程
  233. {
  234. paranum = 5;
  235. ar << paranum;//先保存参数数量
  236. ar << (int)1 << hv_roiDate;
  237. ar << (int)2 << m_RoiController;
  238. ar << (int)3 << m_hoROI;
  239. ar << (int)4 << ui.spinBoxAmplitudeThreshold->value();
  240. ar << (int)5 << ui.spinBoxSigmat->value();
  241. }
  242. else//加载参数流程,参数加载顺序一定要跟保存顺序一致
  243. {
  244. int para;
  245. ar >> paranum;//读取参数数量
  246. for (int i = 0; i < paranum; i++)
  247. {
  248. ar >> para;
  249. switch (para)
  250. {
  251. case 1: ar >> hv_roiDate; break;
  252. case 2: ar >> m_RoiController; break;
  253. case 3: ar >> m_hoROI; break;
  254. case 4: ar >> m_nAmplitudeThreshold; break;
  255. case 5: ar >> m_fSigmat; break;
  256. default:
  257. {
  258. qWarning() << "Serialized(In) Error";
  259. return false;
  260. }
  261. break;
  262. }
  263. }
  264. ui.spinBoxAmplitudeThreshold->setValue(m_nAmplitudeThreshold);
  265. ui.spinBoxSigmat->setValue(m_fSigmat);
  266. }
  267. return true;
  268. }
  269. void ToolDialogImpl::on_UpdateUI()
  270. {
  271. }
  272. /// <summary>
  273. /// 确定
  274. /// </summary>
  275. void ToolDialogImpl::on_btnOK_clicked()
  276. {
  277. this->hide();
  278. }
  279. /// <summary>
  280. /// 取消按钮
  281. /// </summary>
  282. void ToolDialogImpl::on_btnCancel_clicked()
  283. {
  284. // 将本工具的恢复到打开工具之前的状态
  285. RecoverData();
  286. }
  287. /// <summary>
  288. /// 测试按钮
  289. /// </summary>
  290. void ToolDialogImpl::on_btnExecute_clicked()
  291. {
  292. QElapsedTimer toolTimer;
  293. toolTimer.start();
  294. // 发送事件
  295. ToolEvent* pToolEvent = new ToolEvent(m_strPouName, m_strInstanceName, TOOL_EVENT_TYPE::TOOL_TRIGGER);
  296. QCoreApplication::sendEvent(m_pEventTarget, pToolEvent);
  297. // 统计返回值
  298. VPEnum::RETURN_VALUE ret = pToolEvent->ret;
  299. double nExecTime = toolTimer.elapsed();
  300. QString str;
  301. str = QString("耗时: %1 ms").arg(nExecTime, 0, 'G', 5);
  302. ui.label_time->setText(str);
  303. str = QString("状态: %1 ").arg(QMetaEnum::fromType<VPEnum::RETURN_VALUE>().key((short)ret));
  304. ui.label_state->setText(str);
  305. delete pToolEvent;
  306. }