PageRenderTime 54ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/ot/fisheye.cpp

https://github.com/aiwenar/Eyes
C++ | 132 lines | 107 code | 20 blank | 5 comment | 10 complexity | 8e432e47d4ca7f2538e5e3c66ebe56ef MD5 | raw file
  1. #include <math.h>
  2. #include <unistd.h>
  3. #include <getopt.h>
  4. #include <iostream>
  5. #include <opencv2/opencv.hpp>
  6. #include <opencv2/contrib/contrib.hpp>
  7. #define mane main
  8. void sampleImage(const IplImage* arr, double idx0, double idx1, CvScalar& res)
  9. {
  10. if(idx0<0 || idx1<0 || idx0>(cvGetSize(arr).height-1) || idx1>(cvGetSize(arr).width-1)){
  11. res.val[0]=0;
  12. res.val[1]=0;
  13. res.val[2]=0;
  14. res.val[3]=0;
  15. return;
  16. }
  17. double idx0_fl=floor(idx0);
  18. double idx0_cl=ceil(idx0);
  19. double idx1_fl=floor(idx1);
  20. double idx1_cl=ceil(idx1);
  21. CvScalar s1=cvGet2D(arr,(int)idx0_fl,(int)idx1_fl);
  22. CvScalar s2=cvGet2D(arr,(int)idx0_fl,(int)idx1_cl);
  23. CvScalar s3=cvGet2D(arr,(int)idx0_cl,(int)idx1_cl);
  24. CvScalar s4=cvGet2D(arr,(int)idx0_cl,(int)idx1_fl);
  25. double x = idx0 - idx0_fl;
  26. double y = idx1 - idx1_fl;
  27. res.val[0]= s1.val[0]*(1-x)*(1-y) + s2.val[0]*(1-x)*y + s3.val[0]*x*y + s4.val[0]*x*(1-y);
  28. res.val[1]= s1.val[1]*(1-x)*(1-y) + s2.val[1]*(1-x)*y + s3.val[1]*x*y + s4.val[1]*x*(1-y);
  29. res.val[2]= s1.val[2]*(1-x)*(1-y) + s2.val[2]*(1-x)*y + s3.val[2]*x*y + s4.val[2]*x*(1-y);
  30. res.val[3]= s1.val[3]*(1-x)*(1-y) + s2.val[3]*(1-x)*y + s3.val[3]*x*y + s4.val[3]*x*(1-y);
  31. }
  32. double xscale;
  33. double yscale;
  34. double xshift;
  35. double yshift;
  36. double getRadialX(double x,double y,double cx,double cy,double k){
  37. x = (x*xscale+xshift);
  38. y = (y*yscale+yshift);
  39. double res = x+((x-cx)*k*((x-cx)*(x-cx)+(y-cy)*(y-cy)));
  40. return res;
  41. }
  42. double getRadialY(double x,double y,double cx,double cy,double k){
  43. x = (x*xscale+xshift);
  44. y = (y*yscale+yshift);
  45. double res = y+((y-cy)*k*((x-cx)*(x-cx)+(y-cy)*(y-cy)));
  46. return res;
  47. }
  48. double thresh = 1;
  49. double calc_shift(double x1,double x2,double cx,double k){
  50. double x3 = x1+(x2-x1)*0.5;
  51. double res1 = x1+((x1-cx)*k*((x1-cx)*(x1-cx)));
  52. double res3 = x3+((x3-cx)*k*((x3-cx)*(x3-cx)));
  53. // std::cerr<<"x1: "<<x1<<" - "<<res1<<" x3: "<<x3<<" - "<<res3<<std::endl;
  54. if(res1>-thresh and res1 < thresh)
  55. return x1;
  56. if(res3<0){
  57. return calc_shift(x3,x2,cx,k);
  58. }
  59. else{
  60. return calc_shift(x1,x3,cx,k);
  61. }
  62. }
  63. int mane(int argc, char** argv)
  64. {
  65. IplImage* src = cvLoadImage( argv[1], 1 );
  66. IplImage* dst = cvCreateImage(cvGetSize(src),src->depth,src->nChannels);
  67. IplImage* dst2 = cvCreateImage(cvGetSize(src),src->depth,src->nChannels);
  68. double K=atof(argv[3]);
  69. double centerX=atoi(argv[4]);
  70. double centerY=atoi(argv[5]);
  71. int width = cvGetSize(src).width;
  72. int height = cvGetSize(src).height;
  73. xshift = calc_shift(0,centerX-1,centerX,K);
  74. double newcenterX = width-centerX;
  75. double xshift_2 = calc_shift(0,newcenterX-1,newcenterX,K);
  76. yshift = calc_shift(0,centerY-1,centerY,K);
  77. double newcenterY = height-centerY;
  78. double yshift_2 = calc_shift(0,newcenterY-1,newcenterY,K);
  79. // scale = (centerX-xshift)/centerX;
  80. xscale = (width-xshift-xshift_2)/width;
  81. yscale = (height-yshift-yshift_2)/height;
  82. //std::cerr<<xshift<<" "<<yshift<<" "<<xscale<<" "<<yscale<<std::endl;
  83. //std::cerr<<cvGetSize(src).height<<std::endl;
  84. //std::cerr<<cvGetSize(src).width<<std::endl;
  85. for(int j=0;j<cvGetSize(dst).height;j++){
  86. for(int i=0;i<cvGetSize(dst).width;i++){
  87. CvScalar s;
  88. double x = getRadialX((double)i,(double)j,centerX,centerY,K);
  89. double y = getRadialY((double)i,(double)j,centerX,centerY,K);
  90. sampleImage(src,y,x,s);
  91. cvSet2D(dst,j,i,s);
  92. }
  93. }
  94. #if 0
  95. cvNamedWindow( "Source1", 1 );
  96. cvShowImage( "Source1", dst);
  97. cvWaitKey(0);
  98. #endif
  99. cvSaveImage(argv[2],dst,0);
  100. #if 0
  101. for(int j=0;j<cvGetSize(src).height;j++){
  102. for(int i=0;i<cvGetSize(src).width;i++){
  103. CvScalar s;
  104. sampleImage(src,j+0.25,i+0.25,s);
  105. cvSet2D(dst,j,i,s);
  106. }
  107. }
  108. cvNamedWindow( "Source1", 1 );
  109. cvShowImage( "Source1", src);
  110. cvWaitKey(0);
  111. #endif
  112. }