一. 图像矫正
1 #include <opencv2/opencv.hpp> 2 using namespace cv; 3 using namespace std; 4 5 struct userdata{ 6 Mat im; 7 vector<Point2f> points; 8 }; 9 10 void mouseHandler(int event, int x, int y, int flags, void* data_ptr) 11 { 12 if ( event == EVENT_LBUTTONDOWN ) 13 { 14 userdata *data = ((userdata *) data_ptr); 15 circle(data->im, Point(x,y),3,Scalar(0,0,255), 5, CV_AA); 16 imshow("Image", data->im); 17 if (data->points.size() < 4) 18 { 19 data->points.push_back(Point2f(x,y)); 20 } 21 } 22 23 } 24 25 26 27 int main() 28 { 29 30 // Read source image. 31 Mat im_src = imread("1.png"); 32 33 // Destination image. The aspect ratio of the book is 3/4 34 Size size(300,400); 35 Mat im_dst = Mat::zeros(size,CV_8UC3); 36 37 38 // Create a vector of destination points. 39 vector<Point2f> pts_dst; 40 41 pts_dst.push_back(Point2f(0,0)); 42 pts_dst.push_back(Point2f(size.width - 1, 0)); 43 pts_dst.push_back(Point2f(size.width - 1, size.height -1)); 44 pts_dst.push_back(Point2f(0, size.height - 1 )); 45 46 // Set data for mouse event 47 Mat im_temp = im_src.clone(); 48 userdata data; 49 data.im = im_temp; 50 51 cout << "Click on the four corners of the book -- top left first and" << endl 52 << "bottom left last -- and then hit ENTER" << endl; 53 54 // Show image and wait for 4 clicks. 55 imshow("Image", im_temp); 56 // Set the callback function for any mouse event 57 setMouseCallback("Image", mouseHandler, &data); 58 waitKey(0); 59 60 // Calculate the homography 61 Mat h = findHomography(data.points, pts_dst); 62 63 // Warp source image to destination 64 warpPerspective(im_src, im_dst, h, size); 65 66 // Show image 67 imshow("Image", im_dst); 68 waitKey(0); 69 return 0; 70 71 72 }
二.虚拟广告牌
1 #include <opencv2/opencv.hpp> 2 3 using namespace cv; 4 using namespace std; 5 6 struct userdata{ 7 Mat im; 8 vector<Point2f> points; 9 }; 10 11 12 void mouseHandler(int event, int x, int y, int flags, void* data_ptr) 13 { 14 if ( event == EVENT_LBUTTONDOWN ) 15 { 16 userdata *data = ((userdata *) data_ptr); 17 circle(data->im, Point(x,y),3,Scalar(0,255,255), 5, CV_AA); 18 imshow("Image", data->im); 19 if (data->points.size() < 4) 20 { 21 data->points.push_back(Point2f(x,y)); 22 } 23 } 24 25 } 26 27 28 29 int main( int argc, char** argv) 30 { 31 32 // Read in the image. 33 Mat im_src = imread("2.jpeg"); 34 Size size = im_src.size(); 35 36 // Create a vector of points. 37 vector<Point2f> pts_src; 38 pts_src.push_back(Point2f(0,0)); 39 pts_src.push_back(Point2f(size.width - 1, 0)); 40 pts_src.push_back(Point2f(size.width - 1, size.height -1)); 41 pts_src.push_back(Point2f(0, size.height - 1 )); 42 43 44 45 // Destination image 46 Mat im_dst = imread("3.jpeg"); 47 48 49 // Set data for mouse handler 50 Mat im_temp = im_dst.clone(); 51 userdata data; 52 data.im = im_temp; 53 54 55 //show the image 56 imshow("Image", im_temp); 57 58 cout << "Click on four corners of a billboard and then press ENTER" << endl; 59 //set the callback function for any mouse event 60 setMouseCallback("Image", mouseHandler, &data); 61 waitKey(0); 62 63 // Calculate Homography between source and destination points 64 Mat h = findHomography(pts_src, data.points); 65 66 // Warp source image 67 warpPerspective(im_src, im_temp, h, im_temp.size()); 68 69 // Extract four points from mouse data 70 Point pts_dst[4]; 71 for( int i = 0; i < 4; i++) 72 { 73 pts_dst[i] = data.points[i]; 74 } 75 76 // Black out polygonal area in destination image. 77 fillConvexPoly(im_dst, pts_dst, 4, Scalar(0), CV_AA); 78 79 // Add warped source image to destination image. 80 im_dst = im_dst + im_temp; 81 82 // Display image. 83 imshow("Image", im_dst); 84 waitKey(0); 85 86 return 0; 87 }
CMakelists.txt:
1 cmake_minimum_required( VERSION 2.8 ) 2 project( a ) 3 4 set( CMAKE_BUILD_TYPE Release ) 5 set( CMAKE_CXX_FLAGS "-std=c++11 -O3" ) 6 7 find_package( OpenCV ) 8 include_directories( ${OpenCV_INCLUDE_DIRS} ) 9 10 add_executable( a a.cpp ) 11 target_link_libraries( a ${OpenCV_LIBS} ) 12 13 add_executable( b b.cpp ) 14 target_link_libraries( b ${OpenCV_LIBS} )
图片下载:
图片1 图片2
图片3