polar_transforms.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "opencv2/imgproc.hpp"
  2. #include "opencv2/highgui.hpp"
  3. #include "opencv2/videoio.hpp"
  4. #include <iostream>
  5. using namespace cv;
  6. int main( int argc, char** argv )
  7. {
  8. VideoCapture capture;
  9. Mat log_polar_img, lin_polar_img, recovered_log_polar, recovered_lin_polar_img;
  10. CommandLineParser parser(argc, argv, "{@input|0| camera device number or video file path}");
  11. parser.about("\nThis program illustrates usage of Linear-Polar and Log-Polar image transforms\n");
  12. parser.printMessage();
  13. std::string arg = parser.get<std::string>("@input");
  14. if( arg.size() == 1 && isdigit(arg[0]) )
  15. capture.open( arg[0] - '0' );
  16. else
  17. capture.open(samples::findFileOrKeep(arg));
  18. if( !capture.isOpened() )
  19. {
  20. fprintf(stderr,"Could not initialize capturing...\n");
  21. return -1;
  22. }
  23. namedWindow( "Linear-Polar", WINDOW_AUTOSIZE );
  24. namedWindow( "Log-Polar", WINDOW_AUTOSIZE);
  25. namedWindow( "Recovered Linear-Polar", WINDOW_AUTOSIZE);
  26. namedWindow( "Recovered Log-Polar", WINDOW_AUTOSIZE);
  27. moveWindow( "Linear-Polar", 20,20 );
  28. moveWindow( "Log-Polar", 700,20 );
  29. moveWindow( "Recovered Linear-Polar", 20, 350 );
  30. moveWindow( "Recovered Log-Polar", 700, 350 );
  31. int flags = INTER_LINEAR + WARP_FILL_OUTLIERS;
  32. Mat src;
  33. for(;;)
  34. {
  35. capture >> src;
  36. if(src.empty() )
  37. break;
  38. Point2f center( (float)src.cols / 2, (float)src.rows / 2 );
  39. double maxRadius = 0.7*min(center.y, center.x);
  40. #if 0 //deprecated
  41. double M = frame.cols / log(maxRadius);
  42. logPolar(frame, log_polar_img, center, M, flags);
  43. linearPolar(frame, lin_polar_img, center, maxRadius, flags);
  44. logPolar(log_polar_img, recovered_log_polar, center, M, flags + WARP_INVERSE_MAP);
  45. linearPolar(lin_polar_img, recovered_lin_polar_img, center, maxRadius, flags + WARP_INVERSE_MAP);
  46. #endif
  47. //! [InverseMap]
  48. // direct transform
  49. warpPolar(src, lin_polar_img, Size(),center, maxRadius, flags); // linear Polar
  50. warpPolar(src, log_polar_img, Size(),center, maxRadius, flags + WARP_POLAR_LOG); // semilog Polar
  51. // inverse transform
  52. warpPolar(lin_polar_img, recovered_lin_polar_img, src.size(), center, maxRadius, flags + WARP_INVERSE_MAP);
  53. warpPolar(log_polar_img, recovered_log_polar, src.size(), center, maxRadius, flags + WARP_POLAR_LOG + WARP_INVERSE_MAP);
  54. //! [InverseMap]
  55. // Below is the reverse transformation for (rho, phi)->(x, y) :
  56. Mat dst;
  57. if (flags & WARP_POLAR_LOG)
  58. dst = log_polar_img;
  59. else
  60. dst = lin_polar_img;
  61. //get a point from the polar image
  62. int rho = cvRound(dst.cols * 0.75);
  63. int phi = cvRound(dst.rows / 2.0);
  64. //! [InverseCoordinate]
  65. double angleRad, magnitude;
  66. double Kangle = dst.rows / CV_2PI;
  67. angleRad = phi / Kangle;
  68. if (flags & WARP_POLAR_LOG)
  69. {
  70. double Klog = dst.cols / std::log(maxRadius);
  71. magnitude = std::exp(rho / Klog);
  72. }
  73. else
  74. {
  75. double Klin = dst.cols / maxRadius;
  76. magnitude = rho / Klin;
  77. }
  78. int x = cvRound(center.x + magnitude * cos(angleRad));
  79. int y = cvRound(center.y + magnitude * sin(angleRad));
  80. //! [InverseCoordinate]
  81. drawMarker(src, Point(x, y), Scalar(0, 255, 0));
  82. drawMarker(dst, Point(rho, phi), Scalar(0, 255, 0));
  83. imshow("Src frame", src);
  84. imshow("Log-Polar", log_polar_img);
  85. imshow("Linear-Polar", lin_polar_img);
  86. imshow("Recovered Linear-Polar", recovered_lin_polar_img );
  87. imshow("Recovered Log-Polar", recovered_log_polar );
  88. if( waitKey(10) >= 0 )
  89. break;
  90. }
  91. return 0;
  92. }