diff --git a/tpvs17/AlgoTest/AlgoTest.cpp b/tpvs17/AlgoTest/AlgoTest.cpp index 4b51846..6fe4d0b 100644 --- a/tpvs17/AlgoTest/AlgoTest.cpp +++ b/tpvs17/AlgoTest/AlgoTest.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #pragma execution_character_set("utf-8") @@ -11,6 +12,7 @@ AlgoTest::AlgoTest(QWidget *parent) { ui.setupUi(this); connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(onButtonClicked())); + connect(ui.m_pbSelect, SIGNAL(clicked()), this, SLOT(onButtonClicked())); { QGridLayout *pLayout = new QGridLayout(ui.widget_src); @@ -24,7 +26,9 @@ AlgoTest::AlgoTest(QWidget *parent) pLayout->addWidget(m_rltView); ui.widget_rlt->setLayout(pLayout); } - loadImage(); + QString strPath = "H:\\新建文件夹\\Source\\"; + //QString strPath = "H:\\新建文件夹\\7\\"; + loadImage(strPath); m_pAlg = new QAlgDetect(); } @@ -47,10 +51,11 @@ AlgoTest::~AlgoTest() } } -void AlgoTest::loadImage() +void AlgoTest::loadImage(QString strPath) { - QString strPath = "H:\\新建文件夹\\Source\\"; - //QString strPath = "H:\\新建文件夹\\7\\"; + m_filelst.clear(); + m_gIndex = 0; + QStringList fileFilter; fileFilter<< "*.BMP" << "*.JPEG" << "*.JPG" << "*.PNG"; @@ -79,6 +84,7 @@ Q_SLOT void AlgoTest::onButtonClicked() if (m_gIndex >= m_filelst.size()) m_gIndex = 0; QString strPath = m_filelst.at(m_gIndex); + ui.label->setText(strPath); m_gIndex++; QImage img; @@ -87,8 +93,19 @@ Q_SLOT void AlgoTest::onButtonClicked() { m_srcView->setImg(img); } - + bool bCheck = ui.checkBox->isChecked(); AlgCallBack func = std::bind(&AlgoTest::algCallFunc, this, std::placeholders::_1); - m_pAlg->detect(img, func); + QVariantMap param; + param.insert("equal", bCheck); + m_pAlg->detect(img, param, func); + } + else if ("m_pbSelect" == strObj) + { + QString dirPath = QFileDialog::getExistingDirectory(this, "", ""); + if (dirPath.isEmpty()) + return; + loadImage(dirPath); } } + + diff --git a/tpvs17/AlgoTest/AlgoTest.h b/tpvs17/AlgoTest/AlgoTest.h index 7af9cd3..db295f8 100644 --- a/tpvs17/AlgoTest/AlgoTest.h +++ b/tpvs17/AlgoTest/AlgoTest.h @@ -13,7 +13,7 @@ class AlgoTest : public QMainWindow public: AlgoTest(QWidget *parent = Q_NULLPTR); ~AlgoTest(); - void loadImage(); + void loadImage(QString strPath); void algCallFunc(QImage img); Q_SLOT void onButtonClicked(); private: diff --git a/tpvs17/AlgoTest/AlgoTest.ui b/tpvs17/AlgoTest/AlgoTest.ui index 927e3d7..7bc60e4 100644 --- a/tpvs17/AlgoTest/AlgoTest.ui +++ b/tpvs17/AlgoTest/AlgoTest.ui @@ -6,8 +6,8 @@ 0 0 - 681 - 748 + 804 + 666 @@ -15,14 +15,7 @@ - - - - PushButton - - - - + Qt::Vertical @@ -35,7 +28,21 @@ - + + + + run + + + + + + + select + + + + @@ -63,7 +70,7 @@ - 缁撴灉 + 鎶犲浘缁撴灉 @@ -79,6 +86,20 @@ + + + + + + + + + + + 浣跨敤澧炲己 + + + diff --git a/tpvs17/AlgoTest/AlgoTest.vcxproj b/tpvs17/AlgoTest/AlgoTest.vcxproj index 2d937a3..670fd13 100644 --- a/tpvs17/AlgoTest/AlgoTest.vcxproj +++ b/tpvs17/AlgoTest/AlgoTest.vcxproj @@ -85,7 +85,7 @@ true UNICODE;_UNICODE;WIN32;WIN64;QT_DLL;QT_NO_DEBUG;NDEBUG;QT_CORE_LIB;QT_GUI_LIB;QT_WIDGETS_LIB;%(PreprocessorDefinitions) - .\GeneratedFiles;.;$(QTDIR)\include;.\GeneratedFiles\$(ConfigurationName);$(QTDIR)\include\QtCore;$(QTDIR)\include\QtGui;$(QTDIR)\include\QtANGLE;$(QTDIR)\include\QtWidgets;%(AdditionalIncludeDirectories) + .\GeneratedFiles;.;$(QTDIR)\include;.\GeneratedFiles\$(ConfigurationName);$(QTDIR)\include\QtCore;$(QTDIR)\include\QtGui;$(QTDIR)\include\QtANGLE;$(QTDIR)\include\QtWidgets;E:\wheelValve\3part\opencv3.4.1\include;E:\wheelValve\3part\opencv3.4.1\include\opencv;E:\wheelValve\3part\opencv3.4.1\include\opencv2;E:\wheelValve\src\tpMain\algela;E:\wheelValve\3part\edcircle\include;E:\wheelValve\3part\Cyclops\include;%(AdditionalIncludeDirectories) MultiThreadedDLL true @@ -93,14 +93,14 @@ Windows $(OutDir)\$(ProjectName).exe - $(QTDIR)\lib;%(AdditionalLibraryDirectories) + $(QTDIR)\lib;E:\wheelValve\3part\opencv3.4.1\x64\vc15\lib;E:\wheelValve\3part\edcircle\lib;E:\wheelValve\3part\Cyclops\lib;%(AdditionalLibraryDirectories) false - qtmain.lib;Qt5Core.lib;Qt5Gui.lib;Qt5Widgets.lib;%(AdditionalDependencies) + qtmain.lib;Qt5Core.lib;Qt5Gui.lib;Qt5Widgets.lib;opencv_world341.lib;EDCircle.lib;Cyclops.lib;%(AdditionalDependencies) .\GeneratedFiles\$(ConfigurationName)\moc_%(Filename).cpp Moc'ing %(Identity)... - .\GeneratedFiles;.;$(QTDIR)\include;.\GeneratedFiles\$(ConfigurationName);$(QTDIR)\include\QtCore;$(QTDIR)\include\QtGui;$(QTDIR)\include\QtANGLE;$(QTDIR)\include\QtWidgets;%(AdditionalIncludeDirectories) + .\GeneratedFiles;.;$(QTDIR)\include;.\GeneratedFiles\$(ConfigurationName);$(QTDIR)\include\QtCore;$(QTDIR)\include\QtGui;$(QTDIR)\include\QtANGLE;$(QTDIR)\include\QtWidgets;E:\wheelValve\3part\opencv3.4.1\include;E:\wheelValve\3part\opencv3.4.1\include\opencv;E:\wheelValve\3part\opencv3.4.1\include\opencv2;E:\wheelValve\src\tpMain\algela;E:\wheelValve\3part\edcircle\include;E:\wheelValve\3part\Cyclops\include;%(AdditionalIncludeDirectories) UNICODE;_UNICODE;WIN32;WIN64;QT_DLL;QT_NO_DEBUG;NDEBUG;QT_CORE_LIB;QT_GUI_LIB;QT_WIDGETS_LIB;%(PreprocessorDefinitions) diff --git a/tpvs17/AlgoTest/QAlgDetect.cpp b/tpvs17/AlgoTest/QAlgDetect.cpp index c6b1033..663ca45 100644 --- a/tpvs17/AlgoTest/QAlgDetect.cpp +++ b/tpvs17/AlgoTest/QAlgDetect.cpp @@ -104,8 +104,10 @@ QAlgDetect::~QAlgDetect() #define REAIZE 4 -void QAlgDetect::detect(QImage img, AlgCallBack func) +void QAlgDetect::detect(QImage img, QVariantMap param, AlgCallBack func) { + bool bEqual = param.value("equal").toBool(); + Mat srcImg = QImageToMat(img); if(srcImg.channels()!=1) cv::cvtColor(srcImg, srcImg, CV_RGB2GRAY); @@ -115,7 +117,8 @@ void QAlgDetect::detect(QImage img, AlgCallBack func) cv::resize(src, detectImg, cv::Size(src.cols / REAIZE, src.rows / REAIZE)); int bBaseX = detectImg.cols; int bBaseY = detectImg.rows; -// equalizeHist(detectImg, detectImg);//根据图像的实际情况使用 图像暗一点不需要,亮一点的需要 + if(bEqual == true) + equalizeHist(detectImg, detectImg);//根据图像的实际情况使用 图像暗一点不需要,亮一点的需要 detectImg = _EnhanImg_sharpen(detectImg); EDCircles edcircles(detectImg); @@ -130,6 +133,8 @@ void QAlgDetect::detect(QImage img, AlgCallBack func) continue; if (EDCircle[i].center.x + EDCircle[i].r > bBaseX || EDCircle[i].center.y + EDCircle[i].r > bBaseY) continue; + if(EDCircle[i].r< 50 ) + continue; if (EDCircle[i].r > maxR) { maxR = EDCircle[i].r; @@ -147,57 +152,26 @@ void QAlgDetect::detect(QImage img, AlgCallBack func) centerP.setX(EDCircle[nIndex].center.x * REAIZE); centerP.setY(EDCircle[nIndex].center.y * REAIZE); double r = radius * REAIZE; - Mat aa = DetectCircle(srcImg, centerP, r); + Mat aa = DetectCircle(srcImg, centerP, r, bEqual); QImage rltImg = cvMat2QImage(aa); if (func) { func(rltImg); return; } - - - if (startX > 0 && startY > 0 && hight > 0 \ - && startX < src.cols &&startY < src.rows \ - && hight < src.cols&&hight < src.rows \ - && (startX + hight) < src.cols && (startY + hight) < src.rows) - { - Mat cutMat = src(Rect(startX, startY, hight, hight)); - if (cutMat.data != NULL) - { -// if (hight < 50) -// return Mat(); - -// pCircle->ptCenter = center; -// pCircle->fRadius = radius * REAIZE; - - //2021-05-10 增加图像大小判断 对超过900像素的图像进行再一次压缩 - if (cutMat.cols >= 900 || cutMat.rows >= 900) - { - Mat newCutImg; - cv::resize(cutMat, newCutImg, cv::Size(cutMat.cols / REAIZE, cutMat.rows / REAIZE)); -// pCircle->fRadius = pCircle->fRadius / REAIZE; -// return newCutImg; - - QImage rltImg = cvMat2QImage(newCutImg); - if (func) { - func(rltImg); - return; - } - } - else { - QImage rltImg = cvMat2QImage(cutMat); - if (func) { - func(rltImg); - return; - } - } - } + } + else { + QPointF centerP; + centerP.setX(srcImg.cols/2); + centerP.setY(srcImg.rows/2); + double r = 0; + Mat aa = DetectCircle(srcImg, centerP, r, bEqual); + QImage rltImg = cvMat2QImage(aa); + if (func) { + func(rltImg); + return; } } - - - - if (func) { func(img); @@ -205,11 +179,13 @@ void QAlgDetect::detect(QImage img, AlgCallBack func) } -Mat QAlgDetect::DetectCircle(Mat img, QPointF center, double radius) +Mat QAlgDetect::DetectCircle(Mat img, QPointF center, double radius,bool bEqual) { Mat detectImg; - //equalizeHist(img, detectImg); - detectImg = img; + if(bEqual == true) + equalizeHist(img, detectImg); + else + detectImg = img; CircleDetector cd; cd.setAlgoType(CircleDetector::PeakCircle); cd.setEdgeWidth(3); @@ -237,6 +213,10 @@ Mat QAlgDetect::DetectCircle(Mat img, QPointF center, double radius) Rect rect; rect.x = cen.x() - r; rect.y = cen.y() - r; + if (rect.x < 0) + rect.x = 0; + if (rect.y < 0) + rect.y = 0; rect.width = 2 * r; rect.height = 2 * r; Mat s = img(rect); diff --git a/tpvs17/AlgoTest/QAlgDetect.h b/tpvs17/AlgoTest/QAlgDetect.h index 7268a11..1f35b20 100644 --- a/tpvs17/AlgoTest/QAlgDetect.h +++ b/tpvs17/AlgoTest/QAlgDetect.h @@ -4,6 +4,7 @@ #include #include #include +#include using namespace cv; typedef std::function AlgCallBack; @@ -15,8 +16,8 @@ public: QAlgDetect(QObject *parent = nullptr); ~QAlgDetect(); - void detect(QImage img, AlgCallBack func); - Mat DetectCircle(Mat img, QPointF center, double radius); + void detect(QImage img, QVariantMap param, AlgCallBack func); + Mat DetectCircle(Mat img, QPointF center, double radius, bool bEqual); private: };