OpenCV match

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )


First, realize the template matching matchTemplate ()

prototype

void matchTemplate(
	InputArray image,
	InputArray templ,
	OutputArray result,
	int method
)

parameter

  • image: the image search to be prime. It must be 8-bit or 32-bit floating-point image
  • templ: a template image search hormone. And image data types need to be the same as search elements, and the size can not be greater than it.
  • result: the map image comparison result. You must be a single-channel, 32-bit floating-point image
  • method: Matching
    • TM_SQDIFF: matching squared difference
    • TM_SQDIFF_NORMED: Normalized
    • TM_CCORR: correlation matching method
    • TM_CCORR_NORMED: normalized correlation matching method
    • TM_CCOEFF: matching coefficient
    • TM_CCOEFF_NORMED: normalization coefficient matching

example

#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;

int main()
{
	//灰度图
	Mat srcImage=imread("M.jpg",0);
	
	//取阈值成二值图
	srcImage=srcImage>180;

	//CLOSE
	Mat kernel=getStructuringElement(MORPH_RECT,Size(25,25));
	morphologyEx(srcImage,srcImage,MORPH_CLOSE,kernel);

	namedWindow("srcImage",WINDOW_NORMAL);
	imshow("srcImage",srcImage);
	
	Mat dstImage=Mat::zeros(srcImage.size(),srcImage.type());

	vector<vector<Point> > contours;
	vector<Vec4i> hierarchy;
	
	findContours(srcImage,contours,hierarchy,RETR_EXTERNAL,CHAIN_APPROX_SIMPLE);
	
	Scalar color(rand()&255,rand()&255,rand()&255);
	drawContours(dstImage,contours,-1,color,FILLED,8,hierarchy);

	vector<vector<Point> >::iterator It;
	
	Point vertex[4];
	int x1,y1,x2,y2;
	int keyworld=0;

    for(It = contours.begin();It < contours.end();It++)
	{
		//画出可包围数字的最小矩形
        Rect rect = boundingRect(*It);
        vertex[0] = rect.tl();
        //矩阵左上角的点
        vertex[1].x = rect.tl().x, vertex[1].y =rect.br().y;
        //矩阵左下方的点
        vertex[2] = rect.br();
        //矩阵右下角的点
        vertex[3].x = rect.br().x, vertex[3].y =rect.tl().y;
        //矩阵右上方的点
	
		for( int j = 0; j < 4; j++)
		{
			line(dstImage,vertex[j],vertex[(j+1)%4],Scalar(255,255,255),20);
		}

		if(keyworld==0)
		{
			x1=vertex[0].x;
			y1=vertex[0].y;
			x2=vertex[2].x;
			y2=vertex[2].y;
			keyworld=1;
		}
	}

	// namedWindow("dstImage",WINDOW_NORMAL);
	// imshow("dstImage",dstImage);


	srcImage=srcImage(Rect(x1,y1,x2-x1,y2-y1));
	namedWindow("srcImagePlus",WINDOW_NORMAL);
	imshow("srcImagePlus",srcImage);
	Mat templateImage=imread("NG/n0.jpg",0);
	int resultImageCols=srcImage.cols-templateImage.cols+1;
	int resultImageRows=srcImage.rows-templateImage.rows+1;
	Mat resultImage;
	resultImage.create(resultImageCols,resultImageRows,CV_32FC1);
	matchTemplate(srcImage,templateImage,resultImage,TM_SQDIFF);
	imshow("result",resultImage);
	waitKey();
	return 0;
}

Two, minMaxLoc

prototype

void minMaxLoc(
	InputArray 	src,
	double* minVal,
	double* maxVal=0,
	Point*  minLoc=0,
	Point*  maxLoc=0,
	InputArray mask=noArray() 
)

Find the global minimum and maximum values ​​in the array.

Function cv :: minMaxLoc find the minimum and maximum element values ​​and their positions. Search extremes in the whole array, or if the mask is not an empty array, the array in the specified area in search extremes.

This feature does not apply to a multi-channel array. If you need to find the minimum or maximum element in all channels, first use Mat :: reshape the array reinterpreted as a single channel. Or you can use extractImageCOI or mixChannels or split to extract specific channel.

parameter

  • src: the input image is a single-channel array.
  • minVal: minimum value pointer pointing returned; If not, use NULL.
  • maxVal: Returns a pointer to a maximum value; If not, use NULL.
  • minLoc: Returns a pointer to the location of the minimum (in the 2D case); if not, then use NULL.
  • maxLoc: Returns a pointer to the location of the maximum (in the 2D case); if not, then use NULL.
  • mask: used to select the subarray optional mask.

Case

double minVal,maxVal;
Point minLoc,maxLoc;
minMaxLoc(srcIamge,&minVal,&maxVal,&minLoc,&maxLoc,Mat());

Guess you like

Origin blog.csdn.net/sandalphon4869/article/details/94646421