|
|
|
@ -128,6 +128,7 @@ ALLImgCheckAnalysisy::ALLImgCheckAnalysisy()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
m_strTest = "";
|
|
|
|
m_strTest = "";
|
|
|
|
|
|
|
|
m_pdetlog = std::make_shared<DetLog>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ALLImgCheckAnalysisy::~ALLImgCheckAnalysisy()
|
|
|
|
ALLImgCheckAnalysisy::~ALLImgCheckAnalysisy()
|
|
|
|
@ -180,6 +181,12 @@ int ALLImgCheckAnalysisy::RunStart(void *pconfig1)
|
|
|
|
m_nErrorCode = re;
|
|
|
|
m_nErrorCode = re;
|
|
|
|
return m_nErrorCode;
|
|
|
|
return m_nErrorCode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
re = InitTemplateMask();
|
|
|
|
|
|
|
|
if (CHECK_OK != re)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
m_nErrorCode = re;
|
|
|
|
|
|
|
|
return m_nErrorCode;
|
|
|
|
|
|
|
|
}
|
|
|
|
m_nErrorCode = CHECK_OK;
|
|
|
|
m_nErrorCode = CHECK_OK;
|
|
|
|
printf(">>>> ALLImgCheckAnalysisy Start Succ \n");
|
|
|
|
printf(">>>> ALLImgCheckAnalysisy Start Succ \n");
|
|
|
|
|
|
|
|
|
|
|
|
@ -307,6 +314,143 @@ int ALLImgCheckAnalysisy::InitAIFactory()
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int ALLImgCheckAnalysisy::InitTemplateMask()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
std::shared_ptr<AIFactory> AI_Factory = AIFactory::GetInstance();
|
|
|
|
|
|
|
|
if (!AI_Factory || !AI_Factory->Align_Outer || !AI_Factory->Pin_Loc)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pdetlog->AddCheckstr(PrintLevel_1, DET_LOG_LEVEL_1, "---Init Tpl---" , "start");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 模板图路径,暂时使用固定路径
|
|
|
|
|
|
|
|
string tplImgPath = "/home/aidlux/BOE/ZB_SOCKET/Tpl/tpl.bmp"; // 模板图片路径
|
|
|
|
|
|
|
|
Mat tplImg = imread(tplImgPath, IMREAD_UNCHANGED);
|
|
|
|
|
|
|
|
if (tplImg.empty())
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
m_pdetlog->AddCheckstr(PrintLevel_1, DET_LOG_LEVEL_1, "", "Failed to load template image!");
|
|
|
|
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 模板外接矩形,通过模型Align_Outer得到
|
|
|
|
|
|
|
|
RotatedRect tplOuterRect; // 模板外接矩形
|
|
|
|
|
|
|
|
std::shared_ptr<AIModel_Base> pSocket_Align;
|
|
|
|
|
|
|
|
pSocket_Align = AI_Factory->Align_Outer;
|
|
|
|
|
|
|
|
cv::Size sz;
|
|
|
|
|
|
|
|
sz.width = pSocket_Align->input_0.width;
|
|
|
|
|
|
|
|
sz.height = pSocket_Align->input_0.height;
|
|
|
|
|
|
|
|
cv::Mat detImg;
|
|
|
|
|
|
|
|
cout << "imgSize: " << tplImg.size() << ", " << "detImgSize: " << detImg.size() << ", " << "szSize: " << sz << endl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cv::resize(tplImg, detImg, sz);
|
|
|
|
|
|
|
|
int re = 0;
|
|
|
|
|
|
|
|
cv::Mat mask;
|
|
|
|
|
|
|
|
if (detImg.channels() != 1)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
cv::cvtColor(detImg, detImg, cv::COLOR_RGB2GRAY);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
re = pSocket_Align->AIDet(detImg, mask);
|
|
|
|
|
|
|
|
if (re != 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
m_pdetlog->AddCheckstr(PrintLevel_1, DET_LOG_LEVEL_1, "Init Tpl", "AICheck_Edge----error %d ", re);
|
|
|
|
|
|
|
|
int re123 = 100 + re;
|
|
|
|
|
|
|
|
return re123;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 找到最大轮廓
|
|
|
|
|
|
|
|
bool found;
|
|
|
|
|
|
|
|
std::vector<cv::Point> boundingBox = CheckUtil::getLargestContourROI(mask, found);
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
m_pdetlog->AddCheckstr(PrintLevel_1, DET_LOG_LEVEL_1, "AI_Edge_Algin ", "No contours found!----error ");
|
|
|
|
|
|
|
|
return 4;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 边缘轮廓点集还原为原图坐标,并计算得到RotateROI
|
|
|
|
|
|
|
|
if (sz.width <= 0 || sz.height <= 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
m_pdetlog->AddCheckstr(PrintLevel_1, DET_LOG_LEVEL_1, "AI_Edge_Algin ", "invalid model input size!----error ");
|
|
|
|
|
|
|
|
return 5;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 检测小图 到 原图 的缩放比例
|
|
|
|
|
|
|
|
const float scale_x = static_cast<float>(tplImg.cols) / sz.width;
|
|
|
|
|
|
|
|
const float scale_y = static_cast<float>(tplImg.rows) / sz.height;
|
|
|
|
|
|
|
|
// 轮廓点还原到原图坐标系
|
|
|
|
|
|
|
|
std::vector<cv::Point2f> srcContour;
|
|
|
|
|
|
|
|
srcContour.reserve(boundingBox.size());
|
|
|
|
|
|
|
|
for (const auto &pt : boundingBox)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
srcContour.emplace_back(pt.x * scale_x, pt.y * scale_y);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (srcContour.size() < 3)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
m_pdetlog->AddCheckstr(PrintLevel_1, DET_LOG_LEVEL_1, "AI_Edge_Algin ", "contour points too few!----error ");
|
|
|
|
|
|
|
|
return 6;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
tplOuterRect = cv::minAreaRect(srcContour);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 模板Pinmask,通过模型Pin_Loc得到
|
|
|
|
|
|
|
|
Mat tplPinMask; // 模板mask
|
|
|
|
|
|
|
|
std::shared_ptr<AIModel_Base> pSocket_Pin;
|
|
|
|
|
|
|
|
pSocket_Pin = AI_Factory->Pin_Loc;
|
|
|
|
|
|
|
|
sz.width = pSocket_Pin->input_0.width;
|
|
|
|
|
|
|
|
sz.height = pSocket_Pin->input_0.height;
|
|
|
|
|
|
|
|
cv::Mat pinImg = tplImg(tplOuterRect.boundingRect()).clone(); // 裁剪模板图像为外接矩形区域
|
|
|
|
|
|
|
|
cout << "imgSize: " << tplImg.size() << ", " << "pinImgSize: " << pinImg.size() << ", " << "szSize: " << sz << endl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cv::resize(pinImg, detImg, sz);
|
|
|
|
|
|
|
|
if (detImg.channels() != 1)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
cv::cvtColor(detImg, detImg, cv::COLOR_RGB2GRAY);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
re = pSocket_Pin->AIDet(detImg, tplPinMask);
|
|
|
|
|
|
|
|
if (re != 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
m_pdetlog->AddCheckstr(PrintLevel_1, DET_LOG_LEVEL_1, "AI_Pin ", "AICheck_Pin----error %d ", re);
|
|
|
|
|
|
|
|
int re123 = 100 + re;
|
|
|
|
|
|
|
|
return re123;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 统计tplPinMask的blob边缘点,并绘制到tplImg上
|
|
|
|
|
|
|
|
vector<vector<Point>> pin_contours;
|
|
|
|
|
|
|
|
vector<Vec4i> hierarchy;
|
|
|
|
|
|
|
|
cv::findContours(tplPinMask, pin_contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE);
|
|
|
|
|
|
|
|
// mask 尺寸 -> 裁剪图(pinImg) 尺寸 的缩放比例
|
|
|
|
|
|
|
|
const float pin_scale_x = (tplPinMask.cols > 0) ? static_cast<float>(pinImg.cols) / tplPinMask.cols : 0.0f;
|
|
|
|
|
|
|
|
const float pin_scale_y = (tplPinMask.rows > 0) ? static_cast<float>(pinImg.rows) / tplPinMask.rows : 0.0f;
|
|
|
|
|
|
|
|
// 裁剪区域在 tplImg 上的偏移
|
|
|
|
|
|
|
|
const cv::Point pin_offset = tplOuterRect.boundingRect().tl();
|
|
|
|
|
|
|
|
// 将轮廓点从 mask 坐标系 映射回 tplImg 坐标系
|
|
|
|
|
|
|
|
m_tplPinContours.clear();
|
|
|
|
|
|
|
|
m_tplPinContours.reserve(pin_contours.size());
|
|
|
|
|
|
|
|
for (size_t i = 0; i < pin_contours.size(); i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
std::vector<cv::Point> mapped;
|
|
|
|
|
|
|
|
mapped.reserve(pin_contours[i].size());
|
|
|
|
|
|
|
|
for (const auto &pt : pin_contours[i])
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
mapped.emplace_back(cvRound(pt.x * pin_scale_x) + pin_offset.x,
|
|
|
|
|
|
|
|
cvRound(pt.y * pin_scale_y) + pin_offset.y);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
m_tplPinContours.push_back(mapped);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 保存模板外接矩形
|
|
|
|
|
|
|
|
m_tplOuterRect = tplOuterRect;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 绘制到 tplImg 上
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Mat showImg = tplImg.clone();
|
|
|
|
|
|
|
|
rectangle(showImg, tplOuterRect.boundingRect(), Scalar(0, 0, 255), 2);
|
|
|
|
|
|
|
|
cv::drawContours(showImg, m_tplPinContours, -1, Scalar(0, 255, 0), 2);
|
|
|
|
|
|
|
|
cv::imwrite("/home/aidlux/BOE/ZB_SOCKET/Tpl/tpl_pin_contours.png", showImg);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m_pdetlog->AddCheckstr(PrintLevel_1, DET_LOG_LEVEL_1, "---Init Tpl---" , "end");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ALLImgCheckAnalysisy::LoadRunConfig(void *p)
|
|
|
|
int ALLImgCheckAnalysisy::LoadRunConfig(void *p)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
|