int main__()
{
g_srcImage = imread("G:\\1.bmp");
medianBlur(g_srcImage, g_srcImage, 7);
threshold(g_srcImage, g_srcImage, 80, 255, 0);
medianBlur(g_srcImage, g_srcImage, 7);
imshow("原始圖像", g_srcImage);
if (!g_srcImage.data)
{
cout << "原始圖讀取失敗" << endl;
return -1;
}
g_tempalteImage = imread("G:\\3.jpg");
Mat image=g_tempalteImage.clone();
threshold(g_tempalteImage, g_tempalteImage, 80, 255, 0);
medianBlur(g_tempalteImage, g_tempalteImage, 7);
imshow("tMOBAN圖像", g_tempalteImage);
int area = 0;
for (int i = 0; i < g_tempalteImage.rows; i++)
{
for (int j = 0; j < g_tempalteImage.cols; j++)
{
if (g_tempalteImage.at<uchar>(i, j) = 255) {
area++;
}
}
}
if (area > g_tempalteImage.cols*g_tempalteImage.rows / 2)
{
bitwise_not(g_tempalteImage, g_tempalteImage);
bitwise_not(g_srcImage, g_srcImage);
}
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(image, contours,
hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE, Point());
Mat Contours = Mat::zeros(image.size(), CV_8UC1); //繪製
vector<cv::Mat> templates;
vector<cv::Rect>rects;
for (int i = 0; i < contours.size(); i++)
{
if (contourArea(contours[i], true) > 100 && contourArea(contours[i]) < 8000, true)
{
drawContours(Contours, contours,
i, Scalar(255), CV_FILLED, 8, hierarchy);
Mat roi, roi2;
cv::Rect rect = cv::boundingRect((cv::Mat)contours[i]);
roi = g_tempalteImage(rect);
roi2 = Contours(rect);
if (rect.width > 20 && rect.height < 200)
{
printf("%d,%d,%d,%d\t", rect.x, rect.y, rect.width, rect.height);
Mat roi3;
roi.copyTo(roi3, roi2);
rects.push_back(rect);
templates.push_back(roi3);
ostringstream of;
of << "G:\\SVM-OCR\\Template\\" << i + 300 << ".jpg";
cv::imwrite(of.str(), roi3);
cv::rectangle(image, cv::Point(rect.x, rect.y), cv::Point(rect.width + rect.x, rect.height + rect.y), cv::Scalar(255), 3);
//rectangle(g_tempalteImage, Point(rect.x, rect.y), Point(rect.x + 50, rect.y + 50));
}
}
}
imshow("template圖像", image);
vector<double> g_results;
while (true)
{
Mat temps = templates.back();
on_matching(g_srcImage, temps, g_results);
templates.pop_back();
cout << "模板Size" << templates.size() << endl;
if (templates.empty()) { break; }
}
if (!g_tempalteImage.data)
{
cout << "模板圖讀取失敗" << endl;
return -1;
}
imshow("原始圖", g_srcImage);
//namedWindow("效果圖", CV_WINDOW_AUTOSIZE);
//createTrackbar("方法", "原始圖", &g_nMatchMethod, g_nMaxTrackbarNum, on_matching);
waitKey(90000);
}