eyemNCCBasedMatch.h 4.1 KB
#pragma once
//
//	eyemNCCBasedMatchͷ
//
#ifndef __EYEM_NCCBASEDMATCH_H
#define __EYEM_NCCBASEDMATCH_H

#include "eyemLib.h"

class ncc_based_matching
{
public:
	ncc_based_matching() {};
	~ncc_based_matching(void);

	int create_ncc_model(cv::Mat&, int iMinReduceArea);

	int find_ncc_model(cv::Mat& tpImage, double dToleranceAngle, int iNumMatches, double dMaxOverlap, double dScore, float* fCenterX, float* fCenterY, float* fMatchScore, float* fMatchAngle);

	void draw_match_shapes();

private:
	struct s_TemplData
	{
		std::vector<cv::Mat> vecPyramid;
		std::vector<cv::Scalar> vecTemplMean;
		std::vector<double> vecTemplNorm;
		std::vector<double> vecInvArea;
		std::vector<BOOL> vecResultEqual1;
		BOOL bIsPatternLearned;
		int iTopLayer;
		void clear()
		{
			std::vector<cv::Mat>().swap(vecPyramid);
			std::vector<double>().swap(vecTemplNorm);
			std::vector<double>().swap(vecInvArea);
			std::vector<cv::Scalar>().swap(vecTemplMean);
			std::vector<BOOL>().swap(vecResultEqual1);
		}
		void resize(int iSize)
		{
			vecTemplMean.resize(iSize);
			vecTemplNorm.resize(iSize, 0);
			vecInvArea.resize(iSize, 1);
			vecResultEqual1.resize(iSize, FALSE);
		}
		s_TemplData()
		{
			bIsPatternLearned = FALSE;
			iTopLayer = 0;
		}
	};
	struct s_MatchParameter
	{
		cv::Point2d pt;
		double dMatchScore;
		double dMatchAngle;
		//Mat matRotatedSrc;
		cv::Rect rectRoi;
		double dAngleStart;
		double dAngleEnd;
		cv::RotatedRect rectR;
		cv::Rect rectBounding;
		BOOL bDelete;

		double vecResult[3][3];//for subpixel
		int iMaxScoreIndex;//for subpixel
		BOOL bPosOnBorder;
		cv::Point2d ptSubPixel;
		double dNewAngle;

		s_MatchParameter(cv::Point2f ptMinMax, double dScore, double dAngle)//, Mat matRotatedSrc = Mat ())
		{
			pt = ptMinMax;
			dMatchScore = dScore;
			dMatchAngle = dAngle;

			bDelete = FALSE;
			dNewAngle = 0.0;

			bPosOnBorder = FALSE;
		}
		s_MatchParameter()
		{
			double dMatchScore = 0;
			double dMatchAngle = 0;
		}
		~s_MatchParameter()
		{

		}
	};
	struct s_SingleTargetMatch
	{
		cv::Point2d ptLT, ptRT, ptRB, ptLB, ptCenter;
		double dMatchedAngle;
		double dMatchScore;
	};

	cv::Mat m_matSrc;
	cv::Mat m_matDst;

	cv::Mat showResult;

	s_TemplData m_TemplData;
	std::vector<s_SingleTargetMatch> m_vecSingleTargetData;

	int getTopLayer(cv::Mat&, int);

	void drawDashLine(cv::Mat& matDraw, cv::Point ptStart, cv::Point ptEnd, cv::Scalar color1 = cv::Scalar(0, 0, 255), cv::Scalar color2 = cv::Scalar::all(255));
	void drawMarkCross(cv::Mat& matDraw, int iX, int iY, int iLength, cv::Scalar color, int iThickness);

	static bool compareScoreBig2Small(const s_MatchParameter& lhs, const s_MatchParameter& rhs) { return  lhs.dMatchScore > rhs.dMatchScore; }
	static bool comparePtWithAngle(const std::pair<cv::Point2f, double>& lhs, const std::pair<cv::Point2f, double>& rhs) { return lhs.second < rhs.second; }

	static bool compareMatchResultByScore(const s_SingleTargetMatch& lhs, const s_SingleTargetMatch& rhs) { return lhs.dMatchScore > rhs.dMatchScore; }
	static bool compareMatchResultByPosX(const s_SingleTargetMatch& lhs, const s_SingleTargetMatch& rhs) { return lhs.ptCenter.x < rhs.ptCenter.x; }

	void filterWithScore(std::vector<s_MatchParameter>* vec, double dScore);
	void filterWithRotatedRect(std::vector<s_MatchParameter>* vec, int iMethod, double dMaxOverLap);

	void sortPtWithCenter(std::vector<cv::Point2f>& vecSort);

	cv::Point2f ptRotatePt2f(cv::Point2f ptInput, cv::Point2f ptOrg, double dAngle);

	cv::Size bestRotationSize(cv::Size sizeSrc, cv::Size sizeDst, double dRAngle);

	BOOL subPixEsimation(std::vector<s_MatchParameter>* vec, double* dNewX, double* dNewY, double* dNewAngle, double dAngleStep, int iMaxScoreIndex);

	void getRotatedROI(cv::Mat& matSrc, cv::Size size, cv::Point2f ptLT, double dAngle, cv::Mat& matROI);

	cv::Point nextMaxLoc(cv::Mat& matResult, cv::Point ptMaxLoc, double dMinValue, int iTemplateW, int iTemplateH, double& dMaxValue, double dMaxOverlap);

	void m_matchTemplate(cv::Mat& matSrc, s_TemplData* pTemplData, cv::Mat& matResult, int iLayer);

	void TM_CCOEFF_Denominator(cv::Mat& matSrc, s_TemplData* pTemplData, cv::Mat& matResult, int iLayer);
};

ncc_based_matching nbm;

#endif/* __EYEM_NCCBASEDMATCH_H */