Files
handleaiuinew/HanddleAI/HandleAiUi-new/algo/CISCO_APEX.hpp
T
2024-08-09 01:43:16 +08:00

221 lines
8.1 KiB
C++

#include <detData.h>
#include <handleAiModel.h>
#include <Inspenction_apex.hpp>
#include <simpleAlgo.h>
#include <CISCO_struct.hpp>
cv::Size apexMinPin(30, 30), apexMaxPin(400, 400);
double pixACC = 0.01;
struct startAndEndPoints
{
cv::Point start;
cv::Point end;
};
bool compareByStartX(const startAndEndPoints& a, const startAndEndPoints& b) {
return a.start.x < b.start.x;
}
static void apexHalconPaint(cv::Mat& out, std::vector<startAndEndPoints>xLines, const std::vector<cv::Rect>& AllPinsPos, const std::vector<rectAndResult>& AllPos,double &maxRange)
{
std::vector<std::vector<rectAndResult>> groupedRectangles = groupRectanglesByX(AllPos, 40);//编组
for (auto& vec : groupedRectangles)//排序
sortRectanglesByY(vec);
std::vector<std::vector<cv::Rect>> PinGroupedRectangles = groupRectanglesByX(AllPinsPos, 40);//编组
for (auto& vec : PinGroupedRectangles)//排序
sortRectanglesByY(vec);
std::sort(xLines.begin(), xLines.end(), compareByStartX);
for (auto& linex : xLines)//画竖线
{
cv::line(out,
cv::Point(linex.start.x, linex.start.y),
cv::Point(linex.end.x, linex.end.y),
cv::Scalar(0, 255, 0), 3);
}
for (int i = 0; i < xLines.size(); i++)
{
if (groupedRectangles.size() > i)
for (auto& rectIn : groupedRectangles[i])
{
cv::Point mid(rectIn.rect.x, rectIn.rect.y);
double range = pointToLineDistance(mid,xLines[i].start, xLines[i].end)* pixACC;
maxRange = max(maxRange, range);
cv::Rect rect(rectIn.rect.x - rectIn.rect.width/2, rectIn.rect.y- rectIn.rect.height/2, rectIn.rect.width, rectIn.rect.height);
cv::Scalar scolor;
if(rectIn.isOK == false)
scolor = cv::Scalar(0, 0, 255);
else
scolor = cv::Scalar(0, 255, 0);
cv::rectangle(out, rect, scolor, 5); //画pin针
//cv::putText(out,
// std::to_string(range),
// cv::Point(mid.x, mid.y - 5), 4, 1,
// scolor);
}
}
for (int i = 0; i < xLines.size(); i++)
{
if (PinGroupedRectangles.size() > i)
for (auto& rectIn : PinGroupedRectangles[i])
{
cv::Point mid(rectIn.x, rectIn.y);
double range = pointToLineDistance(mid, xLines[i].start, xLines[i].end) * pixACC;
maxRange = max(maxRange, range);
cv::Rect rect(rectIn.x - rectIn.width / 2, rectIn.y - rectIn.height / 2, rectIn.width, rectIn.height);
cv::Scalar scolor = cv::Scalar(0, 255, 0);
cv::rectangle(out, rect, scolor, 3); //画pin针
cv::putText(out,
std::to_string(range),
cv::Point(mid.x, mid.y - 5), 4, 1,
scolor);
}
}
}
//输入det切出的连接器图片
static cv::Mat apexHalconAlgo(std::shared_ptr<detData>data, cv::Mat& src)
{
cv::Mat halconImg;
src(cv::Rect(50, 50, src.cols - 100, src.rows - 150)).copyTo(halconImg);
HTuple hv_out_OK_Pings;
HTuple hv_out_NG_Pings;
HTuple hv_out_Flag_INS;
HTuple hv_out_Line;
HTuple hv_pin_number;
HTuple hv_out_Pings;
HObject ho_in_Image = MatToHObject(halconImg);
cv::Mat r = HObject2Mat(ho_in_Image);
sk_APEX_Pin(ho_in_Image, &hv_out_OK_Pings, &hv_out_NG_Pings,
&hv_out_Line , &hv_pin_number , &hv_out_Pings ,&hv_out_Flag_INS);
int flag = hv_out_Flag_INS.D();
if (halconImg.channels() == 1)
cv::cvtColor(halconImg, halconImg, cv::COLOR_GRAY2BGR);
int hv_out_OK_PingsSize = hv_out_OK_Pings.Length(),
hv_out_NG_PingsSize = hv_out_NG_Pings.Length();
int lineSize = hv_out_Line.Length();
int hv_out_PingsSize = hv_out_Pings.Length();
int hv_pin_numberSize = hv_pin_number.Length();
std::cout << "OKSize" << hv_out_OK_PingsSize << "NGSize" << hv_out_NG_PingsSize << std::endl;
std::vector<rectAndResult> AllBaseRects;
std::vector<cv::Rect> AllPoints;
std::vector<cv::Rect>okPos;
for (int i = 0; i < hv_out_OK_PingsSize / 4; i++)
{
cv::Rect rect(
hv_out_OK_Pings[i * 4].D(),
hv_out_OK_Pings[i * 4 + 1].D(),
hv_out_OK_Pings[i * 4 + 2].D(),
hv_out_OK_Pings[i * 4 + 3].D());
okPos.push_back(rect);
AllBaseRects.push_back(rectAndResult(rect,true));
}
std::vector<cv::Rect>ngPos;
for (int i = 0; i < hv_out_NG_PingsSize / 4; i++)
{
cv::Rect rect(
hv_out_NG_Pings[i * 4].D(),
hv_out_NG_Pings[i * 4 + 1].D(),
hv_out_NG_Pings[i * 4 + 2].D(),
hv_out_NG_Pings[i * 4 + 3].D());
ngPos.push_back(rect);
AllBaseRects.push_back(rectAndResult(rect,false));
}
for (int i = 0; i < hv_out_PingsSize / 4; i++)
{
cv::Rect rect(
hv_out_Pings[i * 4].D(),
hv_out_Pings[i * 4 + 1].D(),
hv_out_Pings[i * 4 + 2].D(),
hv_out_Pings[i * 4 + 3].D());
//okPos.push_back(rect);
AllPoints.push_back(rect);
}
std::vector<startAndEndPoints>xLines;
for (int i = 0; i < lineSize / 4; i++)
{
startAndEndPoints p;
p.start.x = hv_out_Line[i * 4 + 1].D();
p.start.y = hv_out_Line[i * 4 ].D();
p.end.x = hv_out_Line[i * 4 + 3].D();
p.end.y = hv_out_Line[i * 4 + 2].D();
xLines.push_back(p);
}
//画线
double maxRange = 0;
apexHalconPaint(halconImg, xLines, AllPoints, AllBaseRects, maxRange);
data->maxPinDetValue = maxRange;
return halconImg;
}
//输入det切出的连接器图片
static cv::Mat apexHandleAiAlgo(std::shared_ptr<detData>data, cv::Mat& src, handleAiModelManager* _handleAiModelManager)
{
handleAiModelSeg* segPtr = _handleAiModelManager->getSegModel("APEX-Seg.BHD");
handleAiModelCls* clsPtr = _handleAiModelManager->getClsModel("APEX-Cls.BHD");
cv::Mat img;
src(cv::Rect(50, 50, src.cols - 100, src.rows - 150)).copyTo(img);
//结果图
cv::Mat Paint;
if (img.channels() == 3)
img.copyTo(Paint);
else
cvtColor(img, Paint, cv::COLOR_GRAY2BGR);
//处理
auto start = std::chrono::high_resolution_clock::now();
cv::Mat segRes = ~segPtr->Evaluate(img);
std::vector<std::vector<cv::Point>> contours;
cv::findContours(segRes, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE);
int cnt = 0;
if (contours.size())//如果有缺陷
data->segOriginImgs.push_back(img);
bool finalOK = true;
for (auto contour : contours)
{
cv::Rect originRect = cv::boundingRect(contour);
if (originRect.width < apexMinPin.width || originRect.height < apexMinPin.height || originRect.width > apexMaxPin.width || originRect.height > apexMaxPin.height)
continue;
cv::Rect rect = originRect;
cv::Mat clsImg = cutImg2Cls(rect, img);
int clsRes = clsPtr->Evaluate(clsImg);
cv::Scalar color = cv::Scalar(0, 0, 255, 30);
std::string clsFileName;
switch (clsRes)
{
case 0:
color = cv::Scalar(0, 255, 0, 30);
clsFileName = "OK";
break;
case 1:
color = cv::Scalar(0, 0, 255, 30);
clsFileName = "NG1";
finalOK = false;
break;
default:
clsFileName = std::to_string(clsRes);
finalOK = false;
}
data->clsOriginImgs.push_back(std::make_pair(clsFileName, clsImg));
cv::rectangle(Paint, originRect, color, 2);
}
if (finalOK == false)
{
data->halconOK = false;
data->aiDetOK = false;
data->aiNgCount++;
}
//返回结果图
return Paint;
}
static cv::Mat apexBackHalconAlgo(std::shared_ptr<detData>data, cv::Mat& src)
{
cv::Mat dst;
cv::rotate(src, dst, cv::ROTATE_90_COUNTERCLOCKWISE);
return apexHalconAlgo(data, dst);
}
static cv::Mat apexBackHandleAiAlgo(std::shared_ptr<detData>data, cv::Mat& src, handleAiModelManager* _handleAiModelManager)
{
cv::Mat dst;
cv::rotate(src, dst, cv::ROTATE_90_COUNTERCLOCKWISE);
return apexHandleAiAlgo(data, dst, _handleAiModelManager);
}
#pragma once