daily pastebin goal
53%
SHARE
TWEET

Untitled

a guest Mar 13th, 2018 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <opencv2/opencv.hpp>
  2.  
  3. using namespace cv;
  4. using namespace std;
  5.  
  6. int main(int argc, char **argv)
  7. {
  8.  
  9.     //This is camera matrix and dist_coeffs , you will get in camera calibration.(copy from intrinsic.xml)
  10.     Mat camera_matrix = (Mat_<double>(3,3) <<  1.2195112968898779e+003, 0., 3.6448211117862780e+002, 0.,
  11.                                                         1.2414409169216196e+003, 2.4321803868732076e+002, 0., 0., 1.);
  12.     Mat dist_coeffs = Mat::zeros(4,1,DataType<double>::type); // Assuming no lens distortion
  13.  
  14.     vector<Point2f>corners1;// for chessboard pattern
  15.     Mat rotation_vector; // Rotation in axis-angle form
  16.     Mat translation_vector;
  17.  
  18.   string filename = "withChessBoard.MOV";
  19.     VideoCapture capture(filename);
  20.     Mat view;
  21.     Mat Img;
  22.  
  23.     vector<Point3f>  objectPoints;
  24.  
  25.     const string x= "X";
  26.     const string y= "Y";
  27.     const string z= "Z";
  28.     bool found=false;
  29.     vector<Point3d> point3D;
  30.   vector<Point2d> point2D;
  31.  
  32.     int ChessboardPatternWidth=7;//Horrizonal Number of internal corners of pattern //change it accrounding to your pattern
  33.     int ChessboardPatternHight=7;//vertical Number of internal corners of pattern //change it accrounding to your pattern
  34.     Size patternSize(ChessboardPatternWidth,ChessboardPatternHight);
  35.     float BoardBoxSize=4;//distance between 2 correns //change it accrounding to your pattern . megger it in cm or mm.
  36.                          //your unit of meggerment will consider as object point units.
  37.     for (int j=0; j<patternSize.height;j++)
  38.      {
  39.          for( int i=0; i < patternSize.width;i++)
  40.      {
  41.          objectPoints.push_back(Point3f(i*BoardBoxSize,j*BoardBoxSize,0));
  42.      }
  43.      }
  44.  
  45.     //elow are the 3d object point(world point) to drow x , y z axis.
  46.     point3D.push_back(Point3d(0,0,-10.0)); //-z this point represents 10( cm or mm accrounding to BoardBoxSize unit  )
  47.     point3D.push_back(Point3d(10.0,0,0));  //x
  48.     point3D.push_back(Point3d(0,10.0,0));  //y
  49.  
  50.     ////elow are the 3d object point(world point) to drow Box.
  51.     point3D.push_back(Point3d(12, 0,-12.0));//(x,y,z)
  52.     point3D.push_back(Point3d(12, 8,-12.0));
  53.     point3D.push_back(Point3d(20, 8,-12.0));
  54.     point3D.push_back(Point3d(20, 0,-12.0));
  55.  
  56.     while(1)
  57.     {
  58.         capture >> view;
  59.     flip(view, view, 0);
  60.     flip(Img, Img, 0);
  61.  
  62.  
  63.     if(view.empty()!=1)
  64.     {
  65.     found=findChessboardCorners(view, patternSize,  corners1);//This will detect pattern
  66.     }
  67.  
  68.     if(found)
  69.     {
  70.        cvtColor(view,Img,CV_BGR2GRAY);
  71.      cornerSubPix(Img, corners1, Size(11, 11), Size(-1, -1),
  72.      TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1));
  73.      drawChessboardCorners(view, patternSize, corners1, found);
  74.  
  75.         // The following are two important funtion
  76.         solvePnP(objectPoints, corners1, camera_matrix, dist_coeffs, rotation_vector, translation_vector);//Gives you rotation_vector, translation_vector
  77.  
  78.         //following funtion gives you point2d from point3D world point to drow them on 2d image.
  79.     projectPoints(point3D, rotation_vector, translation_vector, camera_matrix, dist_coeffs, point2D);
  80.  
  81.  
  82.         // following are just drowing funtion to drow object on output image.
  83.  
  84.         //Tp drow x,y z axis on image.
  85.         line(view,corners1[0], point2D[0], Scalar(0,0,255), 3);//z
  86.     line(view,corners1[0], point2D[1], Scalar(255,0,0), 3);//x
  87.     line(view,corners1[0], point2D[2], Scalar(0,255,0), 3);//y
  88.  
  89.  
  90.     putText(view, x, Point(point2D[1].x-10,point2D[1].y-10),FONT_HERSHEY_COMPLEX_SMALL,1, Scalar(255,0,0),2 );
  91.         putText(view, y, Point(point2D[2].x-10,point2D[2].y-10),FONT_HERSHEY_COMPLEX_SMALL,1, Scalar(0,255,0),2 );
  92.         putText(view, z, Point(point2D[0].x-10,point2D[0].y-10),FONT_HERSHEY_COMPLEX_SMALL,1, Scalar(0,0,255),2 );
  93.     circle(view,point2D[0], 3, Scalar(0,0,255), 4, 8,0);
  94.     circle(view,point2D[1], 3, Scalar(255,0,0), 4, 8,0);
  95.     circle(view,point2D[2], 3, Scalar(0,255,0), 4, 8,0);
  96.  
  97.     // To drow box on image. It will writen for pattern size 9,6.
  98.    //If you are using diffrent change corners1 point and point2D point.
  99.  
  100.     line(view,corners1[3],  point2D[3], Scalar(0,0,255),6);
  101.     line(view,corners1[21], point2D[4],Scalar(0,0,255), 6);
  102.       line(view,corners1[23], point2D[5],Scalar(0,0,255), 6);
  103.     line(view,corners1[5],  point2D[6], Scalar(0,0,255),6);
  104.  
  105.     line(view,corners1[3],corners1[5], Scalar(0,255,0),  6);
  106.     line(view,corners1[5],corners1[23],Scalar(0,255,0) ,6);
  107.       line(view,corners1[23],corners1[21],Scalar(0,255,0),  6);
  108.     line(view,corners1[21],corners1[3],Scalar(0,255,0),   6);
  109.  
  110.       line(view,point2D[3], point2D[4], Scalar(255,0,0), 6);
  111.       line(view,point2D[4], point2D[5], Scalar(255,0,0), 6);
  112.       line(view,point2D[5], point2D[6], Scalar(255,0,0), 6);
  113.       line(view,point2D[3], point2D[6], Scalar(255,0,0), 6);
  114.  
  115.         }
  116.  
  117.  // Display image.
  118.     imshow("Output", view);
  119.     waitKey(1);
  120.  
  121. }
  122.  
  123.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top