OpenCV 獲取影象矩形4點資料寫入txt檔案 MFC
阿新 • • 發佈:2018-12-21
讀
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;i<ReadRect.size();++i) { Rect re = ReadRect[i]; rectangle(imgd, re, Scalar(0, 0, 255)); } IplImage *sec = &IplImage(imgd); dlg->ShowImage(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<Rect>::iterator it = CaptrueRect.begin(); for (; it != CaptrueRect.end(); ++it) { file << it->x << ' ' << it->y << ' ' << it->width << ' ' << it->height << endl; } file << endl;