OpenCV 获取图像矩形4点数据写入txt文件 MFC

https://blog.csdn.net/zaibeijixing/article/details/83176330

FILE *fp;
	fopen_s(&fp, "sample.txt", "r");
	Mat imgd = imread("d:\\src.bmp");
	Rect rect;
	for (int i=0;i<33;i++)//读取数据
	{
		fscanf_s(fp, "%d %d %d %d", &rect.x, &rect.y, &rect.width, &rect.height);
		ReadRect.push_back(rect);
	}
	fclose(fp);

	for (int i=0;iShowImage(IDC_STATIC_ORC, sec);
void COpenCVOCRDlg::matchTemplateFunction(cv::Mat templ, cv::Mat &img)//模板匹配 获取矩形四点存入文件 方便下次直接读出
{
	EnterCriticalSection(&cs);
	int result_cols = img.cols - templ.cols + 1;
	int result_rows = img.rows - templ.rows + 1;
	result.create(result_cols, result_rows, CV_32FC1);

	matchTemplate(img, templ, result, CV_TM_SQDIFF_NORMED);//这里我们使用的匹配算法是标准平方差匹配 method=CV_TM_SQDIFF_NORMED,数值越小匹配度越好
	normalize(result, result, 0, 1, NORM_MINMAX, -1, Mat());

	double minVal = -1;
	double maxVal;
	Point minLoc;
	Point maxLoc;
	Point matchLoc;

	minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc, Mat());
	//Rect rect(matchLoc, Point(matchLoc.x + temp1.cols, matchLoc.y + temp1.rows));
	matchLoc = minLoc;

	rectangle(img, matchLoc, Point(matchLoc.x + templ.cols, matchLoc.y + templ.rows), Scalar(0, 255, 0), 2, 8, 0);

	int width = abs(matchLoc.x - (matchLoc.x + templ.cols));
	int height = abs(matchLoc.y - (matchLoc.y + templ.rows));
	Rect rect = { min(matchLoc.x,(matchLoc.x + templ.cols)),min(matchLoc.y,(matchLoc.y + templ.rows)),width,height };
	CaptrueRect.push_back(rect);//存放数据
	::LeaveCriticalSection(&cs);
}
main.c


  ofstream file("sample.txt");
	if (!file)
	{
		cout << "open file error!";
		return 1;
	}

	vector::iterator it = CaptrueRect.begin();
	
	for (; it != CaptrueRect.end(); ++it)
	{

		file << it->x << ' ' << it->y << ' ' << it->width << ' ' << it->height << endl;
	}
	
	file << endl;

你可能感兴趣的:(win32)