添加功能:当NG时自动调整相机曝光时间再次进行拍照检测。

jizhi
qushuai777 5 years ago
parent f5fc059c42
commit 271d50af9c

@ -246,7 +246,6 @@ QString CAlgorithmFluorescence::bestMatch(const QMap<QString, IWheelModel*>* mod
return bestName; return bestName;
} }
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)
{ {
QMutexLocker locker(&mutex); QMutexLocker locker(&mutex);
@ -281,7 +280,7 @@ int CAlgorithmFluorescence::IImageAnalysis(class IImageObject* pImgObj, TP_ALGOR
Mat matBack = getBackGroundImage(pImgObj, bReload);//获取背景图 Mat matBack = getBackGroundImage(pImgObj, bReload);//获取背景图
Mat matMatch;//装载抠图后的图像 Mat matMatch;//装载抠图后的图像
if (IsCutedImg == 0){ if (IsCutedImg == 0){
if (matSrc.size().height != matBack.size().height || matSrc.size().width != matBack.size().width) if (bUseBackground == true && (matSrc.size().height != matBack.size().height || matSrc.size().width != matBack.size().width))
{ {
rltMap.insert("error", 0); rltMap.insert("error", 0);
pImgObj->IVariantMapToUI(rltMap); pImgObj->IVariantMapToUI(rltMap);
@ -297,7 +296,12 @@ int CAlgorithmFluorescence::IImageAnalysis(class IImageObject* pImgObj, TP_ALGOR
else{ else{
matMatch = matSrc; matMatch = matSrc;
} }
if (matMatch.empty())
{
rltMap.insert("noCircle", 0);
pImgObj->IVariantMapToUI(rltMap);
return 0;
}
//Mat matMatch = ImageProcess::findCircleObject(matSrc, matBack, 15, NULL/* &lCircle*/); //Mat matMatch = ImageProcess::findCircleObject(matSrc, matBack, 15, NULL/* &lCircle*/);
//double dDiameter = dD2H * matMatch.rows; //double dDiameter = dD2H * matMatch.rows;
double dDiameter = dD2H * lCircle.fRadius * 2; double dDiameter = dD2H * lCircle.fRadius * 2;
@ -325,7 +329,11 @@ int CAlgorithmFluorescence::IImageAnalysis(class IImageObject* pImgObj, TP_ALGOR
pResult->m_dScore = dValue; pResult->m_dScore = dValue;
} }
} }
else { //else {
//}
else if (matMatch.empty())
{
} }
qDebug() << "pull result"; qDebug() << "pull result";
pResult->m_pixSrc = QtCVUtils::cvMatToQPixmap(matSrc);//!>原图像发送值UI 用于保存备份和调试 pResult->m_pixSrc = QtCVUtils::cvMatToQPixmap(matSrc);//!>原图像发送值UI 用于保存备份和调试

@ -40,7 +40,7 @@ Mat findEdge2(const Mat &Src)
filter2D(Src, ret, CV_8UC1, kernel); filter2D(Src, ret, CV_8UC1, kernel);
return ret; return ret;
} }
#define REAIZE 1 #define REAIZE 2
cv::Mat ImageProcess::findCircleObject(const Mat &src, const Mat& backgroundImg, bool useBackgroundFlag, int nThres /*= 20*/, luffy_base::luffyCircle *pCircle /*= NULL*/) cv::Mat ImageProcess::findCircleObject(const Mat &src, const Mat& backgroundImg, bool useBackgroundFlag, int nThres /*= 20*/, luffy_base::luffyCircle *pCircle /*= NULL*/)
{ {
#ifdef MOTO_DETECT//摩轮型号识别抠图算法 #ifdef MOTO_DETECT//摩轮型号识别抠图算法
@ -101,6 +101,15 @@ cv::Mat ImageProcess::findCircleObject(const Mat &src, const Mat& backgroundImg,
//float fScale = src.cols / ALG_RESIZE_IMAGE_WIDTH; //float fScale = src.cols / ALG_RESIZE_IMAGE_WIDTH;
//pCircle->fRadius = hight*1.0 / fScale; //pCircle->fRadius = hight*1.0 / fScale;
pCircle->fRadius = radius * REAIZE; pCircle->fRadius = radius * REAIZE;
//2021-05-10 增加图像大小判断 对超过900像素的图像进行再一次压缩
if (cutMat.cols >= 900 || cutMat.rows >= 900)
{
Mat newCutImg;
cv::resize(cutMat, newCutImg, cv::Size(cutMat.cols / REAIZE, cutMat.rows / REAIZE));
pCircle->fRadius = pCircle->fRadius / REAIZE;
return newCutImg;
}
return cutMat; return cutMat;
} }
} }
@ -239,6 +248,7 @@ cv::Mat ImageProcess::findCircleObject(const Mat &src, const Mat& backgroundImg,
if (src.empty() || backgroundImg.empty() || src.rows < 500) { if (src.empty() || backgroundImg.empty() || src.rows < 500) {
return Mat(); return Mat();
} }
bool findFlag = false;
/*第一阶梯找圆*/ /*第一阶梯找圆*/
assert(backgroundImg.type() == CV_8UC1); assert(backgroundImg.type() == CV_8UC1);
Mat detectImg; Mat detectImg;

@ -11,7 +11,7 @@ struct IPConfig
QString m_ClientAddress; QString m_ClientAddress;
int m_ClientPort; int m_ClientPort;
IPConfig(){ IPConfig(){
m_TcpAddress = "192.168.0.240"; m_TcpAddress = "192.168.0.110";
m_ClientAddress = "192.168.0.119"; m_ClientAddress = "192.168.0.119";
m_TcpPort = 2000; m_TcpPort = 2000;
m_ClientPort = 2000; m_ClientPort = 2000;

@ -144,7 +144,6 @@ Q_SLOT void QCamSettingDlg::onButtonClicked()
} }
else if ("m_pbOpen" == strObj) else if ("m_pbOpen" == strObj)
{ {
if (m_pCoreCtl) if (m_pCoreCtl)
{ {
if (tr("关闭") == ui.m_pbOpen->text()) if (tr("关闭") == ui.m_pbOpen->text())

@ -271,7 +271,7 @@ void QSystemSettingDlg::addPicRoot(QTreeWidget *pTreewidget, QString strName)
m_useBackground->setText(tr("使用背景图")); m_useBackground->setText(tr("使用背景图"));
m_useBackground->setObjectName("m_useBackground"); m_useBackground->setObjectName("m_useBackground");
connect(m_useBackground, SIGNAL(stateChanged(int)), this, SLOT(onCheckstateChanged(int))); connect(m_useBackground, SIGNAL(stateChanged(int)), this, SLOT(onCheckstateChanged(int)));
m_useBackground->setChecked(DetectState::instance()->saveBad == (int)true); m_useBackground->setChecked(DetectState::instance()->m_UseBackground == (int)true);
QCheckBox *m_ResAll2A = new QCheckBox; QCheckBox *m_ResAll2A = new QCheckBox;
m_ResAll2A->setText(tr("轮毂全去A通道")); m_ResAll2A->setText(tr("轮毂全去A通道"));

@ -18,9 +18,12 @@
#include "saveimgthread.h" #include "saveimgthread.h"
#include "lpGlobalData.h" #include "lpGlobalData.h"
#include "qpulpewidget.h" #include "qpulpewidget.h"
#define VERSION_HUB "3.0.0.1"
#define VERSION_ALG "3.0.0.1" #include "QZkJsonParser.h"
#define UPDATE_TIME "2021-02-20"
#define VERSION_HUB "3.0.0.2"
#define VERSION_ALG "3.0.0.2"
#define UPDATE_TIME "2021-05-08"
#pragma execution_character_set("utf-8") #pragma execution_character_set("utf-8")
lpMainWin::lpMainWin(QWidget *parent) lpMainWin::lpMainWin(QWidget *parent)
@ -251,6 +254,10 @@ lpMainWin::lpMainWin(QWidget *parent)
m_timerID = startTimer(1000); m_timerID = startTimer(1000);
//m_PulseTimer.start(1000); //m_PulseTimer.start(1000);
connect(this, SIGNAL(sgAutoExposure()), this, SLOT(onAutoExposure()));
QString strPath = QApplication::applicationDirPath();
readExposureTimeConfig(strPath);
} }
lpMainWin::~lpMainWin() lpMainWin::~lpMainWin()
@ -395,6 +402,8 @@ bool lpMainWin::onInitCoreCtrl()
FuncCallBack_StrImg strImgfunc = std::bind(&lpMainWin::INewCameraImage, this, std::placeholders::_1, std::placeholders::_2); FuncCallBack_StrImg strImgfunc = std::bind(&lpMainWin::INewCameraImage, this, std::placeholders::_1, std::placeholders::_2);
m_pCoreCtrl->IRegisterImageCallBack(strImgfunc); m_pCoreCtrl->IRegisterImageCallBack(strImgfunc);
m_camKey = m_pCoreCtrl->ICameraKeys().first();
return true; return true;
} }
@ -525,16 +534,95 @@ 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)
{ {
emit(sgShowImgState(tr("显示识别结果"))); emit(sgShowImgState(tr("显示识别结果")));
Result2Ui *pResult = (Result2Ui*)vMap.value("result").toLongLong();
if (pResult == nullptr)
{
pResult = new Result2Ui();
}
// 当没抠出轮毂和NG时自动调整曝光时间重新拍照最多调整的次数为5次
if (m_autoExposureSwitch)
{
if (vMap.contains("noCircle"))
{
if (m_exposureTimeCount == 0)
{
int curExposure = getCurExposureTime();
if (curExposure != m_exposureTimeArray[0])
{
emit(sgShowMsgdlg(tr("当前曝光时间未识别到轮毂!")));
emit sgAutoExposure();
m_exposureTimeCount++;
delete pResult;
return;
}
}
m_exposureTimeCount++;
if (m_exposureTimeCount >= m_exposureTimeArray.size())
{
// m_exposureTimeCount = 0;
emit(sgShowMsgdlg(tr("调整5次曝光时间后依然未能识别到轮毂")));
}
else
{
emit(sgShowMsgdlg(tr("当前曝光时间未识别到轮毂!")));
emit sgAutoExposure();
delete pResult;
return;
}
}
bool ngFlag = (pResult->m_strModel.isEmpty()) && (m_exposureTimeCount < m_exposureTimeArray.size());
if (ngFlag)
{
if (m_exposureTimeCount == 0)
{
int curExposure = getCurExposureTime();
if (curExposure != m_exposureTimeArray[0])
{
emit(sgShowMsgdlg(tr("当前曝光时间未识别到轮毂!")));
emit sgAutoExposure();
pResult->m_strModel = "NG";
saveImage(pResult);
m_exposureTimeCount++;
delete pResult;
return;
}
}
m_exposureTimeCount++;
if (m_exposureTimeCount >= m_exposureTimeArray.size())
{
// m_exposureTimeCount = 0;
emit(sgShowMsgdlg(tr("调整5次曝光时间后依然未能识别到轮毂")));
}
else
{
emit(sgShowMsgdlg(tr("当前曝光时间未识别到轮毂!")));
emit sgAutoExposure();
pResult->m_strModel = "NG";
saveImage(pResult);
delete pResult;
return;
}
}
}
if (vMap.contains("error")) if (vMap.contains("error"))
{ {
m_exposureTimeCount = 0;
emit(sgShowMsgdlg(tr("相机图像和背景图不一样,请更换检测背景!"))); emit(sgShowMsgdlg(tr("相机图像和背景图不一样,请更换检测背景!")));
return; return;
} }
Result2Ui *pResult = (Result2Ui*)vMap.value("result").toLongLong();
onShowResult(pResult); onShowResult(pResult);
if (m_pDebugDlg) { if (m_pDebugDlg) {
m_pDebugDlg->onShowResult(pResult); m_pDebugDlg->onShowResult(pResult);
} }
saveImage(pResult);
if (m_autoExposureSwitch)
{
m_exposureTimeCount = 0;
int exp = m_exposureTimeArray[m_exposureTimeCount];
m_pCoreCtrl->ISetExposureTime(m_camKey, exp);
}
// m_pUi->processResult(pResult); // m_pUi->processResult(pResult);
static int ErrorNum = 0; static int ErrorNum = 0;
if (m_pNet) { if (m_pNet) {
@ -561,6 +649,7 @@ void lpMainWin::IVariantMapToUI(const QString& camKey, const QVariantMap& vMap)
SendResultBee(LIGHT_BEE, 3); SendResultBee(LIGHT_BEE, 3);
} }
} }
if (DetectState::instance()->m_SendDetectStr2Net) if (DetectState::instance()->m_SendDetectStr2Net)
SendResultStr2PLC(pResult);//发送检测结果字符到PLC SendResultStr2PLC(pResult);//发送检测结果字符到PLC
if (DetectState::instance()->m_SendChannelRes2COM) if (DetectState::instance()->m_SendChannelRes2COM)
@ -569,7 +658,6 @@ void lpMainWin::IVariantMapToUI(const QString& camKey, const QVariantMap& vMap)
SendResultChannel2PLC(pResult);//发送检测结果通道到PLC SendResultChannel2PLC(pResult);//发送检测结果通道到PLC
m_pCtrl->saveResult(pResult); m_pCtrl->saveResult(pResult);
saveImage(pResult);
delete pResult; delete pResult;
} }
@ -1134,7 +1222,11 @@ Q_SLOT void lpMainWin::onTrigRecv(int m_value)
//if (DetectState::instance()->m_CameraTrigeType == 0)//软件触发模式 //if (DetectState::instance()->m_CameraTrigeType == 0)//软件触发模式
{ {
if (DetectState::instance()->IsDetect == true) { if (DetectState::instance()->IsDetect == true) {
//CC_Action(CC_AC_NEXT); // if (m_exposureTimeCount != 0)
// {
// m_pCoreCtrl->ISetExposureTime(m_camKey, m_exposureTimeArray[m_exposureTimeCount]);
// }
onTriggerCam(); onTriggerCam();
qWarning() << "soft ctrol camera :" << "(" << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss zzz") << ")"; qWarning() << "soft ctrol camera :" << "(" << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss zzz") << ")";
} }
@ -1484,6 +1576,8 @@ void lpMainWin::saveImage(Result2Ui* pRes)
if (pRes == nullptr) if (pRes == nullptr)
return; return;
qDebug() << "satrt tread save Image"; qDebug() << "satrt tread save Image";
QString curExposureTimeStr = QString::number(getCurExposureTime());
QDir testDir; QDir testDir;
bool IsTestDir = testDir.exists(DetectState::instance()->m_SaveImgDirPath); bool IsTestDir = testDir.exists(DetectState::instance()->m_SaveImgDirPath);
if (!IsTestDir) if (!IsTestDir)
@ -1520,6 +1614,7 @@ void lpMainWin::saveImage(Result2Ui* pRes)
QString errorSourceDir = fileSavePath + "\\Error"; QString errorSourceDir = fileSavePath + "\\Error";
filename += QString("_%1_%2_").arg((int)pRes->m_dThickness).arg((int)pRes->m_dDiameter); filename += QString("_%1_%2_").arg((int)pRes->m_dThickness).arg((int)pRes->m_dDiameter);
filename += QDateTime::currentDateTime().toString("hhmmsszzz"); filename += QDateTime::currentDateTime().toString("hhmmsszzz");
filename = filename + "_" + curExposureTimeStr;
if (DetectState::instance()->saveBad == 1) { if (DetectState::instance()->saveBad == 1) {
errorSourceDir += "\\Source"; errorSourceDir += "\\Source";
QString resultpath = errorSourceDir + "\\" + filename + ".png"; QString resultpath = errorSourceDir + "\\" + filename + ".png";
@ -1574,3 +1669,47 @@ Q_SLOT void lpMainWin::onwfPulseTimer()
// frame.data8 = 50; // frame.data8 = 50;
// SendDataToCom(0x43, frame); // SendDataToCom(0x43, frame);
} }
bool lpMainWin::readExposureTimeConfig(const QString& strPath)
{
QString filePath = strPath + "\\config\\exposure.json";
QJsonObject jsonObj = QZkJsonParser::ReadJsonAuto(filePath);
if (jsonObj.empty())
{
qDebug() << "Json file parsing failed!";
return false;
}
QJsonObject exposureObj = jsonObj.value("exposureTime").toObject();
QJsonObject::iterator objIterEnd = exposureObj.end();
for (auto objIter = exposureObj.begin(); objIter != objIterEnd; objIter++)
{
int exposureTime = objIter.value().toInt();
m_exposureTimeArray.emplace_back(exposureTime);
}
m_autoExposureSwitch = jsonObj.value("switch").toInt();
return true;
}
int lpMainWin::getCurExposureTime()
{
TP_CAMERA_OPTION camOpt;
m_pCoreCtrl->IGetCameraOption(m_camKey, camOpt);
return camOpt.exposure;
}
Q_SLOT void lpMainWin::onAutoExposure()
{
if (m_exposureTimeCount >= 0)
{
if (m_exposureTimeArray.size() <= m_exposureTimeCount)
return;
int exp = m_exposureTimeArray[m_exposureTimeCount];
qDebug() << "exp:" << exp;
m_pCoreCtrl->ISetExposureTime(m_camKey, exp);
}
onTriggerCam();
}

@ -61,12 +61,12 @@ signals:
void operate(); void operate();
void sgNetData(int, QVariantMap); void sgNetData(int, QVariantMap);
void sgAutoExposure();
private: private:
Q_SLOT void onLogInOut(QString strName, int level, int state); Q_SLOT void onLogInOut(QString strName, int level, int state);
Q_SLOT void onActionClicked(); Q_SLOT void onActionClicked();
Q_SLOT void onButtonClicked(); Q_SLOT void onButtonClicked();
Q_SLOT void onAutoExposure();
protected: protected:
bool onInitCoreCtrl(); bool onInitCoreCtrl();
@ -189,6 +189,16 @@ private:
int m_timerID{ 0 };//定时器 更新状态栏信息 int m_timerID{ 0 };//定时器 更新状态栏信息
quint64 m_runTimeCount{ 0 }; quint64 m_runTimeCount{ 0 };
private:
int m_exposureTimeCount{ 0 };
QString m_camKey;
std::vector<int> m_exposureTimeArray;
bool m_autoExposureSwitch{false};
bool readExposureTimeConfig(const QString& strPath);
int getCurExposureTime();
}; };
#endif #endif

Loading…
Cancel
Save