|
|
|
|
@ -451,7 +451,7 @@ Point2f GetCoorPoint(Point2f point, Mat img_mat)
|
|
|
|
|
return new_point;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int GetEdgeRoi(Mat img, Rect &new_roi, cv::RotatedRect &rotated_roi, float scale_x, float scale_y, string cur_channel){
|
|
|
|
|
int GetEdgeRoi(Mat img, Rect &new_roi, cv::RotatedRect &rotated_roi, float scale_x, float scale_y){
|
|
|
|
|
if (img.empty())
|
|
|
|
|
{
|
|
|
|
|
return 1;
|
|
|
|
|
@ -465,22 +465,9 @@ int GetEdgeRoi(Mat img, Rect &new_roi, cv::RotatedRect &rotated_roi, float scale
|
|
|
|
|
|
|
|
|
|
// 二值化找最大连通域
|
|
|
|
|
Mat r_img_bin;
|
|
|
|
|
if(cur_channel == "BCA" || cur_channel == "BTA")
|
|
|
|
|
{
|
|
|
|
|
threshold(r_img, r_img_bin, 35, 255, THRESH_BINARY);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
threshold(r_img, r_img_bin, 35, 255, THRESH_BINARY);
|
|
|
|
|
}
|
|
|
|
|
// 做一步开运算
|
|
|
|
|
Mat r_img_bin_close;
|
|
|
|
|
morphologyEx(r_img_bin, r_img_bin_close, MORPH_CLOSE, Mat::ones(15, 15, CV_8U));
|
|
|
|
|
// imwrite("r_img_bin.tif", r_img_bin);
|
|
|
|
|
// imwrite("r_img_bin_close.tif", r_img_bin_close);
|
|
|
|
|
// imwrite("r_img.tif", r_img);
|
|
|
|
|
threshold(r_img, r_img_bin, 15, 255, THRESH_BINARY);
|
|
|
|
|
std::vector<std::vector<cv::Point>> contours;
|
|
|
|
|
cv::findContours(r_img_bin_close, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
|
|
|
|
|
cv::findContours(r_img_bin, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
|
|
|
|
|
if (contours.empty())
|
|
|
|
|
{
|
|
|
|
|
return 2;
|
|
|
|
|
@ -612,7 +599,7 @@ std::vector<cv::Point2f> sort_vertices(cv::RotatedRect rrect) {
|
|
|
|
|
return vertices;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ImgCheckAnalysisy::Adapt_Config(Mat img, Rect cur_roi, bool b_update, cv::RotatedRect &rot_cur_roi){
|
|
|
|
|
int ImgCheckAnalysisy::Adapt_Config(Mat img, Rect cur_roi, bool b_update){
|
|
|
|
|
if (img.empty()) {
|
|
|
|
|
std::cerr << "Error: Input image 'img' is empty!" << std::endl;
|
|
|
|
|
return -1;
|
|
|
|
|
@ -620,8 +607,8 @@ int ImgCheckAnalysisy::Adapt_Config(Mat img, Rect cur_roi, bool b_update, cv::Ro
|
|
|
|
|
if(!b_update){
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
// cv::RotatedRect rot_cur_roi;
|
|
|
|
|
int get_edge_roi = GetEdgeRoi(img, cur_roi, rot_cur_roi, 20, 20, m_CheckResult_shareP->basicResult.strChannel);
|
|
|
|
|
cv::RotatedRect rot_cur_roi;
|
|
|
|
|
int get_edge_roi = GetEdgeRoi(img, cur_roi, rot_cur_roi, 20, 20);
|
|
|
|
|
if(get_edge_roi != 0){
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
@ -636,7 +623,6 @@ int ImgCheckAnalysisy::Adapt_Config(Mat img, Rect cur_roi, bool b_update, cv::Ro
|
|
|
|
|
m_old_cur_markLine_mark1 = m_AnalysisyConfig.baseFunction.markLine.mark_local_1;
|
|
|
|
|
m_old_cur_markLine_mark2 = m_AnalysisyConfig.baseFunction.markLine.mark_local_2;
|
|
|
|
|
m_old_cur_regionConfigArr = m_AnalysisyConfig.commonCheckConfig.nodeConfigArr[0].regionConfigArr;
|
|
|
|
|
m_old_cur_traditional_region = m_pbaseCheckFunction->traditionDet.detArea;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*计算old_roi到new_roi的完整仿射变换矩阵(平移+缩放+旋转)*/
|
|
|
|
|
@ -674,8 +660,6 @@ int ImgCheckAnalysisy::Adapt_Config(Mat img, Rect cur_roi, bool b_update, cv::Ro
|
|
|
|
|
cv::Point& cur_markLine_mark1 = m_AnalysisyConfig.baseFunction.markLine.mark_local_1;
|
|
|
|
|
cv::Point& cur_markLine_mark2 = m_AnalysisyConfig.baseFunction.markLine.mark_local_2;
|
|
|
|
|
std::vector<RegionConfigST>& cur_regionConfigArr = m_AnalysisyConfig.commonCheckConfig.nodeConfigArr[0].regionConfigArr;
|
|
|
|
|
cv::Rect& cur_traditional_rect = m_pbaseCheckFunction->traditionDet.detArea_ROI;
|
|
|
|
|
std::vector<cv::Point>& cur_traditional_region = m_pbaseCheckFunction->traditionDet.detArea;
|
|
|
|
|
|
|
|
|
|
// if (img.empty())
|
|
|
|
|
// {
|
|
|
|
|
@ -733,9 +717,7 @@ int ImgCheckAnalysisy::Adapt_Config(Mat img, Rect cur_roi, bool b_update, cv::Ro
|
|
|
|
|
cur_regionConfigArr[i].basicInfo.pointArry[j] = affinePoint(m_old_cur_regionConfigArr[i].basicInfo.pointArry[j]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(int i = 0; i < cur_traditional_region.size(); i++){
|
|
|
|
|
cur_traditional_region[i] = affinePoint(m_old_cur_traditional_region[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*show*/
|
|
|
|
|
// if (img.empty())
|
|
|
|
|
// {
|
|
|
|
|
@ -813,7 +795,7 @@ int ImgCheckAnalysisy::CheckRun()
|
|
|
|
|
/*自适应更新参数*/
|
|
|
|
|
long time_adapt_s = CheckUtil::getcurTime();
|
|
|
|
|
cv::RotatedRect rot_cur_roi;
|
|
|
|
|
Adapt_Config(m_CheckResult_shareP->in_shareImage->img, m_CutRoi, m_pbaseCheckFunction->markLine.badapt_region, rot_cur_roi);
|
|
|
|
|
Adapt_Config(m_CheckResult_shareP->in_shareImage->img, m_CutRoi, m_pbaseCheckFunction->markLine.badapt_region);
|
|
|
|
|
long time_adapt_e = CheckUtil::getcurTime();
|
|
|
|
|
m_pdetlog->AddCheckstr(PrintLevel_0, "Adapt_Config", "time = %ld", time_adapt_e - time_adapt_s);
|
|
|
|
|
|
|
|
|
|
@ -878,6 +860,7 @@ int ImgCheckAnalysisy::CheckRun()
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// 不推理:直接输出全黑残点图
|
|
|
|
|
m_pdetlog->AddCheckstr(PrintLevel_0, "2.1、pre detect", "-------------------------NO AI_Edge--------------- \n");
|
|
|
|
|
m_pImageAllResult->AIMaskImg = cv::Mat::zeros(m_pImageAllResult->detImg.size(), CV_8UC1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -2232,34 +2215,11 @@ int ImgCheckAnalysisy::Update_DetRoiList()
|
|
|
|
|
float fx = m_pImageAllResult->fscale_detToresult_x;
|
|
|
|
|
float fy = m_pImageAllResult->fscale_detToresult_y;
|
|
|
|
|
|
|
|
|
|
int num = 0;
|
|
|
|
|
cv::Rect offsetroi = m_Crop_Roi_paramImg;
|
|
|
|
|
|
|
|
|
|
if (m_pEdge_Align_Result && m_pEdge_Align_Result->buseOfft)
|
|
|
|
|
// Adapt_Config 已通过仿射变换将配置区域适配到当前产品位置,
|
|
|
|
|
// 此处仅做坐标映射(减去裁剪ROI偏移 + 缩放),不再叠加边缘对齐的二次偏移
|
|
|
|
|
for (const auto ®ion : m_pCommonAnalysisyConfig->regionConfigArr)
|
|
|
|
|
{
|
|
|
|
|
for (const auto ®ion : m_pCommonAnalysisyConfig->regionConfigArr)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (m_pEdge_Align_Result->H.empty())
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
m_DetRoiList.Update_1(region.basicInfo.pointArry, offsetroi, m_pEdge_Align_Result->offt_x, m_pEdge_Align_Result->offt_y, fx, fy);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
m_DetRoiList.Update_Marit(region.basicInfo.pointArry, offsetroi, m_pEdge_Align_Result->H, fx, fy);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// m_DetRoiList.Update_1(region.basicInfo.pointArry, offsetroi, m_pEdge_Align_Result->offt_x, m_pEdge_Align_Result->offt_y, fx, fy);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (const auto ®ion : m_pCommonAnalysisyConfig->regionConfigArr)
|
|
|
|
|
{
|
|
|
|
|
m_DetRoiList.Update(region.basicInfo.pointArry, m_Crop_Roi_paramImg, fx, fy);
|
|
|
|
|
}
|
|
|
|
|
m_DetRoiList.Update(region.basicInfo.pointArry, m_Crop_Roi_paramImg, fx, fy);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_pdetlog->AddCheckstr(PrintLevel_1, DET_LOG_LEVEL_3, "info", "roiList_Src roi num %ld", m_DetRoiList.roiList_Src.size());
|
|
|
|
|
@ -2275,19 +2235,23 @@ int ImgCheckAnalysisy::DrawResult_Step_1()
|
|
|
|
|
{
|
|
|
|
|
int n = 0;
|
|
|
|
|
// std::cout << m_DetRoiList.roiList_Show.size() << std::endl;
|
|
|
|
|
for (const auto &polygon : m_DetRoiList.roiList_Show)
|
|
|
|
|
for(int i = 0; i < m_DetRoiList.roiList_Show.size(); i++)
|
|
|
|
|
{
|
|
|
|
|
// n++;
|
|
|
|
|
// if (n == 1)
|
|
|
|
|
// {
|
|
|
|
|
// continue;
|
|
|
|
|
// }
|
|
|
|
|
cv::polylines(m_CheckResult_shareP->resultimg, m_DetRoiList.roiList_Show[i], true, cv::Scalar(32, 128, (i*64+64)%255), 1);
|
|
|
|
|
}
|
|
|
|
|
// for (const auto &polygon : m_DetRoiList.roiList_Show)
|
|
|
|
|
// {
|
|
|
|
|
// // n++;
|
|
|
|
|
// // if (n == 1)
|
|
|
|
|
// // {
|
|
|
|
|
// // continue;
|
|
|
|
|
// // }
|
|
|
|
|
|
|
|
|
|
// std::cout << polygon << std::endl;
|
|
|
|
|
// // std::cout << polygon << std::endl;
|
|
|
|
|
|
|
|
|
|
// 绘制多边形的边界(不填充),使用绿色线条,线宽为2
|
|
|
|
|
cv::polylines(m_CheckResult_shareP->resultimg, polygon, true, cv::Scalar(128, 128, 255), 1); // true表示闭合多边形
|
|
|
|
|
}
|
|
|
|
|
// // 绘制多边形的边界(不填充),使用绿色线条,线宽为2
|
|
|
|
|
// cv::polylines(m_CheckResult_shareP->resultimg, polygon, true, cv::Scalar(128, 128, 255), 1); // true表示闭合多边形
|
|
|
|
|
// }
|
|
|
|
|
// cv::imwrite(DetImgInfo_shareP->strChannel + "roi_src.png", m_pdetlog->temImgList[TEM_IMG_IDX_DrawSrc]);
|
|
|
|
|
// cv::imwrite(DetImgInfo_shareP->strChannel + "roi_ss.png", m_pdetlog->temImgList[TEM_IMG_IDX_Result]);
|
|
|
|
|
|
|
|
|
|
@ -2302,13 +2266,17 @@ int ImgCheckAnalysisy::DrawResult_Step_1()
|
|
|
|
|
{
|
|
|
|
|
for (const auto &r : m_Edge_DetConfig.edge_det_roi)
|
|
|
|
|
{
|
|
|
|
|
cv::Rect droi;
|
|
|
|
|
droi.x = r.roi.x * fs_resize_x;
|
|
|
|
|
droi.y = r.roi.y * fs_resize_y;
|
|
|
|
|
droi.width = r.roi.width * fs_resize_x;
|
|
|
|
|
droi.height = r.roi.height * fs_resize_y;
|
|
|
|
|
|
|
|
|
|
cv::rectangle(m_CheckResult_shareP->resultimg, droi, cv::Scalar(255, 0, 255), 1); // 黄色角点
|
|
|
|
|
cv::Point2f vertices[4];
|
|
|
|
|
r.rrect.points(vertices);
|
|
|
|
|
for (int j = 0; j < 4; j++)
|
|
|
|
|
{
|
|
|
|
|
cv::Point p1, p2;
|
|
|
|
|
p1.x = (int)(vertices[j].x * fs_resize_x);
|
|
|
|
|
p1.y = (int)(vertices[j].y * fs_resize_y);
|
|
|
|
|
p2.x = (int)(vertices[(j+1)%4].x * fs_resize_x);
|
|
|
|
|
p2.y = (int)(vertices[(j+1)%4].y * fs_resize_y);
|
|
|
|
|
cv::line(m_CheckResult_shareP->resultimg, p1, p2, cv::Scalar(255, 0, 255), 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_Edge_DetConfig.Det_region.size() > 2)
|
|
|
|
|
@ -2334,24 +2302,6 @@ int ImgCheckAnalysisy::DrawResult_Step_1()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (m_pbaseCheckFunction && m_pbaseCheckFunction->markLine.region.size() > 1)
|
|
|
|
|
{
|
|
|
|
|
std::vector<cv::Point> product_roi_show;
|
|
|
|
|
for (const auto &pt : m_pbaseCheckFunction->markLine.region)
|
|
|
|
|
{
|
|
|
|
|
cv::Point draw_pt;
|
|
|
|
|
draw_pt.x = (pt.x - m_Crop_Roi_paramImg.x) * fs_resize_x;
|
|
|
|
|
draw_pt.y = (pt.y - m_Crop_Roi_paramImg.y) * fs_resize_y;
|
|
|
|
|
product_roi_show.push_back(draw_pt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cv::polylines(
|
|
|
|
|
m_CheckResult_shareP->resultimg,
|
|
|
|
|
product_roi_show,
|
|
|
|
|
true,
|
|
|
|
|
cv::Scalar(0, 255, 0),
|
|
|
|
|
1);
|
|
|
|
|
}
|
|
|
|
|
if (m_pEdge_Align_Result.get() != NULL)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
@ -2434,6 +2384,7 @@ int ImgCheckAnalysisy::Edge_Qx_Det(const cv::Mat &img)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
m_Edge_DetConfig.Init();
|
|
|
|
|
m_Edge_DetConfig.detlog = m_pdetlog;
|
|
|
|
|
m_Edge_DetConfig.strChannel = m_strCurDetChannel;
|
|
|
|
|
m_Edge_DetConfig.pBaseCheckFunction = m_pbaseCheckFunction;
|
|
|
|
|
m_Edge_DetConfig.alginResult.corpRoi = m_Crop_Roi_paramImg;
|
|
|
|
|
@ -2656,11 +2607,12 @@ int ImgCheckAnalysisy::CalProductSize()
|
|
|
|
|
m_CheckResult_shareP->productWidht_mm = fw;
|
|
|
|
|
m_CheckResult_shareP->productHeight_mm = fh;
|
|
|
|
|
|
|
|
|
|
m_pdetlog->AddCheckstr(PrintLevel_1, "Det ROI", " [w %d,h %d] (piexl) -> [w %f h %f](mm),scale %f %f",
|
|
|
|
|
m_CutRoi.width, m_CutRoi.height, fw, fh, m_fImgage_Scale_X, m_fImgage_Scale_Y);
|
|
|
|
|
m_pdetlog->AddCheckstr(PrintLevel_1, "Det ROI", " [x%d, y%d, w %d,h %d] (piexl) -> [w %f h %f](mm),scale %f %f",
|
|
|
|
|
m_CutRoi.x, m_CutRoi.y, m_CutRoi.width, m_CutRoi.height, fw, fh, m_fImgage_Scale_X, m_fImgage_Scale_Y);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int ImgCheckAnalysisy::ImgPreDet()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|