#include#include using namespace cv; using namespace std; void Draw_match(Mat &src1, Mat &src2, int x_shift, int y_shift); void drawline(Mat &finsrc, vector pnums, int x_shift, int y_shift,int col); void cal_shift_roi(Mat src1, Mat src2, Point subxy, vector pnums); int main() { Mat src1 = imread("D:/pictures/waferImg/img0523/img0523/01.bmp"); Mat src2 = imread("D:/pictures/waferImg/img0523/img0523/02.bmp"); resize(src1, src1, Size(840, 680)); resize(src2, src2, Size(840,680)); imshow("startsrc1", src1); imshow("startsrc2", src2); Point x1 = Point(200, 200); Point x2 = Point(250, 150); Point x3 = Point(350, 240); Point x4 = Point(440, 250); Point x11 = Point(302, 205); Point x21 = Point(290, 180); Point x31 = Point(300, 420); Point x41 = Point(490, 360); vector points; points.push_back(x1); points.push_back(x2); points.push_back(x3); points.push_back(x4); points.push_back(x11); points.push_back(x21); points.push_back(x31); points.push_back(x41); Point subxy = Point(140, 9); cal_shift_roi(src1, src2, subxy, points); Mat dst1, dst2,src3,src4; cvtColor(src1, src3, COLOR_BGR2GRAY); //转换为灰度图像 src3.convertTo(dst1, CV_32FC1); //转换为32位浮点型 cvtColor(src2, src4, COLOR_BGR2GRAY); src4.convertTo(dst2, CV_32FC1); Point2d phase_shift; phase_shift = phaseCorrelate(dst1, dst2); cout << endl << "warp :" << endl << "tX shift : " << phase_shift.x << "tY shift : " << phase_shift.y << endl; //Draw_match(src1, src2, phase_shift.x, phase_shift.y); waitKey(0); } void Draw_match(Mat &src1, Mat &src2, int x_shift, int y_shift) { int col = src1.cols + src2.cols; int row = src1.rows; Mat finalsrc = Mat::zeros(Size(col, row), src1.type()); Mat roi_1 = finalsrc(Rect(0, 0, src1.cols, src1.rows)); Mat roi_2 = finalsrc(Rect(src1.cols, 0, src2.cols, src2.rows)); src1.copyTo(roi_1); src2.copyTo(roi_2); Point x1 = Point(200, 200); Point x2 = Point(250, 150); Point x3 = Point(350, 240); Point x4 = Point(440, 250); vector points; points.push_back(x1); points.push_back(x2); points.push_back(x3); points.push_back(x4); drawline(finalsrc, points,x_shift,y_shift,src1.cols); } void drawline(Mat & finsrc, vector pnums, int x_shift, int y_shift,int col) { int num = pnums.size(); for (int i = 0; i < num; i++) { Point Pt2 = pnums[i] + Point(x_shift, y_shift)+Point(col,0); circle(finsrc, pnums[i], 2, Scalar(255, 0, 0),2); circle(finsrc, Pt2, 2, Scalar(0, 255, 255),2); line(finsrc, pnums[i], Pt2, Scalar(0, 0, 255), 2); } imshow("final_result", finsrc); } void cal_shift_roi(Mat src1, Mat src2, Point subxy,vector pnums) { int Ps = pnums.size(); double add_x =0.0, add_y = 0.0; for (int i = 0; i < Ps; i++) { Mat dst1, dst2; Point x1 = pnums[i]; Point x2 = x1 - subxy; Mat src_roi1 = src1(Rect(x1, x1 + Point(60, 60))); Mat src_roi2 = src2(Rect(x2, x2 + Point(60, 60))); cvtColor(src_roi1, src_roi1, COLOR_BGR2GRAY); //转换为灰度图像 src_roi1.convertTo(dst1, CV_32FC1); //转换为32位浮点型 cvtColor(src_roi2, src_roi2, COLOR_BGR2GRAY); src_roi2.convertTo(dst2, CV_32FC1); Point2d phase_shift; phase_shift = phaseCorrelate(dst1, dst2); cout << i << " ge" << endl << "tX shift : " << phase_shift.x << "tY shift : " << phase_shift.y << endl; add_x += phase_shift.x; add_y += phase_shift.y; } double mid_x = add_x / Ps; double mid_y = add_y / Ps; double fin_x = subxy.x - mid_x; double fin_y = subxy.y - mid_y; cout << "pingjunzhiwei1: " << mid_x << " " << mid_y << endl; cout << "pingjunzuobiaowei1: " << fin_x << " " << fin_y << endl; }



