Bird eye view homography

asked 2014-10-07 07:15:29 -0500

zweistein666 gravatar image

I I have openCV 3.0 and I'm trying the bird eye view example (modified) from OReilly Learning OpenCV book, example 12.1.

Consider that I did the calibration separately and saved in the .xml files. I would like to have a bird eye view of a road, but the camera is mounted too high and the chessboard can't be seen well on the image. So, I would like to use the pained withe rectangles on the ground (I know their dimensions and relative position from each other).

Yet not calibrated image: image description

How should I set up the objPts and imgPts coordinates in order to achieve a bird eye view?

The modified code:

#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv2/calib3d/calib3d_c.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>

int main(int argc, char* argv[]) {

  IplImage* image = 0;    
  image = cvLoadImage("img/img1.jpg");
  cvShowImage( "Original", image );
  CvMat *intrinsic = (CvMat*)cvLoad("Intrinsics.xml");
  CvMat *distortion = (CvMat*)cvLoad("Distortion.xml");
  // Build the undistort map that we will use for all
  // subsequent frames.
  //
  IplImage* mapx = cvCreateImage( cvGetSize(image), IPL_DEPTH_32F, 1 );
  IplImage* mapy = cvCreateImage( cvGetSize(image), IPL_DEPTH_32F, 1 );
  cvInitUndistortMap(
      intrinsic,
      distortion,
      mapx,
      mapy
      );

  IplImage *t = cvCloneImage(image);  
  cvRemap( t, image, mapx, mapy ); // Undistort image
  cvReleaseImage(&t);
  cvShowImage( "Calibration", image ); // Show raw image
  // FIND THE HOMOGRAPHY
  //
  CvMat *H = cvCreateMat( 3, 3, CV_32F);

  // this parameters are the real size of a white rectangle on the bottom of the image
  CvPoint2D32f objPts[4], imgPts[4];
  objPts[0].x = 0; objPts[0].y = 0; 
  objPts[1].x = 0.5; objPts[1].y = 0;
  objPts[2].x = 0; objPts[2].y = 2.8;
  objPts[3].x = 0.5; objPts[3].y = 2.8;

  // this is the pixel coordinates of one of the rectangles 
  imgPts[0].x = 0; imgPts[0].y = 0;
  imgPts[1].x = 61; imgPts[1].y = 0;
  imgPts[2].x = 23; imgPts[2].y = 80;
  imgPts[3].x = 112; imgPts[3].y = 79;

  cvGetPerspectiveTransform(objPts, imgPts, H);
  // LET THE USER ADJUST THE Z HEIGHT OF THE VIEW
  //
  float Z = 25;
  int key = 0;
  IplImage *birds_image = cvCloneImage(image);
  cvNamedWindow("Birds_Eye");
  // LOOP TO ALLOW USER TO PLAY WITH HEIGHT:
  //
  // escape key stops
  //
  while(key != 27) {
      // Set the height
      //
      CV_MAT_ELEM(*H,float,2,2) = Z;
      // COMPUTE THE FRONTAL PARALLEL OR BIRD’S-EYE VIEW:
      // USING HOMOGRAPHY TO REMAP THE VIEW
      //
      cvWarpPerspective(
          image,
          birds_image,
          H,
          CV_INTER_LINEAR | CV_WARP_INVERSE_MAP |

CV_WARP_FILL_OUTLIERS ); cvShowImage( "Birds_Eye", birds_image );

      key = cvWaitKey();
      if(key == 'u') Z += 0.5;
      if(key == 'd') Z -= 0.5;
  }
  cvSave("H.xml",H); //We can reuse H for the same camera mounting
  return 0;
}

The resulted image is: image description

Moreover, in case I try to modify the height H(2,2)=z than the result is only scaleing the image, but not hing else.

edit retag flag offensive close merge delete