From 9659428e7f10d67ea317f2c51766f097553956cc Mon Sep 17 00:00:00 2001 From: xiewenji <527774126@qq.com> Date: Wed, 26 Nov 2025 15:28:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96ZYWL=E6=A3=80=E6=B5=8B?= =?UTF-8?q?=E7=BB=93=E6=9E=9C=EF=BC=8C=E5=A2=9E=E5=8A=A0=E8=8A=AF=E7=89=87?= =?UTF-8?q?=E5=B0=BA=E5=AF=B8=E5=8F=82=E6=95=B0=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ALL_Det/example/test_example.cpp | 10 ++-- ALL_Det/src/ALL_Detect.cpp | 6 ++- CheckBase/include/CheckBaseParam.hpp | 9 +++- CheckBase/include/DetCommonDefine.hpp | 26 --------- CheckBase/include_base/ImgCheckConfig.h | 28 +++++++++- CheckBase/src/CheckBaseParam.cpp | 43 +++++---------- Down_Angle_Det/src/Down_Angle_Detect.cpp | 4 +- Down_Det/src/Down_Detect.cpp | 56 ++++++++++++++++---- Side_Det/src/Side_Detect.cpp | 67 ++++++++++++++++++------ TOP_Det/src/Top_Detect.cpp | 57 ++++++++++++++++---- 10 files changed, 202 insertions(+), 104 deletions(-) diff --git a/ALL_Det/example/test_example.cpp b/ALL_Det/example/test_example.cpp index b9799b0..455b1b9 100644 --- a/ALL_Det/example/test_example.cpp +++ b/ALL_Det/example/test_example.cpp @@ -83,8 +83,8 @@ int main(int argc, char *argv[]) // ReadSystemConfig("../data/Top1_Det/System_Config.json", psystem_param); // CAMERA_POSITION cam_position = CAMERA_TOP_1;//相机位置 - // ReadSystemConfig("../data/Top2_Det/System_Config.json", psystem_param); - // CAMERA_POSITION cam_position = CAMERA_TOP_2;//相机位置 + ReadSystemConfig("../data/Top2_Det/System_Config.json", psystem_param); + CAMERA_POSITION cam_position = CAMERA_TOP_2;//相机位置 // ReadSystemConfig("../data/Side1_Det/System_Config.json", psystem_param); // CAMERA_POSITION cam_position = CAMERA_SIDE_1;//相机位置 @@ -96,9 +96,9 @@ int main(int argc, char *argv[]) // CAMERA_POSITION cam_position = CAMERA_DOWN_1;//相机位置 //测旋转 - ReadSystemConfig("../data/Down_Det/System_Config.json", psystem_param); - CAMERA_POSITION cam_position = CAMERA_DOWN_1;//相机位置 - detect_type = DETECT_TYPE_ANGLE; + // ReadSystemConfig("../data/Down_Det/System_Config.json", psystem_param); + // CAMERA_POSITION cam_position = CAMERA_DOWN_1;//相机位置 + // detect_type = DETECT_TYPE_ANGLE; std::shared_ptr pImgCount = ImgCheckBase::GetInstance(); printf("%s %s \n", pImgCount->GetVersion().c_str(), pImgCount->GetErrorInfo().c_str()); diff --git a/ALL_Det/src/ALL_Detect.cpp b/ALL_Det/src/ALL_Detect.cpp index e22c80e..60fd09f 100644 --- a/ALL_Det/src/ALL_Detect.cpp +++ b/ALL_Det/src/ALL_Detect.cpp @@ -65,13 +65,17 @@ int ALL_Detect::CheckImg(std::shared_ptr p, std::shared_ptrcam_position) { + std::cout << "ERROR: ALL_Detect cam_position not match, expect: " << m_cam_param.cam_position + << ", but got: " << p->cam_position << std::endl; return -1; } if (p->detect_type == DETECT_TYPE_ANGLE) { - if (p->cam_position != CAMERA_DOWN_1) + if (p->cam_position != CAMERA_DOWN_1) { + std::cout << "ERROR: Down_Angle_Detect only support CAMERA_DOWN_1" << std::endl; return -2; + } this->down_angle_detect.CheckImg(p, pResult); } else diff --git a/CheckBase/include/CheckBaseParam.hpp b/CheckBase/include/CheckBaseParam.hpp index 7fa2d3c..a129cbe 100644 --- a/CheckBase/include/CheckBaseParam.hpp +++ b/CheckBase/include/CheckBaseParam.hpp @@ -126,7 +126,10 @@ struct Base_Function_DetConfig DetRotateType rotate; cv::Rect LabelPolygonBoundingRect; // 标记的芯片多边形最大外接矩形 1107-add std::vector pointArry; // 最小外接矩形 - + int chip_width_mm; // 芯片宽度 mm 1107-add + int chip_height_mm; // 芯片高度 mm 1107-add + int chip_width_offset_mm; // 芯片宽度偏差 mm + int chip_height_offset_mm; // 芯片高度偏差 mm 1107-add Base_Function_DetConfig() { Init(); @@ -139,6 +142,8 @@ struct Base_Function_DetConfig LabelPolygonBoundingRect = cv::Rect(0, 0, 0, 0); // 1107-add pointArry.clear(); // 1107-add pointArry.shrink_to_fit(); // 1107-add + chip_width_mm = 0; // 1107-add + chip_height_mm = 0; // 1107-add } void copy(Base_Function_DetConfig tem) @@ -148,6 +153,8 @@ struct Base_Function_DetConfig this->rotate = tem.rotate; this->pointArry.assign(tem.pointArry.begin(), tem.pointArry.end()); // 1107-add this->LabelPolygonBoundingRect = tem.LabelPolygonBoundingRect; // 1107-add + this->chip_width_mm = tem.chip_width_mm; // 1107-add + this->chip_height_mm = tem.chip_height_mm; // 1107 } void print(std::string str) { diff --git a/CheckBase/include/DetCommonDefine.hpp b/CheckBase/include/DetCommonDefine.hpp index f8e7e50..8184986 100644 --- a/CheckBase/include/DetCommonDefine.hpp +++ b/CheckBase/include/DetCommonDefine.hpp @@ -73,30 +73,4 @@ struct ROI2ROI_SCALE } }; -struct BlobResult -{ - - int nresult; - int nYsresult; - int type; - int area_piexl; - float area_mm2; - int hj; - cv::Rect roi; - BlobResult() - { - Init(); - } - void Init() - { - nresult = 0; - nYsresult = 0; - type = 0; - area_piexl = 0; - area_mm2 = 0; - hj = 0; - roi = cv::Rect(0, 0, 0, 0); - } -}; - #endif \ No newline at end of file diff --git a/CheckBase/include_base/ImgCheckConfig.h b/CheckBase/include_base/ImgCheckConfig.h index 8bf3557..bca724a 100644 --- a/CheckBase/include_base/ImgCheckConfig.h +++ b/CheckBase/include_base/ImgCheckConfig.h @@ -15,10 +15,10 @@ #define _ImgCheckConfig_HPP_ #include #include -#include "DetCommonDefine.hpp" #define RESULT_VERSION 1 #define RESULT_WIDTH 800 +#define RESULT_HEIGHT 600 struct VERSION_INFO { @@ -62,6 +62,32 @@ struct Cam_Param } }; +struct BlobResult +{ + + int nresult; + int nYsresult; + int type; + int area_piexl; + float area_mm2; + int hj; + cv::Rect roi; + BlobResult() + { + Init(); + } + void Init() + { + nresult = 0; + nYsresult = 0; + type = 0; + area_piexl = 0; + area_mm2 = 0; + hj = 0; + roi = cv::Rect(0, 0, 0, 0); + } +}; + // 一个检测项基本信息,包括图片序号,图片、开始时间 struct shareImage { diff --git a/CheckBase/src/CheckBaseParam.cpp b/CheckBase/src/CheckBaseParam.cpp index 064c11d..64c4261 100644 --- a/CheckBase/src/CheckBaseParam.cpp +++ b/CheckBase/src/CheckBaseParam.cpp @@ -451,28 +451,13 @@ int CheckBaseParamJson::GetFunction(Json::Value value) } } - // //测试 - // auto test_param = value_f["form"]["Crop_Config"]["DD"].asInt(); // 1020-add - // bool dd_disabled = value_f["form"]["Crop_Config"]["DD_disabled"].asBool(); // 1020-add - - // auto LablCircleradius = value_f["form"]["Test_Data"]["CenterP"]["radius"].asInt(); - // auto LablCircleCoordinate = value_f["form"]["Test_Data"]["CenterP"]["path"]; - // cv::Point CircleP = cv::Point(0, 0); - // if (LablCircleCoordinate.isArray()) - // { - // CircleP.x = LablCircleCoordinate[0][0].asInt(); - // CircleP.y = LablCircleCoordinate[0][1].asInt(); - // } - - // std::vector lablPoints; - // auto test_area = value_f["form"]["Test_Data"]["test_area"]; - // if (test_area.isArray()) - // { - // for (auto it: test_area) { - // lablPoints.emplace_back(cv::Point(it[0].asInt(), it[1].asInt())); - // } - // } + // 芯片尺寸 + pdetConfig->chip_width_mm = value_f["form"]["Chip_Size"]["ChipWidth"].asInt(); // 1020-add + pdetConfig->chip_height_mm = value_f["form"]["Chip_Size"]["ChipHeight"].asInt(); // 1020-add + pdetConfig->chip_width_offset_mm = value_f["form"]["Chip_Size"]["ChipWOffset"].asInt(); // 1020-add + pdetConfig->chip_height_offset_mm = value_f["form"]["Chip_Size"]["ChipHOffset"].asInt(); // 1020-add + // 旋转角度 auto rotate = value_f["form"]["RotateConfig"]["Rotate"].asInt(); // 1020-add if (rotate == 0) { @@ -496,10 +481,10 @@ int CheckBaseParamJson::GetFunction(Json::Value value) } } } - else - { - pdetConfig->Init(); - } + // else + // { + // pdetConfig->Init(); + // } } if ("ChipOffset" == strCode) // 1009-add @@ -557,10 +542,10 @@ int CheckBaseParamJson::GetFunction(Json::Value value) pdetConfig->LabelPolygonBoundingRect = cv::boundingRect(pdetConfig->pointArry); // 获取芯片区域的标记多边形最大外接矩形 } } - else - { - pdetConfig->Init(); - } + // else + // { + // pdetConfig->Init(); + // } } return 0; } diff --git a/Down_Angle_Det/src/Down_Angle_Detect.cpp b/Down_Angle_Det/src/Down_Angle_Detect.cpp index 23c26a8..6321bd2 100644 --- a/Down_Angle_Det/src/Down_Angle_Detect.cpp +++ b/Down_Angle_Det/src/Down_Angle_Detect.cpp @@ -338,7 +338,7 @@ int Down_Angle_Detect::CheckRun() m_LabelCenterP = LabelCenterP; // 计算与x轴角度,正负按照RotatedRect定义 float LabelAngle = rotatedRect.size.width < rotatedRect.size.height ? 90 - std::abs(rotatedRect.angle) : rotatedRect.angle; - // 按照短边计算距离 + // 按照长边计算距离 double LabelP1P2dis = std::max(rotatedRect.size.width, rotatedRect.size.height); // float LabelAngle = calculate_angle2(LineP1, LineP2); @@ -767,7 +767,7 @@ int Down_Angle_Detect::AI_Run(const cv::Mat &img, cv::Point s2StartP, double Lab { continue; } - + //二次定位 const int h = 360; cv::Point start{MaxRoi.x, MaxRoi.y + MaxRoi.height - h / 2}; cv::Rect SecondAlignRoi{start.x, start.y, MaxRoi.width, h}; diff --git a/Down_Det/src/Down_Detect.cpp b/Down_Det/src/Down_Detect.cpp index b18f63f..97ba997 100644 --- a/Down_Det/src/Down_Detect.cpp +++ b/Down_Det/src/Down_Detect.cpp @@ -1,6 +1,7 @@ #include "Down_Detect.hpp" #include "CheckErrorCode.hpp" #include "CheckUtil.hpp" +#include "DetCommonDefine.hpp" using namespace cv; using namespace std; // 检测模型 单个小图 输入模型图片尺寸 @@ -86,6 +87,8 @@ int Down_Detect::CheckRun() t1 = CheckUtil::getcurTime(); CheckImgInit(); cv::Mat detimg = DetImgInfo_shareP->img; + //准备结果图 + cv::resize(detimg.clone(), m_CheckResult_shareP->resultImg, cv::Size(800, 600)); if (detimg.empty()) { return 1; @@ -299,13 +302,30 @@ int Down_Detect::Draw(const cv::Mat &img, cv::Mat &resultimg) { resultimg = img.clone(); } + // //裁剪到包括产品(1600x1200) + // int w = 1600; + // int h = 1200; + // cv::Point center{this->m_AlignMaxRoi.x + this->m_AlignMaxRoi.width / 2, this->m_AlignMaxRoi.y + this->m_AlignMaxRoi.height / 2}; + + // int x = center.x - w / 2; + // int y = center.y - h / 2; + + // // 确保裁剪区域不超出图像边界 + // x = std::max(0, x); + // y = std::max(0, y); + // x = std::min(x, img.cols - w); + // y = std::min(y, img.rows - h); + + // // 创建裁剪区域 + // cv::Rect cropRoi(x, y, w, h); + // resultimg = resultimg(cropRoi).clone(); // cv::polylines(resultimg, m_CheckBaseConfig->pointArry, true, cv::Scalar(0, 255, 0), 2); - auto regions = m_CheckBaseConfig->nodeConfigArr[0].regionConfigArr; - for (int i = 0; i < regions.size(); i++) - { - cv::polylines(resultimg, regions[i].basicInfo.pointArry, true, cv::Scalar(0, 255, 0), 2); - } + // auto regions = m_CheckBaseConfig->nodeConfigArr[0].regionConfigArr; + // for (int i = 0; i < regions.size(); i++) + // { + // cv::polylines(resultimg, regions[i].basicInfo.pointArry, true, cv::Scalar(0, 255, 0), 2); + // } int fontFace = cv::FONT_HERSHEY_SIMPLEX; // 字体样式 double fontScale = 1; // 字体大小 @@ -352,9 +372,9 @@ int Down_Detect::Draw(const cv::Mat &img, cv::Mat &resultimg) p.y = m_resultList.at(i).roi.y; cv::putText(resultimg, show_info, p, fontFace, 1, color, 1); } - //裁剪 - cv::Size sz{RESULT_WIDTH, resultimg.rows}; - cv::resize(resultimg, resultimg, sz); + // 裁剪 + cv::Size dstSize(RESULT_WIDTH, RESULT_HEIGHT); + cv::resize(resultimg, resultimg, dstSize); return 0; } @@ -467,6 +487,20 @@ int Down_Detect::ImgAlinRotate(const cv::Mat &img, cv::Mat &alignImg) printf("Align is Error exit \n"); return 1; } + + //判断尺寸 + int w = m_CheckBaseConfig->baseCheckFunction.detconfig.chip_width_mm / m_CheckBaseConfig->imageScaleParam.fScale_X; + int h = m_CheckBaseConfig->baseCheckFunction.detconfig.chip_height_mm / m_CheckBaseConfig->imageScaleParam.fScale_Y; + auto w_offset = std::abs(w - m_AlignMaxRoi.width); + auto h_offset = std::abs(h - m_AlignMaxRoi.height); + int param_w_offset = m_CheckBaseConfig->baseCheckFunction.detconfig.chip_width_offset_mm / m_CheckBaseConfig->imageScaleParam.fScale_X; + int param_h_offset = m_CheckBaseConfig->baseCheckFunction.detconfig.chip_height_offset_mm / m_CheckBaseConfig->imageScaleParam.fScale_Y; + if (w_offset > param_w_offset || h_offset > param_h_offset) + { + printf("Align error, no product\n"); + return 1; + } + DetRotateType ratio = m_CheckBaseConfig->baseCheckFunction.detconfig.rotate; std::cout << "rotate ratio : " << ratio << std::endl; if (ratio == Ratio_0) @@ -661,7 +695,7 @@ int Down_Detect::DetectImg(const cv::Mat &img) if (tem.area_mm2 > cur_check.area && tem.hj > cur_check.hj && cur_check.bEnable) { tem.nresult = 1; - printf("region %d NG tem.nresult : %f>%f \n", i, tem.area_mm2, m_CheckBaseConfig->qxSegParam.Area); + printf("region %d NG tem.nresult : %f>%f \n", i, tem.area_mm2, cur_check.area); } else { @@ -669,11 +703,11 @@ int Down_Detect::DetectImg(const cv::Mat &img) if (tem.area_mm2 > cur_ys.area && tem.hj > cur_ys.hj && cur_ys.bEnable) { tem.nYsresult = 1; - printf("region %d YS tem.nresult : %f>%f \n", i, tem.area_mm2, m_CheckBaseConfig->ysSegParam.Area); + printf("region %d YS tem.nresult : %f>%f \n", i, tem.area_mm2, cur_ys.area); } else { - printf("region %d OK tem.nresult : %f < %f \n", i, tem.area_mm2, m_CheckBaseConfig->qxSegParam.Area); + printf("region %d OK tem.nresult : %f < %f \n", i, tem.area_mm2, cur_check.area); } } } diff --git a/Side_Det/src/Side_Detect.cpp b/Side_Det/src/Side_Detect.cpp index 67a8439..cc44b56 100644 --- a/Side_Det/src/Side_Detect.cpp +++ b/Side_Det/src/Side_Detect.cpp @@ -1,6 +1,7 @@ #include "Side_Detect.hpp" #include "CheckErrorCode.hpp" #include "CheckUtil.hpp" +#include "DetCommonDefine.hpp" using namespace cv; using namespace std; // 检测模型 单个小图 输入模型图片尺寸 @@ -87,6 +88,8 @@ int Side_Detect::CheckRun() t1 = CheckUtil::getcurTime(); CheckImgInit(); cv::Mat detimg = DetImgInfo_shareP->img; + //准备结果图 + cv::resize(detimg.clone(), m_CheckResult_shareP->resultImg, cv::Size(800, 600)); if (detimg.empty()) { return 1; @@ -124,7 +127,7 @@ int Side_Detect::CheckRun() t2 = CheckUtil::getcurTime(); m_CheckResult_shareP->UseTimeMS = t2 - t1; - re = Draw(AlignImg, m_CheckResult_shareP->resultImg); + re = Draw(detimg, m_CheckResult_shareP->resultImg); if (0 != re) { return re; @@ -300,13 +303,30 @@ int Side_Detect::Draw(const cv::Mat &img, cv::Mat &resultimg) { resultimg = img.clone(); } + //裁剪到包括产品(1600x1200) + int w = 1600; + int h = 1200; + cv::Point center{this->m_AlignMaxRoi.x + this->m_AlignMaxRoi.width / 2, this->m_AlignMaxRoi.y + this->m_AlignMaxRoi.height / 2}; + + int x = center.x - w / 2; + int y = center.y - h / 2; + + // 确保裁剪区域不超出图像边界 + x = std::max(0, x); + y = std::max(0, y); + x = std::min(x, img.cols - w); + y = std::min(y, img.rows - h); + + // 创建裁剪区域 + cv::Rect cropRoi(x, y, w, h); + resultimg = resultimg(cropRoi).clone(); // cv::polylines(resultimg, m_CheckBaseConfig->pointArry, true, cv::Scalar(0, 255, 0), 2); - auto regions = m_CheckBaseConfig->nodeConfigArr[0].regionConfigArr; - for (int i = 0; i < regions.size(); i++) - { - cv::polylines(resultimg, regions[i].basicInfo.pointArry, true, cv::Scalar(0, 255, 0), 2); - } + // auto regions = m_CheckBaseConfig->nodeConfigArr[0].regionConfigArr; + // for (int i = 0; i < regions.size(); i++) + // { + // cv::polylines(resultimg, regions[i].basicInfo.pointArry, true, cv::Scalar(0, 255, 0), 2); + // } int fontFace = cv::FONT_HERSHEY_SIMPLEX; // 字体样式 double fontScale = 1; // 字体大小 @@ -332,8 +352,8 @@ int Side_Detect::Draw(const cv::Mat &img, cv::Mat &resultimg) } cv::Rect roi = m_resultList.at(i).roi; cv::Point pc; - pc.x = roi.x + roi.width * 0.5; - pc.y = roi.y + roi.height * 0.5; + pc.x = roi.x + roi.width * 0.5 + (m_AlignMaxRoi.x - cropRoi.x);//转换为cropRoi中坐标 + pc.y = roi.y + roi.height * 0.5 + (m_AlignMaxRoi.y - cropRoi.y); int rw = roi.width * 0.5; int rh = roi.height * 0.5; int r = std::sqrt(rw * rw + rh * rh) * 1.1 + 8; @@ -349,13 +369,13 @@ int Side_Detect::Draw(const cv::Mat &img, cv::Mat &resultimg) string show_info = buffer; cv::Point p; - p.x = m_resultList.at(i).roi.x; - p.y = m_resultList.at(i).roi.y; + p.x = m_resultList.at(i).roi.x + (m_AlignMaxRoi.x - cropRoi.x);//转换为cropRoi中坐标 + p.y = m_resultList.at(i).roi.y + (m_AlignMaxRoi.y - cropRoi.y); cv::putText(resultimg, show_info, p, fontFace, 1, color, 1); } - //裁剪 - cv::Size sz{RESULT_WIDTH, resultimg.rows}; - cv::resize(resultimg, resultimg, sz); + // 裁剪 + cv::Size dstSize(RESULT_WIDTH, RESULT_HEIGHT); + cv::resize(resultimg, resultimg, dstSize); return 0; } @@ -465,9 +485,22 @@ int Side_Detect::ImgAlinRotate(const cv::Mat &img, cv::Mat &alignImg) if (alignImg.empty()) { - printf("Align is Error exit \n"); + printf("Align is empty, Error exit \n"); return 1; } + //判断尺寸 + int w = m_CheckBaseConfig->baseCheckFunction.detconfig.chip_width_mm / m_CheckBaseConfig->imageScaleParam.fScale_X; + int h = m_CheckBaseConfig->baseCheckFunction.detconfig.chip_height_mm / m_CheckBaseConfig->imageScaleParam.fScale_Y; + auto w_offset = std::abs(w - m_AlignMaxRoi.width); + auto h_offset = std::abs(h - m_AlignMaxRoi.height); + int param_w_offset = m_CheckBaseConfig->baseCheckFunction.detconfig.chip_width_offset_mm / m_CheckBaseConfig->imageScaleParam.fScale_X; + int param_h_offset = m_CheckBaseConfig->baseCheckFunction.detconfig.chip_height_offset_mm / m_CheckBaseConfig->imageScaleParam.fScale_Y; + if (w_offset > param_w_offset || h_offset > param_h_offset) + { + printf("Align error, no product\n"); + return 1; + } + DetRotateType ratio = m_CheckBaseConfig->baseCheckFunction.detconfig.rotate; std::cout << "rotate ratio : " << ratio << std::endl; if (ratio == Ratio_0) @@ -662,7 +695,7 @@ int Side_Detect::DetectImg(const cv::Mat &img) if (tem.area_mm2 > cur_check.area && tem.hj > cur_check.hj && cur_check.bEnable) { tem.nresult = 1; - printf("region %d NG tem.nresult : %f>%f \n", i, tem.area_mm2, m_CheckBaseConfig->qxSegParam.Area); + printf("region %d NG tem.nresult : %f>%f \n", i, tem.area_mm2, cur_check.area); } else { @@ -670,11 +703,11 @@ int Side_Detect::DetectImg(const cv::Mat &img) if (tem.area_mm2 > cur_ys.area && tem.hj > cur_ys.hj && cur_ys.bEnable) { tem.nYsresult = 1; - printf("region %d YS tem.nresult : %f>%f \n", i, tem.area_mm2, m_CheckBaseConfig->ysSegParam.Area); + printf("region %d YS tem.nresult : %f>%f \n", i, tem.area_mm2, cur_ys.area); } else { - printf("region %d OK tem.nresult : %f < %f \n", i, tem.area_mm2, m_CheckBaseConfig->qxSegParam.Area); + printf("region %d OK tem.nresult : %f < %f \n", i, tem.area_mm2, cur_check.area); } } } diff --git a/TOP_Det/src/Top_Detect.cpp b/TOP_Det/src/Top_Detect.cpp index 928b01a..51c1b63 100644 --- a/TOP_Det/src/Top_Detect.cpp +++ b/TOP_Det/src/Top_Detect.cpp @@ -1,6 +1,7 @@ #include "Top_Detect.hpp" #include "CheckErrorCode.hpp" #include "CheckUtil.hpp" +#include "DetCommonDefine.hpp" using namespace cv; using namespace std; // 检测模型 单个小图 输入模型图片尺寸 @@ -85,6 +86,8 @@ int Top_Detect::CheckRun() t1 = CheckUtil::getcurTime(); CheckImgInit(); cv::Mat detimg = DetImgInfo_shareP->img; + //准备结果图 + cv::resize(detimg.clone(), m_CheckResult_shareP->resultImg, cv::Size(800, 600)); if (detimg.empty()) { return 1; @@ -298,13 +301,30 @@ int Top_Detect::Draw(const cv::Mat &img, cv::Mat &resultimg) { resultimg = img.clone(); } + // //裁剪到包括产品(1600x1200) + // int w = 1600; + // int h = 1200; + // cv::Point center{this->m_AlignMaxRoi.x + this->m_AlignMaxRoi.width / 2, this->m_AlignMaxRoi.y + this->m_AlignMaxRoi.height / 2}; + + // int x = center.x - w / 2; + // int y = center.y - h / 2; + + // // 确保裁剪区域不超出图像边界 + // x = std::max(0, x); + // y = std::max(0, y); + // x = std::min(x, img.cols - w); + // y = std::min(y, img.rows - h); + + // // 创建裁剪区域 + // cv::Rect cropRoi(x, y, w, h); + // resultimg = resultimg(cropRoi); // cv::polylines(resultimg, m_CheckBaseConfig->pointArry, true, cv::Scalar(0, 255, 0), 2); - auto regions = m_CheckBaseConfig->nodeConfigArr[0].regionConfigArr; - for (int i = 0; i < regions.size(); i++) - { - cv::polylines(resultimg, regions[i].basicInfo.pointArry, true, cv::Scalar(0, 255, 0), 2); - } + // auto regions = m_CheckBaseConfig->nodeConfigArr[0].regionConfigArr; + // for (int i = 0; i < regions.size(); i++) + // { + // cv::polylines(resultimg, regions[i].basicInfo.pointArry, true, cv::Scalar(0, 255, 0), 2); + // } int fontFace = cv::FONT_HERSHEY_SIMPLEX; // 字体样式 double fontScale = 1; // 字体大小 @@ -351,9 +371,9 @@ int Top_Detect::Draw(const cv::Mat &img, cv::Mat &resultimg) p.y = m_resultList.at(i).roi.y; cv::putText(resultimg, show_info, p, fontFace, 1, color, 1); } - //裁剪 - cv::Size sz{RESULT_WIDTH, resultimg.rows}; - cv::resize(resultimg, resultimg, sz); + // 裁剪 + cv::Size dstSize(RESULT_WIDTH, RESULT_HEIGHT); + cv::resize(resultimg, resultimg, dstSize); return 0; } @@ -466,6 +486,20 @@ int Top_Detect::ImgAlinRotate(const cv::Mat &img, cv::Mat &alignImg) printf("Align is Error exit \n"); return 1; } + + //判断尺寸 + int w = m_CheckBaseConfig->baseCheckFunction.detconfig.chip_width_mm / m_CheckBaseConfig->imageScaleParam.fScale_X; + int h = m_CheckBaseConfig->baseCheckFunction.detconfig.chip_height_mm / m_CheckBaseConfig->imageScaleParam.fScale_Y; + auto w_offset = std::abs(w - m_AlignMaxRoi.width); + auto h_offset = std::abs(h - m_AlignMaxRoi.height); + int param_w_offset = m_CheckBaseConfig->baseCheckFunction.detconfig.chip_width_offset_mm / m_CheckBaseConfig->imageScaleParam.fScale_X; + int param_h_offset = m_CheckBaseConfig->baseCheckFunction.detconfig.chip_height_offset_mm / m_CheckBaseConfig->imageScaleParam.fScale_Y; + if (w_offset > param_w_offset || h_offset > param_h_offset) + { + printf("Align error, no product\n"); + return 1; + } + DetRotateType ratio = m_CheckBaseConfig->baseCheckFunction.detconfig.rotate; std::cout << "rotate ratio : " << ratio << std::endl; if (ratio == Ratio_0) @@ -508,6 +542,7 @@ int Top_Detect::DetectImg(const cv::Mat &img) { showimg = AlignImg.clone(); } + printf("start --- seg \n"); cv::Mat detSrcMask = cv::Mat(AlignImg.rows, AlignImg.cols, CV_8U, cv::Scalar(0)); ROI2ROI_SCALE SegImgToSrcImg; @@ -660,7 +695,7 @@ int Top_Detect::DetectImg(const cv::Mat &img) if (tem.area_mm2 > cur_check.area && tem.hj > cur_check.hj && cur_check.bEnable) { tem.nresult = 1; - printf("region %d NG tem.nresult : %f>%f \n", i, tem.area_mm2, m_CheckBaseConfig->qxSegParam.Area); + printf("region %d NG tem.nresult : %f>%f \n", i, tem.area_mm2, cur_check.area); } else { @@ -668,11 +703,11 @@ int Top_Detect::DetectImg(const cv::Mat &img) if (tem.area_mm2 > cur_ys.area && tem.hj > cur_ys.hj && cur_ys.bEnable) { tem.nYsresult = 1; - printf("region %d YS tem.nresult : %f>%f \n", i, tem.area_mm2, m_CheckBaseConfig->ysSegParam.Area); + printf("region %d YS tem.nresult : %f>%f \n", i, tem.area_mm2, cur_ys.area); } else { - printf("region %d OK tem.nresult : %f < %f \n", i, tem.area_mm2, m_CheckBaseConfig->qxSegParam.Area); + printf("region %d OK tem.nresult : %f < %f \n", i, tem.area_mm2, cur_check.area); } } }