|
|
|
|
@ -51,143 +51,149 @@ CAlgorithmFluorescence::~CAlgorithmFluorescence(void)
|
|
|
|
|
//检测算法入口函数 由corctl框架回调
|
|
|
|
|
int CAlgorithmFluorescence::IImageAnalysis(class IImageObject* pImgObj, TP_ALGORITHM_OPTION* pOpt, class IDetectorEngine* pDE)
|
|
|
|
|
{
|
|
|
|
|
qDebug() << "start alg";
|
|
|
|
|
QMutexLocker locker(&mutex);
|
|
|
|
|
CircleParam cParam;
|
|
|
|
|
double dStart = cv::getTickCount();
|
|
|
|
|
Mat matSrc = getImage(pImgObj).clone();//获取相机原始图像
|
|
|
|
|
QVariantMap vMap = pImgObj->IVarFromUI().toMap();//获取由UI传递过来的算法参数
|
|
|
|
|
try {
|
|
|
|
|
qDebug() << "start alg";
|
|
|
|
|
QMutexLocker locker(&mutex);
|
|
|
|
|
CircleParam cParam;
|
|
|
|
|
double dStart = cv::getTickCount();
|
|
|
|
|
Mat matSrc = getImage(pImgObj).clone();//获取相机原始图像
|
|
|
|
|
QVariantMap vMap = pImgObj->IVarFromUI().toMap();//获取由UI传递过来的算法参数
|
|
|
|
|
|
|
|
|
|
//解析出模板库指针 这里获取的是整个模块库
|
|
|
|
|
long long modelMapPtr = vMap.value("modelMap").toLongLong();
|
|
|
|
|
QMap<QString, IWheelModel*> *ptr = (QMap<QString, IWheelModel*>*)modelMapPtr;
|
|
|
|
|
|
|
|
|
|
//获取需要检测的列表 型号名
|
|
|
|
|
long long defectListPtr = vMap.value("defectList").toLongLong();
|
|
|
|
|
QStringList* strModelListptr = (QStringList*)defectListPtr;
|
|
|
|
|
QStringList strModelList;
|
|
|
|
|
if(strModelListptr)
|
|
|
|
|
strModelList = *(strModelListptr);
|
|
|
|
|
int IsCutedImg = vMap.value("IsCutImg", 0).toInt();//裁剪后的轮毂图 检测模式 小图
|
|
|
|
|
|
|
|
|
|
int th = vMap.value("thickness", -1).toInt();//轮毂厚度 轮毂高度数据
|
|
|
|
|
bool useThickness = vMap.value("useThickness", 0).toBool();//是否使用厚度检测判断 匹配时过滤不符合范围的模板
|
|
|
|
|
bool useDiameter = vMap.value("useDiameter", 0).toBool();//是否使用直径参数检测判断
|
|
|
|
|
double dD2H = vMap.value("d2h", -1).toDouble();
|
|
|
|
|
|
|
|
|
|
int nthreshold = vMap.value("Threshold", 15).toInt();//图像阈值参数 使用背景图抠图算法时使用
|
|
|
|
|
bool bUseBackground = vMap.value("useBackground",false).toBool();//true 使用背景图抠图 false 不使用背景
|
|
|
|
|
//解析出模板库指针 这里获取的是整个模块库
|
|
|
|
|
long long modelMapPtr = vMap.value("modelMap").toLongLong();
|
|
|
|
|
QMap<QString, IWheelModel*> *ptr = (QMap<QString, IWheelModel*>*)modelMapPtr;
|
|
|
|
|
|
|
|
|
|
//获取需要检测的列表 型号名
|
|
|
|
|
long long defectListPtr = vMap.value("defectList").toLongLong();
|
|
|
|
|
QStringList* strModelListptr = (QStringList*)defectListPtr;
|
|
|
|
|
QStringList strModelList;
|
|
|
|
|
if(strModelListptr)
|
|
|
|
|
strModelList = *(strModelListptr);
|
|
|
|
|
int IsCutedImg = vMap.value("IsCutImg", 0).toInt();//裁剪后的轮毂图 检测模式 小图
|
|
|
|
|
|
|
|
|
|
int th = vMap.value("thickness", -1).toInt();//轮毂厚度 轮毂高度数据
|
|
|
|
|
bool useThickness = vMap.value("useThickness", 0).toBool();//是否使用厚度检测判断 匹配时过滤不符合范围的模板
|
|
|
|
|
bool useDiameter = vMap.value("useDiameter", 0).toBool();//是否使用直径参数检测判断
|
|
|
|
|
double dD2H = vMap.value("d2h", -1).toDouble();
|
|
|
|
|
|
|
|
|
|
int nthreshold = vMap.value("Threshold", 15).toInt();//图像阈值参数 使用背景图抠图算法时使用
|
|
|
|
|
bool bUseBackground = vMap.value("useBackground",false).toBool();//true 使用背景图抠图 false 不使用背景
|
|
|
|
|
|
|
|
|
|
int ratioType = vMap.value("RatioType").toInt();//偏距检测模式 启用方式
|
|
|
|
|
double ratioVal = vMap.value("Ratio").toDouble();//偏距系数
|
|
|
|
|
bool bEqual = vMap.value("bEqual").toBool();//使用使用图像增强
|
|
|
|
|
int filterSize = vMap.value("filterSize").toInt();//过滤圆大小
|
|
|
|
|
cParam.CirclePolarity = vMap.value("Circle_Polarity",0).toInt();
|
|
|
|
|
cParam.CircleACThres = vMap.value("Circle_ACThres",3).toInt();
|
|
|
|
|
cParam.CircleEdgeWidth = vMap.value("Circle_EdgeWidth",3).toInt();
|
|
|
|
|
cParam.filterSize = filterSize;
|
|
|
|
|
|
|
|
|
|
if (nthreshold <= 0)
|
|
|
|
|
nthreshold = 15;
|
|
|
|
|
|
|
|
|
|
QVariantMap rltMap;//算法检测结果值 输出到UI用
|
|
|
|
|
rltMap.insert("ratioVal", ratioVal);
|
|
|
|
|
luffy_base::luffyCircle lCircle;
|
|
|
|
|
static bool bReload = false;//背景图加载判断
|
|
|
|
|
Mat matBack = getBackGroundImage(pImgObj, bReload);//获取背景图
|
|
|
|
|
int ratioType = vMap.value("RatioType").toInt();//偏距检测模式 启用方式
|
|
|
|
|
double ratioVal = vMap.value("Ratio").toDouble();//偏距系数
|
|
|
|
|
bool bEqual = vMap.value("bEqual").toBool();//使用使用图像增强
|
|
|
|
|
int filterSize = vMap.value("filterSize").toInt();//过滤圆大小
|
|
|
|
|
cParam.CirclePolarity = vMap.value("Circle_Polarity",0).toInt();
|
|
|
|
|
cParam.CircleACThres = vMap.value("Circle_ACThres",3).toInt();
|
|
|
|
|
cParam.CircleEdgeWidth = vMap.value("Circle_EdgeWidth",3).toInt();
|
|
|
|
|
cParam.filterSize = filterSize;
|
|
|
|
|
|
|
|
|
|
if (nthreshold <= 0)
|
|
|
|
|
nthreshold = 15;
|
|
|
|
|
|
|
|
|
|
QVariantMap rltMap;//算法检测结果值 输出到UI用
|
|
|
|
|
rltMap.insert("ratioVal", ratioVal);
|
|
|
|
|
luffy_base::luffyCircle lCircle;
|
|
|
|
|
static bool bReload = false;//背景图加载判断
|
|
|
|
|
Mat matBack = getBackGroundImage(pImgObj, bReload);//获取背景图
|
|
|
|
|
|
|
|
|
|
Mat matMatch;//抠图后的图像 目标图像
|
|
|
|
|
if (IsCutedImg == 0) //使用的是原始图像 需要执行圆查找算法找出 轮毂
|
|
|
|
|
{
|
|
|
|
|
//原始图像的size和背景图size不匹配, 直接返回错误提示
|
|
|
|
|
if (bUseBackground == true && (matSrc.size().height != matBack.size().height || matSrc.size().width != matBack.size().width))
|
|
|
|
|
Mat matMatch;//抠图后的图像 目标图像
|
|
|
|
|
if (IsCutedImg == 0) //使用的是原始图像 需要执行圆查找算法找出 轮毂
|
|
|
|
|
{
|
|
|
|
|
rltMap.insert("error", 0);
|
|
|
|
|
pImgObj->IVariantMapToUI(rltMap);
|
|
|
|
|
bReload = true;
|
|
|
|
|
return 0;
|
|
|
|
|
//原始图像的size和背景图size不匹配, 直接返回错误提示
|
|
|
|
|
if (bUseBackground == true && (matSrc.size().height != matBack.size().height || matSrc.size().width != matBack.size().width))
|
|
|
|
|
{
|
|
|
|
|
rltMap.insert("error", 0);
|
|
|
|
|
pImgObj->IVariantMapToUI(rltMap);
|
|
|
|
|
bReload = true;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
bReload = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bUseBackground == true)//使用背景图 做减法找圆
|
|
|
|
|
{
|
|
|
|
|
Point2f centerPoint;
|
|
|
|
|
double radius = 0;
|
|
|
|
|
matMatch = ImageProcess::findCircleByBackground(matSrc, matBack, centerPoint, radius, bEqual, filterSize, cParam);
|
|
|
|
|
lCircle.ptCenter = centerPoint;
|
|
|
|
|
lCircle.fRadius = radius;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
//不需要 背景图找圆算法
|
|
|
|
|
Point2f centerPoint;
|
|
|
|
|
double radius = 0;
|
|
|
|
|
matMatch = ImageProcess::findCircle(matSrc, centerPoint, radius, bEqual,filterSize,cParam);
|
|
|
|
|
lCircle.ptCenter = centerPoint;
|
|
|
|
|
lCircle.fRadius = radius;
|
|
|
|
|
}
|
|
|
|
|
if (matMatch.cols >= 900 || matMatch.rows >= 900)//控制检测图像大小不能超过这个范围
|
|
|
|
|
{
|
|
|
|
|
cv::resize(matMatch, matMatch, cv::Size(matMatch.cols / 2, matMatch.rows / 2));
|
|
|
|
|
lCircle.fRadius = lCircle.fRadius / 2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
bReload = false;
|
|
|
|
|
matMatch = matSrc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bUseBackground == true)//使用背景图 做减法找圆
|
|
|
|
|
Result2Ui *pResult = new Result2Ui;
|
|
|
|
|
pResult->m_pixSrc = QtCVUtils::cvMatToQPixmap(matSrc);//!>原图像发送值UI 用于保存备份和调试
|
|
|
|
|
if (matMatch.empty())//抠图为空,表示抠图失败,直接返回错误
|
|
|
|
|
{
|
|
|
|
|
Point2f centerPoint;
|
|
|
|
|
double radius = 0;
|
|
|
|
|
matMatch = ImageProcess::findCircleByBackground(matSrc, matBack, centerPoint, radius, bEqual, filterSize, cParam);
|
|
|
|
|
lCircle.ptCenter = centerPoint;
|
|
|
|
|
lCircle.fRadius = radius;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
//不需要 背景图找圆算法
|
|
|
|
|
Point2f centerPoint;
|
|
|
|
|
double radius = 0;
|
|
|
|
|
matMatch = ImageProcess::findCircle(matSrc, centerPoint, radius, bEqual,filterSize,cParam);
|
|
|
|
|
lCircle.ptCenter = centerPoint;
|
|
|
|
|
lCircle.fRadius = radius;
|
|
|
|
|
rltMap.insert("noCircle", 0);
|
|
|
|
|
long long varRlt = (long long)pResult;
|
|
|
|
|
rltMap.insert("result", varRlt);
|
|
|
|
|
pImgObj->IVariantMapToUI(rltMap);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (matMatch.cols >= 900 || matMatch.rows >= 900)//控制检测图像大小不能超过这个范围
|
|
|
|
|
{
|
|
|
|
|
cv::resize(matMatch, matMatch, cv::Size(matMatch.cols / 2, matMatch.rows / 2));
|
|
|
|
|
lCircle.fRadius = lCircle.fRadius / 2;
|
|
|
|
|
|
|
|
|
|
double dDiameter = dD2H * lCircle.fRadius * 2;//计算轮毂直径
|
|
|
|
|
//打包相关数据
|
|
|
|
|
CLocalWheel wheelLocal;
|
|
|
|
|
wheelLocal.defectList = strModelList;
|
|
|
|
|
wheelLocal.img = matMatch.clone();
|
|
|
|
|
wheelLocal.height = th;
|
|
|
|
|
wheelLocal.diameter = dDiameter;
|
|
|
|
|
wheelLocal.useHeight = useThickness;
|
|
|
|
|
wheelLocal.useDiameter = useDiameter;
|
|
|
|
|
wheelLocal.ratioType = ratioType;
|
|
|
|
|
wheelLocal.dRatioVal = ratioVal;
|
|
|
|
|
//开始匹配
|
|
|
|
|
if (!matMatch.empty() && ptr && ptr->size() > 0) {
|
|
|
|
|
vector<double> minDisVec(ptr->size());//初始化模板匹配分数值
|
|
|
|
|
QString str = bestMatch(ptr, &wheelLocal, &(minDisVec[0]), minDisVec.size());
|
|
|
|
|
pResult->m_strModel = str;
|
|
|
|
|
pResult->m_dMinDis = minDisVec[0];
|
|
|
|
|
if (ptr->contains(str) && ptr->value(str)->getImageComModel()) {
|
|
|
|
|
ICompareModel *pModel = ptr->value(str)->getImageComModel();
|
|
|
|
|
double d = pModel->getDisThre();
|
|
|
|
|
double dValue = (d - minDisVec[0]) / d * 0.4 + 0.6;
|
|
|
|
|
pResult->m_dScore = dValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
matMatch = matSrc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Result2Ui *pResult = new Result2Ui;
|
|
|
|
|
pResult->m_pixSrc = QtCVUtils::cvMatToQPixmap(matSrc);//!>原图像发送值UI 用于保存备份和调试
|
|
|
|
|
if (matMatch.empty())//抠图为空,表示抠图失败,直接返回错误
|
|
|
|
|
{
|
|
|
|
|
rltMap.insert("noCircle", 0);
|
|
|
|
|
qDebug() << "pull result";
|
|
|
|
|
QImage img = QtCVUtils::cvMatToQImage(matMatch);
|
|
|
|
|
pResult->m_pixResult = QtCVUtils::cvMatToQPixmap(matMatch);// .scaled(125, 125);
|
|
|
|
|
pResult->m_dDiameter = dDiameter;
|
|
|
|
|
pResult->m_dThickness = th;
|
|
|
|
|
|
|
|
|
|
//!>传递检测结果到UI
|
|
|
|
|
//pImgObj->IDrawImage(img.scaled(300, 300));//!>显示结果图片到UI的第二个窗口上
|
|
|
|
|
double dTime = (cv::getTickCount() - dStart) / cv::getTickFrequency();
|
|
|
|
|
pResult->m_dRunTime = dTime;
|
|
|
|
|
long long varRlt = (long long)pResult;
|
|
|
|
|
rltMap.insert("result", varRlt);
|
|
|
|
|
pImgObj->IVariantMapToUI(rltMap);
|
|
|
|
|
return 0;
|
|
|
|
|
qDebug() << "finish alg";
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double dDiameter = dD2H * lCircle.fRadius * 2;//计算轮毂直径
|
|
|
|
|
//打包相关数据
|
|
|
|
|
CLocalWheel wheelLocal;
|
|
|
|
|
wheelLocal.defectList = strModelList;
|
|
|
|
|
wheelLocal.img = matMatch.clone();
|
|
|
|
|
wheelLocal.height = th;
|
|
|
|
|
wheelLocal.diameter = dDiameter;
|
|
|
|
|
wheelLocal.useHeight = useThickness;
|
|
|
|
|
wheelLocal.useDiameter = useDiameter;
|
|
|
|
|
wheelLocal.ratioType = ratioType;
|
|
|
|
|
wheelLocal.dRatioVal = ratioVal;
|
|
|
|
|
//开始匹配
|
|
|
|
|
if (!matMatch.empty() && ptr && ptr->size() > 0) {
|
|
|
|
|
vector<double> minDisVec(ptr->size());//初始化模板匹配分数值
|
|
|
|
|
QString str = bestMatch(ptr, &wheelLocal, &(minDisVec[0]), minDisVec.size());
|
|
|
|
|
pResult->m_strModel = str;
|
|
|
|
|
pResult->m_dMinDis = minDisVec[0];
|
|
|
|
|
if (ptr->contains(str) && ptr->value(str)->getImageComModel()) {
|
|
|
|
|
ICompareModel *pModel = ptr->value(str)->getImageComModel();
|
|
|
|
|
double d = pModel->getDisThre();
|
|
|
|
|
double dValue = (d - minDisVec[0]) / d * 0.4 + 0.6;
|
|
|
|
|
pResult->m_dScore = dValue;
|
|
|
|
|
}
|
|
|
|
|
catch (...) {
|
|
|
|
|
qDebug() << "algo run Error " << __FUNCTION__;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qDebug() << "pull result";
|
|
|
|
|
QImage img = QtCVUtils::cvMatToQImage(matMatch);
|
|
|
|
|
pResult->m_pixResult = QtCVUtils::cvMatToQPixmap(matMatch);// .scaled(125, 125);
|
|
|
|
|
pResult->m_dDiameter = dDiameter;
|
|
|
|
|
pResult->m_dThickness = th;
|
|
|
|
|
|
|
|
|
|
//!>传递检测结果到UI
|
|
|
|
|
//pImgObj->IDrawImage(img.scaled(300, 300));//!>显示结果图片到UI的第二个窗口上
|
|
|
|
|
double dTime = (cv::getTickCount() - dStart) / cv::getTickFrequency();
|
|
|
|
|
pResult->m_dRunTime = dTime;
|
|
|
|
|
long long varRlt = (long long)pResult;
|
|
|
|
|
rltMap.insert("result", varRlt);
|
|
|
|
|
pImgObj->IVariantMapToUI(rltMap);
|
|
|
|
|
qDebug() << "finish alg";
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//模板匹配流程
|
|
|
|
|
|