1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| vector<Point2f> Conners(4);// 左上角 左下角 右上角 右下角
void calcCorners(const Mat& homo,const Mat& input) { double v2[]={0,0,1}; double v1[3];//改变后值 Mat V2=Mat(3,1,CV_64FC1,v2); Mat V1=Mat(3,1,CV_64FC1,v1); //下面都是重复代码计算看懂一个即可 V1=homo*V2; Conners[0].x=v1[0]/v1[2]; Conners[0].y=v1[1]/v1[2];
v2[0]=0; v2[1]=input.rows; v2[2]=1; V2=Mat(3,1,CV_64FC1,v2); V1=Mat(3,1,CV_64FC1,v1); V1=homo*V2; Conners[1].x=v1[0]/v1[2]; Conners[1].y=v1[1]/v1[2];
v2[0]=input.cols; v2[1]=0; v2[2]=1; V2=Mat(3,1,CV_64FC1,v2); V1=Mat(3,1,CV_64FC1,v1); V1=homo*V2; Conners[2].x=v1[0]/v1[2]; Conners[2].y=v1[1]/v1[2];
v2[0]=input.cols; v2[1]=input.rows; v2[2]=1; V2=Mat(3,1,CV_64FC1,v2); V1=Mat(3,1,CV_64FC1,v1); V1=homo*V2; Conners[3].x=v1[0]/v1[2]; Conners[3].y=v1[1]/v1[2];
}
main:://此处接着标题二的代码
vector<Point2f> image_points1,image_points2;
for(auto x:goodMatchPoints) { image_points2.push_back(KeyPoints2[x.queryIdx].pt); image_points1.push_back(KeyPoints1[x.trainIdx].pt); }
Mat homo=findHomography(image_points1,image_points2,CV_RANSAC);
cout<<"homo matrix:"<<homo<<endl;
calcCorners(homo,image_src1); Mat imageTransform1,imageTransform2; warpPerspective(image_src1,imageTransform1,homo,Size(max(Conners[2].x, Conners[3].x),image_src2.rows)); imshow("12",imageTransform1);
int dst_width=imageTransform1.cols; int dst_height=image_src2.rows;
Mat dst(dst_height,dst_width,CV_8UC3); dst.setTo(0); imageTransform1.copyTo(dst(Rect(0,0,imageTransform1.cols,imageTransform1.rows)));
image_src2.copyTo(dst(Rect(0,0,image_src2.cols,image_src2.rows))); imshow("pin",dst);
|