|
|
|
|
@ -7,9 +7,18 @@ CategoryMatcher::CategoryMatcher(QObject *parent)
|
|
|
|
|
m_ilmatch = ILMatchPtr(__uuidof(LMatch));
|
|
|
|
|
m_ilmatchF = ILMatchPtr(__uuidof(LMatch));
|
|
|
|
|
m_display = ILDisplayPtr(__uuidof(LDisplay));
|
|
|
|
|
m_text = ILTextPtr(__uuidof(LText));
|
|
|
|
|
m_textSys= ILSystemPtr(__uuidof(LSystem));
|
|
|
|
|
initILMatchParam(QCoreApplication::applicationDirPath() + MATCHER_CONFIG);
|
|
|
|
|
setILMatch(m_matchParam);
|
|
|
|
|
qDebug() << "init CategoryMatcher successed";
|
|
|
|
|
|
|
|
|
|
m_text->Alignment = LPVAlignment::LPVAlignLeft; // 设置文本绘制时的对齐方式
|
|
|
|
|
m_textSys->SetFontColor(m_text, m_textSys->GetColorRef(0, 255, 0)); // 设置文本的颜色
|
|
|
|
|
m_textSys->SetFontHeight(m_text, 80); // 设置字体高度
|
|
|
|
|
m_textSys->SetFontFamily(m_text, "Microsoft YaHei"); // 设置字体类型
|
|
|
|
|
m_text->PosX = 100;
|
|
|
|
|
m_text->PosY = 100; // 设置文本绘制时的位置
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CategoryMatcher::~CategoryMatcher()
|
|
|
|
|
@ -76,33 +85,14 @@ void CategoryMatcher::setILMatch(const MatchParam& matchParam)
|
|
|
|
|
|
|
|
|
|
bool CategoryMatcher::smokeMatch(const QString& typeNo, ILImagePtr image, int id)
|
|
|
|
|
{
|
|
|
|
|
std::pair<int, QVector<ILMatchResultPtr>> templateLevelAndvecMatchRltPtr;
|
|
|
|
|
bool bRlt = smokeMatch(typeNo, image, templateLevelAndvecMatchRltPtr,id);
|
|
|
|
|
drawImage(image, templateLevelAndvecMatchRltPtr);
|
|
|
|
|
return bRlt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CategoryMatcher::smokeMatch(const QString& typeNo, const cv::Mat& inputMat, cv::Mat& rltMat)
|
|
|
|
|
{
|
|
|
|
|
if (inputMat.empty())
|
|
|
|
|
{
|
|
|
|
|
qWarning() << "inputMat is empty";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
ILImagePtr ilImgGray(__uuidof(LImage));
|
|
|
|
|
cv::Mat imgMatGray;
|
|
|
|
|
ensureGrayImg(inputMat, imgMatGray);
|
|
|
|
|
qDebug() << "ensureGrayImg OK!";
|
|
|
|
|
ilImgGray->SetImageData(imgMatGray.cols, imgMatGray.rows, (void*)imgMatGray.data, imgMatGray.step, 0);
|
|
|
|
|
qDebug() << "LPV SetImageData OK!";
|
|
|
|
|
std::pair<int, QVector<ILMatchResultPtr>> templateLevelAndvecMatchRltPtr;
|
|
|
|
|
bool bRlt = smokeMatch(typeNo, ilImgGray, templateLevelAndvecMatchRltPtr, 0);
|
|
|
|
|
drawImage(imgMatGray, rltMat, templateLevelAndvecMatchRltPtr);
|
|
|
|
|
QMap<int, ILMatchResultPtr> resultsPair;
|
|
|
|
|
bool bRlt = smokeMatch(typeNo, image, resultsPair,id);
|
|
|
|
|
drawImage(image, resultsPair);
|
|
|
|
|
return bRlt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CategoryMatcher::smokeMatch(const QString& typeNo, const ILImagePtr ilImgPtr,
|
|
|
|
|
std::pair<int, QVector<ILMatchResultPtr>>& templateLevelAndvecMatchRltPtr,int id)
|
|
|
|
|
QMap<int, ILMatchResultPtr>& resultsMap, int id)
|
|
|
|
|
{
|
|
|
|
|
TemplateObject templateObj;
|
|
|
|
|
QString templateImgFolderPath = TYPE_TEMPLATE_PATH;
|
|
|
|
|
@ -123,16 +113,16 @@ bool CategoryMatcher::smokeMatch(const QString& typeNo, const ILImagePtr ilImgPt
|
|
|
|
|
QVector<double> vecMatchSroreMax;
|
|
|
|
|
QVector<ILMatchResultPtr> vecMatchRltsMax;
|
|
|
|
|
QList<int> keys = templateObj.uniqueKeys();
|
|
|
|
|
int templateLevel = keys.length();
|
|
|
|
|
str = QString("[%1]:templateLevel =%2").arg(id).arg(templateLevel);
|
|
|
|
|
int templateLevelCount = keys.length();
|
|
|
|
|
str = QString("[%1]:templateLevelCount =%2").arg(id).arg(templateLevelCount);
|
|
|
|
|
qDebug() << str;
|
|
|
|
|
templateLevelAndvecMatchRltPtr.first = templateLevel;
|
|
|
|
|
|
|
|
|
|
// ±éÀú²»Í¬Ä£°å¼¶±ð
|
|
|
|
|
for each (int level in keys)
|
|
|
|
|
{
|
|
|
|
|
QVector<double> vecMatchSrore;
|
|
|
|
|
QVector<ILMatchResultPtr> vecMatchRlts;
|
|
|
|
|
resultsMap.insert(level, nullptr);
|
|
|
|
|
QMap<QString, int> strScoreMap = templateObj.value(level);
|
|
|
|
|
for (QMap<QString, int>::iterator it = strScoreMap.begin(); it != strScoreMap.end(); ++it)
|
|
|
|
|
{
|
|
|
|
|
@ -149,6 +139,7 @@ bool CategoryMatcher::smokeMatch(const QString& typeNo, const ILImagePtr ilImgPt
|
|
|
|
|
m_ilmatch->Learn(ilTemplatePtr, nullptr);
|
|
|
|
|
ILImagePtr img(__uuidof(LImage));
|
|
|
|
|
ILImagePtr tempImage(__uuidof(LImage));
|
|
|
|
|
//由于如果角度开到±180,部分匹配时间过长,故而用两次模板匹配,分别匹配模板和模板旋转180度情况
|
|
|
|
|
ILImageArithmPtr imgArithmF(__uuidof(LImageArithm));
|
|
|
|
|
imgArithmF->Flip(ilTemplatePtr, LPVFlipType::LPVFlipH, tempImage);
|
|
|
|
|
imgArithmF->Flip(tempImage, LPVFlipType::LPVFlipV, tempImage);
|
|
|
|
|
@ -207,7 +198,9 @@ bool CategoryMatcher::smokeMatch(const QString& typeNo, const ILImagePtr ilImgPt
|
|
|
|
|
}
|
|
|
|
|
if (vecMatchSrore.isEmpty())
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
str = QString("[%1]:Level: %1 match failed!").arg(id).arg(level);
|
|
|
|
|
qDebug() << str;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
@ -215,19 +208,9 @@ bool CategoryMatcher::smokeMatch(const QString& typeNo, const ILImagePtr ilImgPt
|
|
|
|
|
auto maxMatchScorePos = std::max_element(vecMatchSrore.begin(), vecMatchSrore.end());
|
|
|
|
|
auto maxIndex = maxMatchScorePos - vecMatchSrore.begin();
|
|
|
|
|
vecMatchSroreMax.push_back(*maxMatchScorePos);
|
|
|
|
|
vecMatchRltsMax.push_back(vecMatchRlts.at(maxIndex));
|
|
|
|
|
}
|
|
|
|
|
//vecMatchRltsMax.push_back(vecMatchRlts.at(maxIndex));
|
|
|
|
|
resultsMap[level]=vecMatchRlts.at(maxIndex);
|
|
|
|
|
}
|
|
|
|
|
if (vecMatchSroreMax.isEmpty() || (vecMatchSroreMax.length() != templateLevel))
|
|
|
|
|
{
|
|
|
|
|
str = QString("[%1]:Level: %1 match failed!").arg(id).arg(vecMatchSroreMax.length() + 1);
|
|
|
|
|
qDebug() << str;
|
|
|
|
|
templateLevelAndvecMatchRltPtr.second = vecMatchRltsMax;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
templateLevelAndvecMatchRltPtr.second = vecMatchRltsMax;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
@ -288,48 +271,63 @@ bool CategoryMatcher::getTemplateImgFileInfo(const QString& folderPath, Template
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CategoryMatcher::drawImage(ILImagePtr image, std::pair<int, QVector<ILMatchResultPtr>> templateLevelAndvecMatchRltPtr)
|
|
|
|
|
void CategoryMatcher::drawImage(ILImagePtr image, QMap<int, ILMatchResultPtr> resultsMap)
|
|
|
|
|
{
|
|
|
|
|
if (image->Void())
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
int level = 1;
|
|
|
|
|
int templateLevel = templateLevelAndvecMatchRltPtr.first;
|
|
|
|
|
|
|
|
|
|
std::stringstream textStream("");
|
|
|
|
|
//在Display上绘制图像+结果
|
|
|
|
|
m_display->SetImage(image);
|
|
|
|
|
m_display->RemoveAllObjects();
|
|
|
|
|
QString tittleStr,rltStr;
|
|
|
|
|
|
|
|
|
|
QVector<ILMatchResultPtr> vecMatchRlts = templateLevelAndvecMatchRltPtr.second;
|
|
|
|
|
QString rltStr;
|
|
|
|
|
cv::Scalar textColor(0, 255, 0);
|
|
|
|
|
if (!vecMatchRlts.isEmpty() && (vecMatchRlts.length() == templateLevel))
|
|
|
|
|
bool bRlt = false;
|
|
|
|
|
for (auto iter = resultsMap.begin(); iter != resultsMap.end(); iter++)
|
|
|
|
|
{
|
|
|
|
|
rltStr = QString("Match successful! Total level: %1").arg(templateLevel);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
ILMatchResultPtr matchRlt = iter.value();
|
|
|
|
|
if (matchRlt == nullptr)
|
|
|
|
|
{
|
|
|
|
|
textColor = cv::Scalar(0, 0, 255);
|
|
|
|
|
rltStr = QString("Match failed! Total level: %1").arg(templateLevel);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bRlt = true;
|
|
|
|
|
double score = matchRlt->GetScore();
|
|
|
|
|
rltStr.append(QString("\n Level %1 score: %2").arg(iter.key()).arg(score));
|
|
|
|
|
|
|
|
|
|
//在Display上绘制图像+结果
|
|
|
|
|
m_display->SetImage(image);
|
|
|
|
|
//m_display->AddObject(rltStr,0);
|
|
|
|
|
|
|
|
|
|
rltStr.clear();
|
|
|
|
|
for each(ILMatchResultPtr matchRltPtr in vecMatchRlts)
|
|
|
|
|
if (iter.key() == 1)
|
|
|
|
|
{
|
|
|
|
|
// 存在问题:如果不存在1级模板,直接是2级模板
|
|
|
|
|
double score = matchRltPtr->GetScore();
|
|
|
|
|
rltStr.append(QString(" Level %1 score: %2").arg(level++).arg(score));
|
|
|
|
|
//存在的问题——大模板的时候匹配的得到的结果是放缩后的结果,需要对位置进行扩大
|
|
|
|
|
m_display->AddObject(matchRltPtr, LPVPatDrawFlags::LPVPatDrawBoundingRect | LPVPatDrawFlags::LPVPatDrawCenter);
|
|
|
|
|
ILRotRectRegionPtr m_rect(__uuidof(LRotRectRegion));
|
|
|
|
|
m_rect->SetPlacement(matchRlt->GetCenter()->X * 2, matchRlt->GetCenter()->Y * 2
|
|
|
|
|
, matchRlt->GetRect()->Width * 2, matchRlt->GetRect()->Height * 2, matchRlt->GetRect()->Angle);
|
|
|
|
|
m_textSys->SetPenWidth(m_rect, 3);
|
|
|
|
|
m_textSys->SetPenColor(m_rect, m_textSys->GetColorRef(0, 255, 0));
|
|
|
|
|
m_display->AddObject(m_rect, 0);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
m_textSys->SetPenWidth(matchRlt, 3);
|
|
|
|
|
m_textSys->SetPenColor(matchRlt, m_textSys->GetColorRef(0, 255, 0));
|
|
|
|
|
m_display->AddObject(matchRlt, LPVPatDrawFlags::LPVPatDrawBoundingRect);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!rltStr.isEmpty())
|
|
|
|
|
}
|
|
|
|
|
if (bRlt)
|
|
|
|
|
{
|
|
|
|
|
m_textSys->SetFontColor(m_text, m_textSys->GetColorRef(0, 255, 0));
|
|
|
|
|
tittleStr = QString("Match successful! Total level Count: %1").arg(resultsMap.keys().length());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//m_display->AddObject(rltStr, 0);
|
|
|
|
|
m_textSys->SetFontColor(m_text, m_textSys->GetColorRef(255, 0, 0));
|
|
|
|
|
tittleStr = QString("Match failed! Total level Count: %1").arg(resultsMap.keys().length());
|
|
|
|
|
}
|
|
|
|
|
textStream << tittleStr.toStdString();
|
|
|
|
|
textStream << rltStr.toStdString();
|
|
|
|
|
m_text->Text = textStream.str().c_str();
|
|
|
|
|
m_display->AddObject(m_text,0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CategoryMatcher::saveResultsImage(const QString& filePath)
|
|
|
|
|
|