You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
4.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "Base_matcher.h"
#include "PatternMatching.h"
#include "Arith_ImgOperate.h"
namespace templateMatching
{
Fast_Matcher* GetMatcher(const MatcherParam& param)
{
MatcherParam paramCopy = param;
BaseMatcher* matcher = nullptr;
switch (paramCopy.matcherType)
{
case MatcherType::PATTERN:
//logger_->info("Initializing matcher for type: PATTERN");
matcher = new PatternMatcher(paramCopy);
break;
default:
break;
}
if (matcher->isInited() == false)
{
delete matcher;
matcher = nullptr;
}
return matcher;
}
void Matcher_Create(templateMatching::Fast_Matcher** matcher)
{
// 匹配器参数
templateMatching::MatcherParam param;
param.angle = 0; // 旋转角度容纳范围0表示无旋转角度
param.iouThreshold = 0.1;
param.matcherType = templateMatching::MatcherType::PATTERN;
param.maxCount = 6; // 输出候选个数
param.minArea = 256;
param.scoreThreshold = 0.5;
*matcher = GetMatcher(param);
(*matcher)->g_Matcher_Image = new UINT16[MATCHER_MAX * MATCHER_MAX]; // 锁定创建,解锁需要释放
}
void Matcher_SetTemplate(templateMatching::Fast_Matcher* matcher, GD_VIDEO_FRAME_S img, RECT32S crfCandiRect)
{
MINMAXRECT mmRect = { 0 };
cv::Mat roi = cv::Mat(crfCandiRect.h, crfCandiRect.w, 0);
if (GD_PIXEL_FORMAT_E::GD_PIXEL_FORMAT_GRAY_Y16 == img.enPixelFormat)
{
GD_FRAME_pyrDownToY16(img, matcher->g_Matcher_Image, 1, crfCandiRect, mmRect);
IMGO_gcABCLinRoi(matcher->g_Matcher_Image, roi.data, crfCandiRect.w * crfCandiRect.h);
}
else if (GD_PIXEL_FORMAT_E::GD_PIXEL_FORMAT_GRAY_Y8 == img.enPixelFormat ||
GD_PIXEL_FORMAT_E::GD_PIXEL_FORMAT_RGB_PACKED == img.enPixelFormat ||
GD_PIXEL_FORMAT_E::GD_PIXEL_FORMAT_NV12 == img.enPixelFormat)
{
GD_FRAME_pyrDownToY8(img, roi.data, 1, crfCandiRect, mmRect);
}
matcher->setTemplate(roi);
}
void Matcher_Destory(templateMatching::Fast_Matcher* matcher)
{
delete matcher->g_Matcher_Image;
delete matcher;
}
void Matcher_Process(templateMatching::Fast_Matcher* matcher, GD_VIDEO_FRAME_S img, OBJECTSTATUS* pTrackStatus, TSky_Output* pTSky_Output)
{
SINT16 scale = 7;
SINT32 regionW = SINT32(scale * pTrackStatus->sfSize.w);
SINT32 regionH = SINT32(scale * pTrackStatus->sfSize.h);
RECT32S sRect = { SINT32(pTrackStatus->ptPos.x - 3.5 * pTrackStatus->sfSize.w), SINT32(pTrackStatus->ptPos.y - 3.5 * pTrackStatus->sfSize.h),
regionW, regionH };
cv::Mat searchRegion = cv::Mat(regionH, regionW, CV_8UC1, cv::Scalar(0));
MINMAXRECT mmRect = { 0 };
if (GD_PIXEL_FORMAT_E::GD_PIXEL_FORMAT_GRAY_Y16 == img.enPixelFormat)
{
GD_FRAME_pyrDownToY16(img, matcher->g_Matcher_Image, 1, sRect, mmRect);
IMGO_gcABCLinRoi(matcher->g_Matcher_Image, searchRegion.data, regionW * regionH);
}
else if (GD_PIXEL_FORMAT_E::GD_PIXEL_FORMAT_GRAY_Y8 == img.enPixelFormat ||
GD_PIXEL_FORMAT_E::GD_PIXEL_FORMAT_RGB_PACKED == img.enPixelFormat ||
GD_PIXEL_FORMAT_E::GD_PIXEL_FORMAT_NV12 == img.enPixelFormat)
{
GD_FRAME_pyrDownToY8(img, searchRegion.data, 1, sRect, mmRect);
}
pTSky_Output->m_nMatcherNum = 0;
SINT32 matchedNum = matcher->match(searchRegion, sRect, matcher->matchResults);
if (matchedNum > 0)
{
pTSky_Output->m_nMatcherNum = matchedNum;
for (int i = 0; i < MIN(matcher->matchResults.size(), DT_MATCHER_MAX_NUM); ++i)
{
memcpy(&pTSky_Output->mMatcher_Array[i], &matcher->matchResults[i], sizeof(MATCHER_TARGET));
}
}
// 计算得到最佳匹配结果,并进行尺寸、匹配特性分析
}
}