123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- #include "ToolDialog.h"
- #include <QImage>
- #include <QFileInfo>
- #include <QFileDialog>
- #include <QElapsedTimer>
- #include <QGridLayout>
- #include <QCloseEvent>
- ToolDialogImpl::ToolDialogImpl(QWidget* parent)
- : DllToolDialog(parent)
- {
- ui.setupUi(this);
- this->setWindowFlags(Qt::Dialog | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint /*| Qt::WindowStaysOnTopHint*/);
- m_nIndex = 0;
- m_nAmplitudeThreshold = 40;
- m_fSigmat = 5.0;
- ui.comboModes->addItem("边沿模式");
- ui.comboModes->addItem("卡尺模式");
- ui.comboModes->addItem("预测模式");
- ui.spinBoxAmplitudeThreshold->setRange(0, 255);
- ui.spinBoxAmplitudeThreshold->setValue(m_nAmplitudeThreshold);
-
- ui.spinBoxSigmat->setRange(0.4, 32.0);
- ui.spinBoxSigmat->setValue(m_fSigmat);
- // ui.sliderSigmat->setMinimum(0.4);
- // ui.sliderSigmat->setMaximum(32.0);
- // ui.sliderSigmat->setValue(m_fSigmat);
- ui.comboSelect;
- ui.comboTransition;
- connect(this, SIGNAL(sigUpdateUI()), this, SLOT(on_UpdateUI()));
- // 显示控件初始化
- hwndUnit = new HWndUnit(this);
- Util::ShowUnitInWidget(ui.Image_widget, hwndUnit);
- hwndUnit->getHWndCtrl()->useROIController(&m_RoiController);
- connect(&m_RoiController, SIGNAL(ROIChange(const ViewMessage)), this, SLOT(on_ROIChange(const ViewMessage)));
- ROIRect2* roi = new ROIRect2();
- roi->setTitle("ROI");
- m_RoiController.setROISign(ROIOperation::Negative);
- m_RoiController.setROIShape(roi);
- m_RoiController.mouseDownAction(50, 50);
- QString strPath = QCoreApplication::applicationDirPath() + "/Test.jpg";
- try
- {
- m_Image.ReadImage(strPath.toStdString().c_str());
- hwndUnit->ShowImage(m_Image);
- hwndUnit->Refresh(false);
- }
- catch (HException& exception)
- {
- char m_szMsg[2048] = "";
- snprintf(m_szMsg, sizeof(m_szMsg), "Error #%u in %s: %s\n", exception.ErrorCode(),
- exception.ProcName().TextA(),
- exception.ErrorMessage().TextA());
- qWarning() << m_szMsg;
- }
-
- }
- ToolDialogImpl::~ToolDialogImpl()
- {
- }
- VPEnum::RETURN_VALUE ToolDialogImpl::Execute()
- {
- try
- {
- hwndUnit->ShowImage(m_Image);
- hwndUnit->ShowObj(m_hoROI, QColor(128, 222, 0));
- hwndUnit->Refresh();
- HTuple hv_MsrHandle_Measure;
- HTuple hv_Width, hv_Height;
- GetImageSize(m_Image,&hv_Width, &hv_Height);
- GenMeasureRectangle2(hv_roiDate[0], hv_roiDate[1], hv_roiDate[2], hv_roiDate[3], hv_roiDate[4],
- hv_Width, hv_Height, "bilinear", &hv_MsrHandle_Measure);
-
- HTuple hv_AmplitudeThreshold = ui.spinBoxAmplitudeThreshold->value();
- HTuple hv_Sigmat = ui.spinBoxSigmat->value();;
- HTuple hv_Phi = hv_roiDate[3];
- HTuple hv_Row_Measure, hv_Column_Measure, hv_Amplitude_Measure, hv_Score_Measure, hv_Width_Measure, hv_Distance_Measure;
-
- HTuple hv_Transition;
- switch (ui.comboTransition->currentIndex())
- {
- case 0: hv_Transition = "all"; break;
- case 1: hv_Transition = "negative"; break;
- case 2: hv_Transition = "positive"; break;
- default:
- break;
- }
- switch (ui.comboModes->currentIndex() )
- {
- case 0:
- {
- //negative
- FuzzyMeasurePos(m_Image, hv_MsrHandle_Measure, hv_Sigmat, hv_AmplitudeThreshold,
- 0.6, hv_Transition,
- &hv_Row_Measure, &hv_Column_Measure, &hv_Amplitude_Measure, &hv_Score_Measure, &hv_Distance_Measure);
- }
- break;
- case 1:
- {
- // 根据用户的选择,输出找到的边沿点和点点之间的距离
- MeasurePos(m_Image, hv_MsrHandle_Measure, hv_Sigmat, hv_AmplitudeThreshold, hv_Transition, "all",
- &hv_Row_Measure, &hv_Column_Measure, &hv_Amplitude_Measure,
- &hv_Distance_Measure);
- }
- break;
- default:
- break;
- }
- hwndUnit->ShowMsg(hv_Distance_Measure,QColor(0,255,0), hv_roiDate[0].D(), hv_roiDate[1].D());
-
- hwndUnit->Refresh(true);
- return VPEnum::RETURN_VALUE::Success;
- // 输出边缘对,可输出宽度,可设置正负边沿和第一个、最后一个(还可输出两个边沿对的中心距离)
- HTuple hv_Row1_Measure, hv_Column1_Measure, hv_Amplitude1_Measure;
- HTuple hv_Row2_Measure, hv_Column2_Measure, hv_Amplitude2_Measure;
- MeasurePairs(m_Image, hv_MsrHandle_Measure, hv_Sigmat, hv_AmplitudeThreshold, "all","all",
- &hv_Row1_Measure, &hv_Column1_Measure, &hv_Amplitude1_Measure,
- &hv_Row2_Measure, &hv_Column2_Measure, &hv_Amplitude2_Measure,
- &hv_Width_Measure, &hv_Distance_Measure);
- for (int i = 0; i < hv_Row1_Measure.TupleLength(); i++)
- {
- HObject ho_Cross1, ho_Cross2, ho_Contour;
- GenCrossContourXld(&ho_Cross1, hv_Row1_Measure[i], hv_Column1_Measure[i], 6, hv_Phi);
- GenCrossContourXld(&ho_Cross2, hv_Row2_Measure[i], hv_Column2_Measure[i], 6, hv_Phi);
- GenContourPolygonXld(&ho_Contour,
- HTuple(hv_Row1_Measure[i]).TupleConcat(HTuple(hv_Row2_Measure[i])),
- HTuple(hv_Column1_Measure[i]).TupleConcat(HTuple(hv_Column2_Measure[i])));
- hwndUnit->ShowObj(ho_Cross1, qRgb(255,0,0 ),3 );
- hwndUnit->ShowObj(ho_Cross2, qRgb(255, 0, 0), 3);
- hwndUnit->ShowObj(ho_Contour, qRgb(255, 0, 0), 3);
- }
-
- if(false)
- {
- // Local iconic variables
- HObject ho_Contour, ho_ContoursAffineTrans;
- HObject ho_Contour1, ho_ContoursAffineTrans1;
- HObject ho_Contour2, ho_ContoursAffineTrans2;
- // Local control variables
- HTuple hv_Row1, hv_Column1, hv_Phi1, hv_Length11, hv_Length21;
- HTuple hv_HorProjection, hv_VertProjection, hv_XValues;
- HTuple hv_Phi, hv_Length1, hv_Length2, hv_HomMat2D, hv_Threshold;
- HTuple hv_height;
- GrayProjections(m_hoROI, m_Image, "simple", &hv_HorProjection, &hv_VertProjection);
- hv_XValues = HTuple::TupleGenSequence(0, (hv_VertProjection.TupleLength()) - 1, 1);
- GenContourPolygonXld(&ho_Contour, (255 - hv_VertProjection) / 2, hv_XValues);
- SmallestRectangle2Xld(ho_Contour, &hv_Row1, &hv_Column1, &hv_Phi, &hv_Length1, &hv_Length2);
- // 外框
- hv_height = 255 / 2.0 + 10;
- // 曲线 // /*+ (hv_Length1 / 2)) + (hv_roiDate[4] / 2*/
- VectorAngleToRigid(180 + hv_roiDate[4], hv_Column1 , 0, hv_roiDate[0], hv_roiDate[1], hv_roiDate[2], &hv_HomMat2D);
- AffineTransContourXld(ho_Contour, &ho_ContoursAffineTrans, hv_HomMat2D);
- GenContourPolygonXld(&ho_Contour2, (((HTuple(-10).Append(-10)).TupleConcat(hv_height)).TupleConcat(hv_height)).TupleConcat(-10),
- ((HTuple(-10).TupleConcat(hv_VertProjection.TupleLength() + 10)).TupleConcat(hv_VertProjection.TupleLength() + 10)).TupleConcat((HTuple(-10).Append(-10))));
- AffineTransContourXld(ho_Contour2, &ho_ContoursAffineTrans2, hv_HomMat2D);
- HObject ho_ContoursAffineTransRegion;
- GenRegionContourXld(ho_ContoursAffineTrans2, &ho_ContoursAffineTransRegion, "filled");
- DilationRectangle1(ho_ContoursAffineTransRegion, &ho_ContoursAffineTransRegion, 50, 50);
- // 标记线
- hv_Threshold = (255 - hv_Amplitude1_Measure[0]) / 2;
- GenContourPolygonXld(&ho_Contour1, hv_Threshold.TupleConcat(hv_Threshold), HTuple(0).TupleConcat(hv_VertProjection.TupleLength()));
- AffineTransContourXld(ho_Contour1, &ho_ContoursAffineTrans1, hv_HomMat2D);
- hwndUnit->ShowObj(ho_ContoursAffineTransRegion,QColor( 0, 0, 0, 200 ));
- hwndUnit->ShowObj(ho_ContoursAffineTrans, qRgb(255, 120, 147));
- hwndUnit->ShowObj(ho_ContoursAffineTrans1, qRgb(255, 20, 147) );
- hwndUnit->ShowObj(ho_ContoursAffineTrans2, qRgb(65, 105, 225));
-
- }
- hwndUnit->ShowMsg(hv_Distance_Measure);
- //qWarning() << "Value: " << hv_Distance_Measure[0].D();
- hwndUnit->Refresh(true);
- }
- catch (HException& exception)
- {
- char m_szMsg[2048] = "";
- snprintf(m_szMsg, sizeof(m_szMsg),"Error #%u in %s: %s\n", exception.ErrorCode(),
- exception.ProcName().TextA(),
- exception.ErrorMessage().TextA());
- qWarning() << "Execute() " << m_szMsg;
- }
- //emit sigUpdateUI();
- //m_TestClass.SetValue(m_Value3);
- return VPEnum::RETURN_VALUE::Success;
- }
- void ToolDialogImpl::on_ROIChange(const ViewMessage& sign)
- {
- switch (sign)
- {
- case ViewMessage::MovingROI:
- {
- }break;
- case ViewMessage::UpdateROI:
- {
- ROI* roi = m_RoiController.getActiveROI();
- if (roi == nullptr)
- {
- return;
- }
- hv_roiDate = roi->getROIData();
- try
- {
- GenRectangle2(&m_hoROI, hv_roiDate[0], hv_roiDate[1], hv_roiDate[2], hv_roiDate[3], hv_roiDate[4]);
- Execute();
- }
- catch (...)
- {
- }
- }
- break;
- case ViewMessage::CreatedROI:
- {
- ROI* roi = m_RoiController.getActiveROI();
- if (roi == nullptr)
- {
- return;
- }
- hv_roiDate = roi->getROIData();
- try
- {
- GenRectangle2(&m_hoROI, hv_roiDate[0], hv_roiDate[1], hv_roiDate[2], hv_roiDate[3], hv_roiDate[4]);
- }
- catch (...)
- {
- }
- }
- break;
- default:
- break;
- }
- }
- void ToolDialogImpl::Running(bool bRun)
- {
- }
- void ToolDialogImpl::timerEvent(QTimerEvent* event)
- {
- }
- bool ToolDialogImpl::Serialized(QDataStream& ar, bool bIsOut)
- {
- int paranum;//参数数量
- if (bIsOut)//保存参数流程
- {
- paranum = 5;
- ar << paranum;//先保存参数数量
- ar << (int)1 << hv_roiDate;
- ar << (int)2 << m_RoiController;
- ar << (int)3 << m_hoROI;
- ar << (int)4 << ui.spinBoxAmplitudeThreshold->value();
- ar << (int)5 << ui.spinBoxSigmat->value();
-
- }
- else//加载参数流程,参数加载顺序一定要跟保存顺序一致
- {
-
- int para;
- ar >> paranum;//读取参数数量
- for (int i = 0; i < paranum; i++)
- {
- ar >> para;
- switch (para)
- {
- case 1: ar >> hv_roiDate; break;
- case 2: ar >> m_RoiController; break;
- case 3: ar >> m_hoROI; break;
- case 4: ar >> m_nAmplitudeThreshold; break;
- case 5: ar >> m_fSigmat; break;
- default:
- {
- qWarning() << "Serialized(In) Error";
- return false;
- }
- break;
- }
- }
- ui.spinBoxAmplitudeThreshold->setValue(m_nAmplitudeThreshold);
- ui.spinBoxSigmat->setValue(m_fSigmat);
- }
- return true;
- }
- void ToolDialogImpl::on_UpdateUI()
- {
- }
- /// <summary>
- /// 确定
- /// </summary>
- void ToolDialogImpl::on_btnOK_clicked()
- {
- this->hide();
- }
- /// <summary>
- /// 取消按钮
- /// </summary>
- void ToolDialogImpl::on_btnCancel_clicked()
- {
- // 将本工具的恢复到打开工具之前的状态
- RecoverData();
- }
- /// <summary>
- /// 测试按钮
- /// </summary>
- void ToolDialogImpl::on_btnExecute_clicked()
- {
- QElapsedTimer toolTimer;
- toolTimer.start();
- // 发送事件
- ToolEvent* pToolEvent = new ToolEvent(m_strPouName, m_strInstanceName, TOOL_EVENT_TYPE::TOOL_TRIGGER);
- QCoreApplication::sendEvent(m_pEventTarget, pToolEvent);
- // 统计返回值
- VPEnum::RETURN_VALUE ret = pToolEvent->ret;
- double nExecTime = toolTimer.elapsed();
- QString str;
- str = QString("耗时: %1 ms").arg(nExecTime, 0, 'G', 5);
- ui.label_time->setText(str);
- str = QString("状态: %1 ").arg(QMetaEnum::fromType<VPEnum::RETURN_VALUE>().key((short)ret));
- ui.label_state->setText(str);
- delete pToolEvent;
- }
|