// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//
#include "pch.h"
#include<opencv2\opencv.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<vector>
using namespace cv;
using namespace std;
int main()
{
Mat img2= imread("D:\\f1.bmp",0);
Rect r1,r2;
r1.x = 323;
r1.y = 987;
r1.width = 30;
r1.height = 30;
r2.x = 5355;
r2.y = 975;
r2.width = 30;
r2.height = 30;
Mat d1 = Mat(img2, r1);
Mat d2= Mat(img2, r1);
threshold(d1, d1, 200, 255, THRESH_BINARY);
threshold(d2, d2, 200, 255, THRESH_BINARY);
int a = d1.at<uchar>(2, 0);
int b = d2.at<uchar>(2, 0);
vector<int> l1;
vector<int> l2;
if (a == 0)
{
for (int i = 0; i < d1.rows; i++)
{
for (int j = 0; j < d1.cols; j++)
{
if (d1.at<uchar>(i, j) == 255)
{
l1.push_back(j);
break;
}
}
}
}
if (b == 0)
{
for (int i = 0; i < d2.rows; i++)
{
for (int j = 0; j < d2.cols; j++)
{
if (d2.at<uchar>(i, j) == 255)
{
l2.push_back(j);
break;
}
}
}
}
int s1 = 0;
int s2 = 0;
for (int i = 0; i < l1.size(); i++)
{
s1 = l1[i] + s1;
}
int m1 = s1 / l1.size();
int c1 = r1.x + m1;
for (int i = 0; i < l2.size(); i++)
{
s2 = l2[i] + s2;
}
int m2 = s2 / l2.size();
int c2 = r2.x + m2;
int c3 = c2 - c1;
imshow("匹配后的图片", d1);
waitKey(0);
return 0;
}