Guest User

Untitled

a guest
Oct 18th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.27 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <cv.h>
  5. #include <highgui.h>
  6.  
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10.   IplImage* img = 0;
  11.   IplImage* world_img = 0;
  12.   FILE* pfile;
  13.  
  14.   int i,j,k;
  15.   double xi[4];
  16.   double yi[4];
  17.   double xw[4];
  18.   double yw[4];
  19.  
  20.  
  21.   if(argc<3){
  22.     printf("Usage: main <image-file-name> <coordinates-file>\n\7");
  23.     exit(0);
  24.   }
  25.  
  26.   // load an image  
  27.   img=cvLoadImage(argv[1]);
  28.   if(!img){
  29.     printf("Could not load image file: %s\n",argv[1]);
  30.     exit(0);
  31.   }
  32.  
  33.   // read coordinates
  34.   pfile = fopen(argv[2], "r");
  35.  
  36.   for (i=0;i<4;i++)
  37.   {
  38.       fscanf(pfile,"%lf",&xi[i]);
  39.       fscanf(pfile,"%lf",&yi[i]);
  40.   }
  41.   for (i=0;i<4;i++)
  42.   {
  43.       fscanf(pfile,"%lf",&xw[i]);
  44.       fscanf(pfile,"%lf",&yw[i]);
  45.   }
  46.    
  47.     // fill up the matrix for world coordinates
  48.     double wc[8];
  49.     for (i=0;i<4;i++)
  50.     {
  51.     wc[2*i] = xw[i];
  52.     wc[2*i+1] = yw[i];
  53.     }
  54.    
  55.     CvMat WC;
  56.     cvInitMatHeader(&WC, 8, 1, CV_64FC1, wc, CV_AUTOSTEP);
  57.  
  58.     // fill up the matrix equation
  59.     double m[64];  
  60.     for (i=0;i<4;i++)
  61.     {
  62.     m[i*2*8+0] = xi[i];
  63.     m[i*2*8+1] = yi[i];
  64.     m[i*2*8+2] = 1;
  65.     m[i*2*8+3] = 0;
  66.     m[i*2*8+4] = 0;
  67.     m[i*2*8+5] = 0;
  68.     m[i*2*8+6] = -xw[i]*xi[i];
  69.     m[i*2*8+7] = -xw[i]*yi[i];
  70.    
  71.     m[(i*2+1)*8+0] = 0;
  72.     m[(i*2+1)*8+1] = 0;
  73.     m[(i*2+1)*8+2] = 0;
  74.     m[(i*2+1)*8+3] = xi[i];
  75.     m[(i*2+1)*8+4] = yi[i];
  76.     m[(i*2+1)*8+5] = 1;
  77.     m[(i*2+1)*8+6] = -yw[i]*xi[i];
  78.     m[(i*2+1)*8+7] = -yw[i]*yi[i];
  79.     }
  80.  
  81.     CvMat M;
  82.     cvInitMatHeader(&M, 8, 8, CV_64FC1, m, CV_AUTOSTEP);
  83.    
  84.     //solve the equation
  85.     CvMat *ans = cvCreateMat(8, 1, CV_64FC1);
  86.     cvSolve(&M, &WC, ans, CV_LU);
  87.    
  88.     //get H and H_inv
  89.     CvMat *H = cvCreateMat(3, 3, CV_64FC1);
  90.     CvMat *H_inv = cvCreateMat(3, 3, CV_64FC1);    
  91.     for (i=0;i<8;i++)
  92.     {
  93.     cvmSet(H_inv, i/3, i%3, cvmGet(ans, i, 0));
  94.     }
  95.     cvmSet(H_inv, 2, 2, 1.0);
  96.     cvInvert(H_inv, H, CV_LU);
  97.    
  98.    
  99.     // determine the world boundry
  100.     double img_bound[] = {
  101.     0, img->width-1, 0, img->width-1,
  102.     0, 0, img->height-1, img->height-1,
  103.     1,1,1,1};
  104.     CvMat *m_imgb = cvCreateMat(3, 4, CV_64FC1);
  105.     cvInitMatHeader(m_imgb, 3, 4, CV_64FC1, img_bound, CV_AUTOSTEP);
  106.    
  107.     CvMat *m_worldb = cvCreateMat(3, 4, CV_64FC1);
  108.     cvMatMul(H_inv, m_imgb, m_worldb);
  109.    
  110.     double xmin = 10000;
  111.     double xmax = 0;
  112.     double ymin = 10000;
  113.     double ymax = 0;
  114.    
  115.     for (i=0;i<4;i++)
  116.     {
  117.     double x = cvmGet(m_worldb, 0, i)/cvmGet(m_worldb, 2, i);
  118.     double y = cvmGet(m_worldb, 1, i)/cvmGet(m_worldb, 2, i);
  119.     if (x<xmin) xmin = x;
  120.     if (x>xmax) xmax = x;
  121.     if (y<ymin) ymin = y;
  122.     if (y>ymax) ymax = y;
  123.     }
  124.    
  125.     //printf("%f %f %f %f \n", xmin, xmax, ymin, ymax);
  126.    
  127.     //creat world image
  128.     double scale = ((double)img->width)/(xmax - xmin);
  129.     int world_height = (int) ((ymax - ymin) * scale);
  130.     world_img = cvCreateImage(cvSize(img->width, world_height), IPL_DEPTH_8U, 3);
  131.     cvZero(world_img);
  132.    
  133.     //generate the world image
  134.     double step = 1.0/scale;
  135.     CvMat* point_w = cvCreateMat(3,1, CV_64FC1);
  136.     CvMat* point_i = cvCreateMat(3,1, CV_64FC1);
  137.     cvmSet(point_w, 2, 0, 1.0);
  138.    
  139.     for (i=0; i<world_img->width; i++)
  140.     {
  141.     cvmSet(point_w, 0, 0, xmin+((double)i)*step);
  142.     for(j=0; j<world_img->height; j++)
  143.     {
  144.         cvmSet(point_w, 1, 0, ymin+((double)j)*step);
  145.         cvMatMul(H, point_w, point_i);
  146.         double xi = cvmGet(point_i, 0, 0)/cvmGet(point_i, 2, 0);
  147.         double yi = cvmGet(point_i, 1, 0)/cvmGet(point_i, 2, 0);
  148.        
  149.         //out of bound
  150.         if (xi<0 || yi<0) continue;
  151.         if (xi>=img->width-1 || yi>=img->height-1) continue;
  152.        
  153.         for (k=0;k<3;k++)
  154.         {
  155.         double value = ((uchar *)(img->imageData + img->widthStep*(int)yi))[(int)xi*3+k];
  156.         ((uchar *)(world_img->imageData + world_img->widthStep*j))[i*3+k] = value;
  157.         }
  158.     }
  159.     }
  160.    
  161.     //save world img
  162.     char str[256];
  163.     sprintf(str, "%s-world.png",argv[1]);
  164.     cvSaveImage(str, world_img);
  165.    
  166.     // release the image and matrix
  167.     cvReleaseImage(&img);
  168.     cvReleaseImage(&world_img);
  169.     cvReleaseMat(&ans);
  170.     cvReleaseMat(&point_i);
  171.     cvReleaseMat(&point_w);
  172.     cvReleaseMat(&H);
  173.     cvReleaseMat(&H_inv);
  174.     cvReleaseMat(&m_worldb);
  175.     cvReleaseMat(&m_imgb);
  176.     return 0;
  177. }
Add Comment
Please, Sign In to add comment