增加try catch捕获异常

屏蔽circle算法(该算法可能存在bug)
jizhi
bob.pan 4 years ago
parent 84dfb14055
commit d534ed13ee

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

@ -127,9 +127,12 @@ Q_SLOT void QAlgParamDlg::onChangeBG()
createfile->remove(DstPath); createfile->remove(DstPath);
//} //}
} }
if (!QFile::copy(sourcePath, DstPath)) { // if (!QFile::copy(sourcePath, DstPath)) {
//return false; // //return false;
} // }
QImage img;
img.load(sourcePath);
img.save(DstPath);
//emit sgChangeBG(DstPath); //emit sgChangeBG(DstPath);
QMessageBox infobox(QMessageBox::Information, tr("提示"), tr("背景图更新完成,请重启本软件."), QMessageBox::Yes, NULL); QMessageBox infobox(QMessageBox::Information, tr("提示"), tr("背景图更新完成,请重启本软件."), QMessageBox::Yes, NULL);
infobox.setWindowIcon(QIcon(":/image/leaper")); infobox.setWindowIcon(QIcon(":/image/leaper"));

@ -23,9 +23,9 @@
#include <QProcess> #include <QProcess>
#include "lpCryptokey.h" #include "lpCryptokey.h"
#define VERSION_HUB "3.0.1.7" #define VERSION_HUB "3.0.2.0"
#define VERSION_ALG "3.0.1.5" #define VERSION_ALG "3.0.1.5"
#define UPDATE_TIME "2021-10-09" #define UPDATE_TIME "2021-11-24"
#pragma execution_character_set("utf-8") #pragma execution_character_set("utf-8")
lpMainWin::lpMainWin(QWidget *parent) lpMainWin::lpMainWin(QWidget *parent)
@ -586,7 +586,10 @@ void lpMainWin::INewCameraImage(const QString& camKey, QImage img)
/*多线程发送算法结果*/ /*多线程发送算法结果*/
void lpMainWin::IVariantMapToUI(const QString& camKey, const QVariantMap& vMap) void lpMainWin::IVariantMapToUI(const QString& camKey, const QVariantMap& vMap)
{ {
try {
m_glbalRatio = 0; m_glbalRatio = 0;
emit(sgShowImgState(tr("显示识别结果"))); emit(sgShowImgState(tr("显示识别结果")));
Result2Ui *pResult = (Result2Ui*)vMap.value("result").toLongLong(); Result2Ui *pResult = (Result2Ui*)vMap.value("result").toLongLong();
@ -708,6 +711,11 @@ void lpMainWin::IVariantMapToUI(const QString& camKey, const QVariantMap& vMap)
m_pCtrl->saveResult(pResult); m_pCtrl->saveResult(pResult);
delete pResult; delete pResult;
}
catch(...)
{
qWarning() << "dump result " << __FUNCTION__;
}
} }
/*多线程获取算法参数*/ /*多线程获取算法参数*/

Loading…
Cancel
Save