Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <opencv2/core/core.hpp>
  3. #include <opencv2/imgproc/imgproc.hpp>
  4. #include <opencv2/calib3d/calib3d.hpp>
  5. #include <opencv2/highgui/highgui.hpp>
  6.  
  7. using namespace std;
  8. using namespace cv;
  9.  
  10. const double PI = 3.141592653589793;
  11. const string PATH_IMAGE = "/Users/Kenza/Desktop/Xcode_cpp_opencv/PaulBourke2/PaulBourke2/Images/img1.jpg";
  12. const int ESC = 27;
  13.  
  14. Point2f findCorrespondingFisheyePoint(int Xe, int Ye, double He, double We, double Hf, double Wf, double FOV){
  15. Point2f fisheyePoint;
  16. double Xfn, Yfn; //Normalized Cartesian Coordinates
  17. double longitude, latitude, Px, Py, Pz; //Spherical Coordinates
  18. double r, theta; //Polar coordinates
  19. double Xpn, Ypn; //Normalized Polar coordinates
  20.  
  21. //Normalize Coordinates
  22. Xfn = ( ( 2.0 * (double)Xe ) - We) / Wf;//Between -1 and 1
  23. Yfn = ( ( 2.0 * (double)Ye ) - He) / Hf;//Between -1 and 1
  24.  
  25. //Normalize Coordinates to Spherical Coordinates
  26. longitude = Xfn*PI; //Between -PI and PI (2*PI interval)
  27. latitude = Yfn*(PI/2.0); //Between -PI/2 and PI/2 (PI interval)
  28. Px = cos(latitude)*cos(longitude);
  29. Py = cos(latitude)*sin(longitude);
  30. Pz = sin(latitude);
  31.  
  32. //Spherical Coordinates to Polar Coordinates
  33. r = 2.0 * atan2(sqrt(pow(Px,2)+pow(Pz,2)),Py)/FOV;
  34. theta = atan2(Pz,-Px);
  35. Xpn = r * cos(theta);
  36. Ypn = r * sin(theta);
  37.  
  38. //Normalize Coordinates to CartesianImage Coordinates
  39. fisheyePoint.x = (int)(((Xpn+1.0)*Wf)/2.0);
  40. fisheyePoint.y = (int)(((Ypn+1.0)*Hf)/2.0);
  41.  
  42. return fisheyePoint;
  43. }
  44.  
  45. int main(int argc, char** argv){
  46.  
  47. Mat fisheyeImage, equirectangularImage;
  48.  
  49. fisheyeImage = imread(PATH_IMAGE, CV_32FC1);
  50. namedWindow("Fisheye Image", WINDOW_AUTOSIZE);
  51. imshow("Fisheye Image", fisheyeImage);
  52.  
  53. while(waitKey(0) != ESC) {
  54. //wait until the key ESC is pressed
  55. }
  56.  
  57. //destroyWindow("Fisheye Image");
  58.  
  59. int Hf, Wf; //Height, width and FOV for the input image (=fisheyeImage)
  60. double FOV;
  61. int He, We; //Height and width for the outpout image (=EquirectangularImage)
  62.  
  63. Hf = fisheyeImage.size().height;
  64. Wf = fisheyeImage.size().width;
  65. FOV = PI; //FOV in radian
  66.  
  67. //We keep the same ratio for the image input and the image output
  68. We = Wf;
  69. He = Hf;
  70.  
  71. equirectangularImage.create(Hf, Wf, fisheyeImage.type()); //We create the outpout image (=EquirectangularImage)
  72.  
  73. //For each pixels of the ouput equirectangular Image
  74. for (int Xe = 0; Xe <equirectangularImage.size().width; Xe++){
  75. for (int Ye = 0; Ye <equirectangularImage.size().height; Ye++){
  76.  
  77. equirectangularImage.at<Vec3b>(Point(Xe,Ye)) = fisheyeImage.at<Vec3b>(findCorrespondingFisheyePoint(Xe, Ye, He, We, Hf, Wf, FOV)); //We find the corresponding point in the fisheyeImage
  78. }
  79. }
  80.  
  81. namedWindow("Equirectangular Image", WINDOW_AUTOSIZE);
  82. imshow("Equirectangular Image",equirectangularImage);
  83.  
  84. while(waitKey(0) != ESC) {
  85. //wait until the key ESC is pressed
  86. }
  87.  
  88. destroyWindow("Fisheye Image");
  89.  
  90. imwrite("equirectangularImage.jpg", equirectangularImage);
  91.  
  92. return 0;
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement