Files
handleaiuinew/HanddleAI/HandleAiUi-new/algo/CISCO_APEXSMALL.hpp
T
2024-08-13 18:07:24 +08:00

154 lines
6.0 KiB
C++

#include <detData.h>
#include <handleAiModel.h>
#include <Inspection_apexSmall.hpp>
#include <simpleAlgo.h>
//输入det切出的连接器图片
static void apexSmallHalconPaint(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);
}
}
}
static cv::Mat apexSmallHalconAlgo(std::shared_ptr<detData>data, cv::Mat& src)
{
cv::Mat halconImg;
src.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);
sk2_APEXSMALL_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;
apexSmallHalconPaint(halconImg, xLines, AllPoints, AllBaseRects, maxRange);
data->maxPinDetValue = maxRange;
return halconImg;
}
//输入det切出的连接器图片
static cv::Mat apexSmallHandleAiAlgo(std::shared_ptr<detData>data, cv::Mat& src, handleAiModelManager* _handleAiModelManager)
{
data->segOriginImgs.push_back(src);
return src;
}
static cv::Mat apexSmallBackHalconAlgo(std::shared_ptr<detData>data, cv::Mat& src)
{
cv::Mat dst;
cv::rotate(src, dst, cv::ROTATE_90_COUNTERCLOCKWISE);
return apexSmallHalconAlgo(data, dst);
}
static cv::Mat apexSmalllBackHandleAiAlgo(std::shared_ptr<detData>data, cv::Mat& src, handleAiModelManager* _handleAiModelManager)
{
cv::Mat dst;
cv::rotate(src, dst, cv::ROTATE_90_COUNTERCLOCKWISE);
return apexSmallHandleAiAlgo(data, dst, _handleAiModelManager);
}