warpPerspective_demo.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. @file warpPerspective_demo.cpp
  3. @brief a demo program shows how perspective transformation applied on an image
  4. @based on a sample code http://study.marearts.com/2015/03/image-warping-using-opencv.html
  5. @modified by Suleyman TURKMEN
  6. */
  7. #include "opencv2/imgproc.hpp"
  8. #include "opencv2/imgcodecs.hpp"
  9. #include "opencv2/highgui.hpp"
  10. #include <iostream>
  11. using namespace std;
  12. using namespace cv;
  13. static void help(char** argv)
  14. {
  15. // print a welcome message, and the OpenCV version
  16. cout << "\nThis is a demo program shows how perspective transformation applied on an image, \n"
  17. "Using OpenCV version " << CV_VERSION << endl;
  18. cout << "\nUsage:\n" << argv[0] << " [image_name -- Default right.jpg]\n" << endl;
  19. cout << "\nHot keys: \n"
  20. "\tESC, q - quit the program\n"
  21. "\tr - change order of points to rotate transformation\n"
  22. "\tc - delete selected points\n"
  23. "\ti - change order of points to inverse transformation \n"
  24. "\nUse your mouse to select a point and move it to see transformation changes" << endl;
  25. }
  26. static void onMouse(int event, int x, int y, int, void*);
  27. Mat warping(Mat image, Size warped_image_size, vector< Point2f> srcPoints, vector< Point2f> dstPoints);
  28. String windowTitle = "Perspective Transformation Demo";
  29. String labels[4] = { "TL","TR","BR","BL" };
  30. vector< Point2f> roi_corners;
  31. vector< Point2f> midpoints(4);
  32. vector< Point2f> dst_corners(4);
  33. int roiIndex = 0;
  34. bool dragging;
  35. int selected_corner_index = 0;
  36. bool validation_needed = true;
  37. int main(int argc, char** argv)
  38. {
  39. help(argv);
  40. CommandLineParser parser(argc, argv, "{@input| right.jpg |}");
  41. string filename = samples::findFile(parser.get<string>("@input"));
  42. Mat original_image = imread( filename );
  43. Mat image;
  44. float original_image_cols = (float)original_image.cols;
  45. float original_image_rows = (float)original_image.rows;
  46. roi_corners.push_back(Point2f( (float)(original_image_cols / 1.70), (float)(original_image_rows / 4.20) ));
  47. roi_corners.push_back(Point2f( (float)(original_image.cols / 1.15), (float)(original_image.rows / 3.32) ));
  48. roi_corners.push_back(Point2f( (float)(original_image.cols / 1.33), (float)(original_image.rows / 1.10) ));
  49. roi_corners.push_back(Point2f( (float)(original_image.cols / 1.93), (float)(original_image.rows / 1.36) ));
  50. namedWindow(windowTitle, WINDOW_NORMAL);
  51. namedWindow("Warped Image", WINDOW_AUTOSIZE);
  52. moveWindow("Warped Image", 20, 20);
  53. moveWindow(windowTitle, 330, 20);
  54. setMouseCallback(windowTitle, onMouse, 0);
  55. bool endProgram = false;
  56. while (!endProgram)
  57. {
  58. if ( validation_needed & (roi_corners.size() < 4) )
  59. {
  60. validation_needed = false;
  61. image = original_image.clone();
  62. for (size_t i = 0; i < roi_corners.size(); ++i)
  63. {
  64. circle( image, roi_corners[i], 5, Scalar(0, 255, 0), 3 );
  65. if( i > 0 )
  66. {
  67. line(image, roi_corners[i-1], roi_corners[(i)], Scalar(0, 0, 255), 2);
  68. circle(image, roi_corners[i], 5, Scalar(0, 255, 0), 3);
  69. putText(image, labels[i].c_str(), roi_corners[i], FONT_HERSHEY_SIMPLEX, 0.8, Scalar(255, 0, 0), 2);
  70. }
  71. }
  72. imshow( windowTitle, image );
  73. }
  74. if ( validation_needed & ( roi_corners.size() == 4 ))
  75. {
  76. image = original_image.clone();
  77. for ( int i = 0; i < 4; ++i )
  78. {
  79. line(image, roi_corners[i], roi_corners[(i + 1) % 4], Scalar(0, 0, 255), 2);
  80. circle(image, roi_corners[i], 5, Scalar(0, 255, 0), 3);
  81. putText(image, labels[i].c_str(), roi_corners[i], FONT_HERSHEY_SIMPLEX, 0.8, Scalar(255, 0, 0), 2);
  82. }
  83. imshow( windowTitle, image );
  84. midpoints[0] = (roi_corners[0] + roi_corners[1]) / 2;
  85. midpoints[1] = (roi_corners[1] + roi_corners[2]) / 2;
  86. midpoints[2] = (roi_corners[2] + roi_corners[3]) / 2;
  87. midpoints[3] = (roi_corners[3] + roi_corners[0]) / 2;
  88. dst_corners[0].x = 0;
  89. dst_corners[0].y = 0;
  90. dst_corners[1].x = (float)norm(midpoints[1] - midpoints[3]);
  91. dst_corners[1].y = 0;
  92. dst_corners[2].x = dst_corners[1].x;
  93. dst_corners[2].y = (float)norm(midpoints[0] - midpoints[2]);
  94. dst_corners[3].x = 0;
  95. dst_corners[3].y = dst_corners[2].y;
  96. Size warped_image_size = Size(cvRound(dst_corners[2].x), cvRound(dst_corners[2].y));
  97. Mat M = getPerspectiveTransform(roi_corners, dst_corners);
  98. Mat warped_image;
  99. warpPerspective(original_image, warped_image, M, warped_image_size); // do perspective transformation
  100. imshow("Warped Image", warped_image);
  101. }
  102. char c = (char)waitKey( 10 );
  103. if ((c == 'q') | (c == 'Q') | (c == 27))
  104. {
  105. endProgram = true;
  106. }
  107. if ((c == 'c') | (c == 'C'))
  108. {
  109. roi_corners.clear();
  110. }
  111. if ((c == 'r') | (c == 'R'))
  112. {
  113. roi_corners.push_back(roi_corners[0]);
  114. roi_corners.erase(roi_corners.begin());
  115. }
  116. if ((c == 'i') | (c == 'I'))
  117. {
  118. swap(roi_corners[0], roi_corners[1]);
  119. swap(roi_corners[2], roi_corners[3]);
  120. }
  121. }
  122. return 0;
  123. }
  124. static void onMouse(int event, int x, int y, int, void*)
  125. {
  126. // Action when left button is pressed
  127. if (roi_corners.size() == 4)
  128. {
  129. for (int i = 0; i < 4; ++i)
  130. {
  131. if ((event == EVENT_LBUTTONDOWN) & ((abs(roi_corners[i].x - x) < 10)) & (abs(roi_corners[i].y - y) < 10))
  132. {
  133. selected_corner_index = i;
  134. dragging = true;
  135. }
  136. }
  137. }
  138. else if ( event == EVENT_LBUTTONDOWN )
  139. {
  140. roi_corners.push_back( Point2f( (float) x, (float) y ) );
  141. validation_needed = true;
  142. }
  143. // Action when left button is released
  144. if (event == EVENT_LBUTTONUP)
  145. {
  146. dragging = false;
  147. }
  148. // Action when left button is pressed and mouse has moved over the window
  149. if ((event == EVENT_MOUSEMOVE) && dragging)
  150. {
  151. roi_corners[selected_corner_index].x = (float) x;
  152. roi_corners[selected_corner_index].y = (float) y;
  153. validation_needed = true;
  154. }
  155. }