image_framework_ymj/image_framework/plugins/PlguinRoi.cpp

147 lines
3.4 KiB
C++
Raw Normal View History

2024-12-06 16:25:16 +08:00
#include "PlguinRoi.h"
#include "MsgBase.h"
#include "Libapi2d.h"
#include "Libapi2D3D.h"
#include "Log.h"
#include <opencv2/opencv.hpp>
#include <string>
static std::mutex mtx;
static int rois_count = 0;
static cv::Rect OutSquare(const std::vector<cv::Point2f>& vp)
{
int x1 = 9999;
int y1 = 9999;
int x2 = 0;
int y2 = 0;
for (int i = 0; i < vp.size(); i++)
{
if (vp[i].x < x1)
x1 = vp[i].x;
if (vp[i].x > x2)
x2 = vp[i].x;
if (vp[i].y < y1)
y1 = vp[i].y;
if (vp[i].y > y2)
y2 = vp[i].y;
}
return cv::Rect(x1, y1, x2 - x1, y2 - y1);
}
static std::vector<cv::Point2f> Offset(const std::vector<cv::Point2f>& vp, int x, int y)
{
std::vector<cv::Point2f> vpTar;
for (int i = 0; i < vp.size(); i++)
{
cv::Point2f p(vp[i].x + x, vp[i].y + y);
vpTar.push_back(p);
}
return vpTar;
}
static cv::Mat GetRoi(cv::Mat& mat, const std::vector<cv::Point2f>& vp, cv::Point2f& vpOffset)
{
//cv::Mat dst;
cv::Rect rt = OutSquare(vp);
cv::Mat dst = mat(rt);
//cv::Mat roi = cv::Mat::zeros(src.size(), CV_8U);
//std::vector<std::vector<cv::Point2f>> contour;
vpOffset = {(float) rt.x, (float)rt.y};
//contour.push_back(vpOffset);
//cv::drawContours(roi, contour, 0, cv::Scalar::all(255), -1);
//src.copyTo(dst, roi);
return dst;
}
PlguinRoi::PlguinRoi()
{
};
PlguinRoi::~PlguinRoi()
{
};
int PlguinRoi::OnProcess(LibapiThread* pThisThread, void* pMsg)
{
int ret = CAL_OK;
MsgBase* pMsgRoi = (MsgBase*)pMsg;
if (pMsgRoi->mMsgType == "roi_start")
{
pMsgRoi->mMsgType = "roi_process";
return CAL_OK;
}
if (pMsgRoi->mMsgType != "roi_process")
return CAL_OK;
int roiIndx = pMsgRoi->mRoiIndex;
CMeasureInfo* measureInfo = CMeasureInfo::Get();
std::shared_ptr<CSubRoi> subRoiPtr = measureInfo->GetSubRois()[roiIndx];
// roiMat:roi mat
// vpOffset: 在原图中的偏移量
cv::Mat roiMat = GetRoi(*measureInfo->GetIm(), subRoiPtr->mRoiPoints, subRoiPtr->vpOffset);
// 待优化
cv::imwrite(std::to_string(pMsgRoi->mRoiIndex)+"_roi_image.png", roiMat);
// 处理高精度角点
std::vector<std::vector<float>> sort_lines4;
std::vector<std::vector<float>> sort_conners4;
ret = Cal2D::LibapiGetEdegsAndConners(roiMat, sort_lines4, sort_conners4);
if (ret != CAL_OK)
{
subRoiPtr->isGood = false;
Loger(LEVEL_INFOR, "subRoiPtr->isGood = false; mId is %d : ", subRoiPtr->mId);
// 保存图片
//DoCallback(ret);
return ret;
}
for (int i = 0; i < 4; ++i)
{
std::cout << sort_conners4[i][0] << ", " << sort_conners4[i][1] << std::endl;
cv::Point2f p = { sort_conners4[i][0] + subRoiPtr->vpOffset.x,
sort_conners4[i][1] + subRoiPtr->vpOffset.y };
subRoiPtr->mInnerConners.push_back(p);
std::cout << p.x << ", " << p.y << std::endl;
}
return CAL_OK;
}
void PlguinRoi::OnEndProcess(LibapiThread* pThisThread, void* pMsg)
{
MACRO_RECORD_RUN_TIME(pThisThread->get_name());
MsgBase* pMsgRoi = (MsgBase*)pMsg;
if (pThisThread->get_name() == "_t_PluginRoiStart")
return;
if (pMsgRoi->mMsgType == "roi_process")
{
std::lock_guard<std::mutex> guard(mtx);
rois_count++;
if (rois_count == pMsgRoi->mRoiCounts)
{
// 在 PluginCameraProcessing 中会等待该信号
MsgBase::SetWaitBool(MsgBase::bAllRoisConnersCheckIsEnd);
std::cout << "MsgBase::bAllRoisConnersCheckIsEnd is ok" << std::endl;
rois_count = 0;
}
}
}