From 0cc1d7984354fcb9e135f99d7cef399ad4247050 Mon Sep 17 00:00:00 2001 From: "bob.pan" Date: Tue, 14 Sep 2021 11:39:06 +0800 Subject: [PATCH] =?UTF-8?q?=20=20=E5=8A=A0=E5=85=A5=E6=8A=A0=E5=9B=BE?= =?UTF-8?q?=E7=AE=97=E6=B3=95=E5=8F=82=E6=95=B0=E5=88=B0UI=E4=B8=8A?= =?UTF-8?q?=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/algorithm/AlgorithmCompare.cpp | 84 +++--------------------------- src/algorithm/ImageProcess.cpp | 52 ++++++++++++++---- src/algorithm/ImageProcess.h | 6 ++- src/tpMain/lpGlobalConfig.cpp | 12 +++++ src/tpMain/lpGlobalConfig.h | 6 +++ tpvs17/AlgoTest/QAlgDetect.cpp | 12 ++++- tpvs17/tpMain/lpMainWin.cpp | 6 ++- tpvs17/tpMain/lpSystemConfigUI.cpp | 5 ++ tpvs17/tpMain/lpSystemConfigUI.ui | 83 +++++++++++++++++++++++------ 9 files changed, 158 insertions(+), 108 deletions(-) diff --git a/src/algorithm/AlgorithmCompare.cpp b/src/algorithm/AlgorithmCompare.cpp index 87dd829..0628c8a 100644 --- a/src/algorithm/AlgorithmCompare.cpp +++ b/src/algorithm/AlgorithmCompare.cpp @@ -97,8 +97,6 @@ CAlgorithmCompare::~CAlgorithmCompare(void) } -//cv::BackgroundSubtractorMOG CAlgorithmFluorescence::bgSubtractor(30, 5, 0.95, false); - QString CAlgorithmCompare::bestMatch(const QMap* modelMap, CLocalWheel*pLocal, double* pMinDis /*= NULL*/, int minDisNum /*= -1*/) const { double minDis = DBL_MAX; @@ -279,7 +277,9 @@ int CAlgorithmCompare::IImageAnalysis(class IImageObject* pImgObj) double dD2H = vMap.value("d2h", -1).toDouble(); int nthreshold = vMap.value("Threshold", 15).toInt(); bool bUseBackground = vMap.value("useBackground",false).toBool();//true 使用背景图抠图 false 不使用背景 - + int filterCircleSize = vMap.value("filterSize", 50).toInt();//过滤尺寸 + bool bEqual = vMap.value("bEqual", false).toBool();//是否使用图像增强 + if (nthreshold <= 0) nthreshold = 15; int IsCutedImg = vMap.value("IsCutImg", 0).toInt();//裁剪后的轮毂图 @@ -301,17 +301,16 @@ int CAlgorithmCompare::IImageAnalysis(class IImageObject* pImgObj) else{ bReload = false; } - //imageSegementation(matSrc); + //matMatch = ImageProcess::findCircleObject(matSrc, matBack, bUseBackground, nthreshold/*15*/, &lCircle/* NULL*/); Point2f centerPoint; double radius = 0; - matMatch = ImageProcess::findCircle(matSrc, centerPoint, radius); + matMatch = ImageProcess::findCircle(matSrc, centerPoint, radius, bEqual, filterCircleSize); lCircle.ptCenter = centerPoint; lCircle.fRadius = radius; if (matMatch.cols >= 900 || matMatch.rows >= 900) { - cv::resize(matMatch, matMatch, cv::Size(matMatch.cols / 2, matMatch.rows / 2)); lCircle.fRadius = lCircle.fRadius / 2; } @@ -327,10 +326,8 @@ int CAlgorithmCompare::IImageAnalysis(class IImageObject* pImgObj) pImgObj->IVariantMapToUI(rltMap); return 0; } - //Mat matMatch = ImageProcess::findCircleObject(matSrc, matBack, 15, NULL/* &lCircle*/); - //double dDiameter = dD2H * matMatch.rows; - double dDiameter = dD2H * lCircle.fRadius * 2; + double dDiameter = dD2H * lCircle.fRadius * 2; QVariantMap mResult;//匹配算法结果 CLocalWheel wheelLocal; wheelLocal.defectList = strModelList; @@ -370,7 +367,6 @@ int CAlgorithmCompare::IImageAnalysis(class IImageObject* pImgObj) return 1; } - cv::Mat CAlgorithmCompare::getImage(class IImageObject *pImgObj) { emTpColorFormat colorFormat = pImgObj->IColorFormat(); @@ -418,71 +414,3 @@ cv::Mat CAlgorithmCompare::getBackGroundImage(class IImageObject *pObj, bool bRe } return matback; } - - - -//void CAlgorithmFluorescence::imageSegementation(const cv::Mat &srcImage) -//{ -// Mat mask; -// //Mat sobelImg = srcImage.clone(); -// //genSobelImage(sobelImg); -// //sobelImg.convertTo(sobelImg, CV_8UC1); -// -// -// Mat sobelX, sobelY; -// Sobel(srcImage, sobelX, CV_32FC1, 1, 0, 3, BORDER_REPLICATE); -// Sobel(srcImage, sobelY, CV_32FC1, 0, 1, 3, BORDER_REPLICATE); -// Mat img = sobelX.mul(sobelX) + sobelY.mul(sobelY); -// Mat tempImg; -// img.convertTo(tempImg, CV_32FC1); -// Mat tempImg0; -// sqrt(tempImg, tempImg0); -// img = tempImg0; -// img.convertTo(img, CV_8UC1); -// Mat blurImg; -// cv::medianBlur(img, blurImg, 11); -// bgSubtractor(img, mask, 0.001); -// -// /*cv::Mat element5(5, 5, CV_8U, cv::Scalar(1)); -// cv::Mat closedMat; -// cv::morphologyEx(mask, closedMat, cv::MORPH_CLOSE, element5);*/ -// -// //cv::floodFill(); -// luffy_base::luffyCircle pCircle; -// -// vector mats= ImageProcess::findCircleObject(mask, srcImage, 15, &pCircle); -// if (mats.size() == 0) -// { -// return; -// } -// if (mats[0].size() != mats[1].size()) -// { -// return; -// } -// Mat dilatedImgBin; -// Mat imgBinary = mats[0] > 0; -// dilate(imgBinary, dilatedImgBin, Mat::ones(5, 5, CV_32FC1)); -// erode(dilatedImgBin, imgBinary, Mat::ones(5, 5, CV_32FC1)); -// imgBinary = ~imgBinary; -// //Mat canvas(imgBinary.size(), imgBinary.type(), Scalar::all(0)); -// vector> cons; -// cv::findContours(imgBinary, cons, RETR_EXTERNAL, CHAIN_APPROX_NONE); -// for (const vector & pContour : cons) -// { -// const int& size = pContour.size(); -// if (size < 200) -// { -// Mat(pContour).setTo(255); -// //continue; -// } -// //cv::fillPoly(canvas, vector>(1, pContour), Scalar(255)); -// } -// //Mat tarMat = (~canvas).mul(mats[1]) / 255; -// //openOper(imgBinary, Mat::ones(1, 13, CV_32FC1)); -// -// -// -// -// return; -// -//} diff --git a/src/algorithm/ImageProcess.cpp b/src/algorithm/ImageProcess.cpp index 8d3dcb8..a6a08d5 100644 --- a/src/algorithm/ImageProcess.cpp +++ b/src/algorithm/ImageProcess.cpp @@ -573,7 +573,7 @@ cv::Mat ImageProcess::getForeImage(const Mat & src, const Mat &backgroundImg) return (src - resizedBackgroundImg); } //输入一张灰度图,输出抠出的圆小图和半径,圆心坐标 -cv::Mat ImageProcess::findCircle(const Mat &srcImg, Point2f& center ,double &radius) +cv::Mat ImageProcess::findCircle(const Mat &srcImg, Point2f& center ,double &radius, bool bEqual,int filterSize) { Mat detectImg; Mat src = srcImg; @@ -581,7 +581,9 @@ cv::Mat ImageProcess::findCircle(const Mat &srcImg, Point2f& center ,double &rad 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); @@ -596,6 +598,10 @@ cv::Mat ImageProcess::findCircle(const Mat &srcImg, Point2f& center ,double &rad continue; if (EDCircle[i].center.x + EDCircle[i].r > bBaseX || EDCircle[i].center.y + EDCircle[i].r > bBaseY) continue; + + if((EDCircle[i].r*REAIZE) < filterSize) + continue; + if (EDCircle[i].r > maxR) { maxR = EDCircle[i].r; @@ -612,26 +618,48 @@ cv::Mat ImageProcess::findCircle(const Mat &srcImg, Point2f& center ,double &rad center.x = (EDCircle[nIndex].center.x * REAIZE); center.y = (EDCircle[nIndex].center.y * REAIZE); radius = radius * REAIZE; - return DetectCircle(srcImg, center, radius); + return DetectCircle(srcImg, center, radius, bEqual); } - else { - center.x = 0; - center.y = 0; + else {//EDCirle 没找到圆 直径调用精细找圆算法 + center.x = srcImg.cols / 2; + center.y = srcImg.rows / 2; radius = 0; - return Mat(); + return DetectCircle(srcImg, center, radius, bEqual); } } -Mat ImageProcess::DetectCircle(Mat img, Point2f& center, double& radius) +Mat ImageProcess::DetectCircle(Mat img, Point2f& center, double& radius, bool bEqual) { Mat detectImg; - equalizeHist(img, detectImg); + if (bEqual == true) + { + equalizeHist(img, detectImg); + } + else { + detectImg = img; + } + CircleDetector cd; cd.setAlgoType(CircleDetector::PeakCircle); cd.setEdgeWidth(3); cd.setPolarity(Polarity::White2Black); cd.setFindBy(FindBy::Best); - cd.setRadii(radius - 100, radius + 150); + double difRadiusMin = radius - 100; + double difRadiusMax = radius + 150; + if (difRadiusMin < 0) + { + difRadiusMin = 0; + if(radius>0) + difRadiusMax = abs(center.y - (img.cols / 2)) - 50; + else { + int minV = (img.rows / 2) > (img.cols / 2) ? (img.cols / 2) : (img.rows / 2); + difRadiusMax = minV - 50; + center.x = img.cols / 2; + center.y = img.rows / 2; + } + } + + cd.setRadii(difRadiusMin, difRadiusMax); cd.setACThres(3); vector allScores; Vec3f bestCircle; @@ -648,6 +676,10 @@ Mat ImageProcess::DetectCircle(Mat img, Point2f& 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/src/algorithm/ImageProcess.h b/src/algorithm/ImageProcess.h index 6d5098d..76fba4c 100644 --- a/src/algorithm/ImageProcess.h +++ b/src/algorithm/ImageProcess.h @@ -23,8 +23,10 @@ public: static cv::Mat findCircleObject(const Mat &src, const Mat& backgroundImg, bool useBackgroundFlag, int nThres = 20, luffy_base::luffyCircle *pCircle = NULL); static cv::Mat getForeImage(const Mat & src, const Mat &backgroundImg); - static cv::Mat findCircle(const Mat &srcImg, Point2f& center, double &radius);//找圆 - static cv::Mat DetectCircle(Mat img, Point2f& center, double& radius);//精细找圆 + //找圆 + static cv::Mat findCircle(const Mat &srcImg, Point2f& center, double &radius, bool bEqual = true, int filterSize = 50 ); + //精细找圆 + static cv::Mat DetectCircle(Mat img, Point2f& center, double& radius, bool bEqual); }; #endif diff --git a/src/tpMain/lpGlobalConfig.cpp b/src/tpMain/lpGlobalConfig.cpp index d45a7c6..6407e25 100644 --- a/src/tpMain/lpGlobalConfig.cpp +++ b/src/tpMain/lpGlobalConfig.cpp @@ -43,6 +43,13 @@ void lpGlobalConfig::init(QString strPath) bRunBackClosing = jsMyself.value("runBackClosing").toBool(false);//关闭页面时自动后台运行 strSysTitle = jsMyself.value("systitle").toString(QObject::tr("识别定位一体系统"));//系统标题 + QJsonObject algParamObj = jsMyself.value("algParam").toObject(); + if (!algParamObj.isEmpty()) + { + algParam.bEqual = algParamObj.value("bEqual").toBool(false); + algParam.filterCircleSize = algParamObj.value("filterSize").toInt(50); + } + QJsonObject algObj = jsMyself.value(WHEEL_SELFDEFINE_ALGPARA).toObject(); if (!algObj.isEmpty()) { @@ -292,6 +299,11 @@ void lpGlobalConfig::saveDeteImage() jsMyself.insert("runBackClosing", bRunBackClosing);//关闭页面时自动后台运行 jsMyself.insert("systitle", strSysTitle);//系统标题 + QJsonObject algParamObj; + algParamObj.insert("bEqual", algParam.bEqual); + algParamObj.insert("filterSize", algParam.filterCircleSize); + jsMyself.insert("algParam", algParamObj); + QZkJsonParser::WriteJsonObject(fileMyself, jsMyself); } diff --git a/src/tpMain/lpGlobalConfig.h b/src/tpMain/lpGlobalConfig.h index 4085e41..a788e96 100644 --- a/src/tpMain/lpGlobalConfig.h +++ b/src/tpMain/lpGlobalConfig.h @@ -7,6 +7,10 @@ #include /*系统参数管理类 全局调用*/ +typedef struct tagAlgParam {//算法参数 + bool bEqual{ false };//是否使用增强 + int filterCircleSize{ 50 };//过滤 +}AlgParam; class lpGlobalConfig :public lpsingleton { public: @@ -101,6 +105,8 @@ public: bool bRunBackRunning{ true };//启动是自动后台运行 bool bRunBackClosing{ false };//关闭页面时自动后台运行 QString strSysTitle;//系统标题 + + AlgParam algParam; }; #endif diff --git a/tpvs17/AlgoTest/QAlgDetect.cpp b/tpvs17/AlgoTest/QAlgDetect.cpp index 663ca45..7257d14 100644 --- a/tpvs17/AlgoTest/QAlgDetect.cpp +++ b/tpvs17/AlgoTest/QAlgDetect.cpp @@ -196,7 +196,17 @@ Mat QAlgDetect::DetectCircle(Mat img, QPointF center, double radius,bool bEqual) if (difRadiusMin < 0) { difRadiusMin = 0; - difRadiusMax = abs(center.y()-(img.cols / 2))-50; + if (radius > 0) + { + difRadiusMax = abs(center.y() - (img.cols / 2)) - 50; + } + else + { + int minV = (img.rows / 2) > (img.cols / 2) ? (img.cols / 2) : (img.rows / 2); + difRadiusMax = minV - 50; + center.setX(img.cols / 2); + center.setY(img.rows / 2); + } } cd.setRadii(difRadiusMin, difRadiusMax); diff --git a/tpvs17/tpMain/lpMainWin.cpp b/tpvs17/tpMain/lpMainWin.cpp index 742bcc3..118f871 100644 --- a/tpvs17/tpMain/lpMainWin.cpp +++ b/tpvs17/tpMain/lpMainWin.cpp @@ -736,6 +736,8 @@ QVariant lpMainWin::IGetVariantById(int id) vMap.insert("Threshold", lpGlobalConfig::instance()->m_AlgThres); vMap.insert("IsCutImg", lpGlobalConfig::instance()->m_UseCutImg); vMap.insert("useBackground", lpGlobalConfig::instance()->m_UseBackground>0?true:false);//使用背景图 + vMap.insert("filterSize", lpGlobalConfig::instance()->algParam.filterCircleSize);//定位圆过滤大小 + vMap.insert("bEqual", lpGlobalConfig::instance()->algParam.bEqual);//是否使用图像增强 void* address = (void*)m_pCtrl->getAllModelMapPtr(); long long varadr = (long long)address; @@ -1787,8 +1789,8 @@ Q_SLOT void lpMainWin::onActivated(QSystemTrayIcon::ActivationReason reason) void lpMainWin::setMainWindowVisibility(bool state) { if (state) { - //show(); - showMaximized(); + show(); + //showMaximized(); qApp->processEvents(); qApp->setActiveWindow(this); qApp->processEvents(); diff --git a/tpvs17/tpMain/lpSystemConfigUI.cpp b/tpvs17/tpMain/lpSystemConfigUI.cpp index 1db771c..27af3e5 100644 --- a/tpvs17/tpMain/lpSystemConfigUI.cpp +++ b/tpvs17/tpMain/lpSystemConfigUI.cpp @@ -40,6 +40,8 @@ Q_SLOT void lpSystemConfigUI::onButtonClicked() lpGlobalConfig::instance()->bRunBackRunning = ui.checkBox_runBackRunning->isChecked(); lpGlobalConfig::instance()->bRunBackClosing = ui.checkBox_runBackClosing->isChecked(); lpGlobalConfig::instance()->strSysTitle = ui.lineEdit_4->text(); + lpGlobalConfig::instance()->algParam.bEqual = ui.checkBox_bEqual->isChecked(); + lpGlobalConfig::instance()->algParam.filterCircleSize = ui.lineEdit_filterSize->text().toInt(); lpGlobalConfig::instance()->saveDeteImage(); QString strLanguage = ui.comboBox->currentText(); @@ -83,6 +85,9 @@ void lpSystemConfigUI::showEvent(QShowEvent *event) QSettings languageSetting("hubdetect.ini", QSettings::IniFormat); QString strLanguage = languageSetting.value("language", "Chinese").toString(); ui.comboBox->setCurrentText(strLanguage); + + ui.checkBox_bEqual->setChecked(lpGlobalConfig::instance()->algParam.bEqual); + ui.lineEdit_filterSize->setText(QString("%1").arg(lpGlobalConfig::instance()->algParam.filterCircleSize)); } void lpSystemConfigUI::timerEvent(QTimerEvent *event) diff --git a/tpvs17/tpMain/lpSystemConfigUI.ui b/tpvs17/tpMain/lpSystemConfigUI.ui index 36b6bec..732248a 100644 --- a/tpvs17/tpMain/lpSystemConfigUI.ui +++ b/tpvs17/tpMain/lpSystemConfigUI.ui @@ -29,8 +29,8 @@ 0 0 - 577 - 448 + 560 + 510 @@ -70,6 +70,19 @@ + + + + Qt::Vertical + + + + 20 + 40 + + + + @@ -206,19 +219,6 @@ - - - Qt::Vertical - - - - 20 - 40 - - - - - 其他: @@ -259,6 +259,59 @@ + + + + 算法参数: + + + + + + 过滤小于 + + + + + + + 的圆(单位:像素) + + + + + + + + 100 + 16777215 + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + 是否使用图像增强 + + + + + +