코드 8-7 영상의 투시 변환 예제 [ch08/perspective]
01 #include "opencv2/opencv.hpp" 02 #include <iostream> 03 04 using namespace cv; 05 using namespace std; 06 07 Mat src; 08 Point2f srcQuad[4], dstQuad[4]; 09 10 void on_mouse(int event, int x, int y, int flags, void* userdata); 11 12 int main() 13 { 14 src = imread("card.bmp"); 15 16 if (src.empty()) { 17 cerr << "Image load failed!" << endl; 18 return -1; 19 } 20 21 namedWindow("src"); 22 setMouseCallback("src", on_mouse); 23 24 imshow("src", src); 25 waitKey(0); 26 27 return 0; 28 } 29 30 void on_mouse(int event, int x, int y, int flags, void*) 31 { 32 static int cnt = 0; 33 34 if (event = = EVENT_LBUTTONDOWN) { 35 if (cnt < 4) { 36 srcQuad[cnt++] = Point2f(x, y); 37 38 circle(src, Point(x, y), 5, Scalar(0, 0, 255), -1); 39 imshow("src", src); 40 41 if (cnt = = 4) { 42 int w = 200, h = 300; 43 44 dstQuad[0] = Point2f(0, 0); 45 dstQuad[1] = Point2f(w - 1, 0); 46 dstQuad[2] = Point2f(w - 1, h - 1); 47 dstQuad[3] = Point2f(0, h - 1); 48 49 Mat pers = getPerspectiveTransform(srcQuad, dstQuad); 50 51 Mat dst; 52 warpPerspective(src, dst, pers, Size(w, h)); 53 54 imshow("dst", dst); 55 } 56 } 57 } 58 }