Advertisement
Guest User

Untitled

a guest
Feb 5th, 2014
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. #include "opencv2/core/core.hpp"
  2. #include "opencv2/calib3d/calib3d.hpp"
  3. #include <opencv2/highgui/highgui.hpp>
  4. #include <opencv2/imgproc/imgproc.hpp>
  5. #include "opencv2/contrib/contrib.hpp"
  6. #include <stdio.h>
  7. #include <iostream>
  8. using namespace cv;
  9. using namespace std;
  10.  
  11. static void onMouse(int event, int x, int y, int flags, void* userdata){
  12.  
  13. Mat *Image = (Mat *) userdata;
  14. if(event == EVENT_LBUTTONDOWN ){
  15. printf("\nMouse button used at position: (%d, %d)", x, y);
  16. printf("\nDepth at point is %f", Image->at<float>(y,x));
  17. }
  18. }
  19.  
  20.  
  21.  
  22. int main(int argc, char* argv[])
  23. {
  24. int numBoards = atoi(argv[1]);
  25. int board_w = atoi(argv[2]);
  26. int board_h = atoi(argv[3]);
  27. int skipframes = atoi(argv[4]);
  28.  
  29. Size board_sz = Size(board_w, board_h);
  30. int board_n = board_w*board_h;
  31.  
  32. vector<vector<Point3f> > object_points;
  33. vector<vector<Point2f> > imagePoints1, imagePoints2;
  34. vector<Point2f> corners1, corners2;
  35.  
  36. vector<Point3f> obj;
  37. for (int j=0; j<board_n; j++)
  38. {
  39. obj.push_back(Point3f(j/board_w, j%board_w, 0.0f));
  40. }
  41.  
  42. Mat img1, img2, gray1, gray2;
  43. VideoCapture cap1 = VideoCapture(0);
  44. VideoCapture cap2 = VideoCapture(2);
  45.  
  46. int success = 0, k = 0;
  47. bool found1 = false, found2 = false;
  48. int frame = 0;
  49.  
  50. while ( success < numBoards )
  51. {
  52. cap1 >> img1;
  53. cap2 >> img2;
  54. if(++frame % skipframes == 0){
  55. //resize(img1, img1, Size(320, 280));
  56. //resize(img2, img2, Size(320, 280));
  57. cvtColor(img1, gray1, CV_BGR2GRAY);
  58. cvtColor(img2, gray2, CV_BGR2GRAY);
  59.  
  60. found1 = findChessboardCorners(img1, board_sz, corners1, CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS);
  61. found2 = findChessboardCorners(img2, board_sz, corners2, CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS);
  62.  
  63. if (found1)
  64. {
  65. cornerSubPix(gray1, corners1, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 30, 0.1));
  66. drawChessboardCorners(gray1, board_sz, corners1, found1);
  67. }
  68.  
  69. if (found2)
  70. {
  71. cornerSubPix(gray2, corners2, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 30, 0.1));
  72. drawChessboardCorners(gray2, board_sz, corners2, found2);
  73. }
  74.  
  75. imshow("image1", gray1);
  76. imshow("image2", gray2);
  77.  
  78. k = waitKey(2000);
  79. if (found1 && found2)
  80. {
  81. //k = waitKey(0);
  82. }
  83. if (k == 27)
  84. {
  85. break;
  86. }
  87.  
  88. if (found1 !=0 && found2 != 0)
  89. {
  90. imagePoints1.push_back(corners1);
  91. imagePoints2.push_back(corners2);
  92. object_points.push_back(obj);
  93. printf ("Corners stored\n");
  94. success++;
  95.  
  96. if (success >= numBoards)
  97. {
  98. break;
  99. }
  100. }
  101. }
  102. }
  103.  
  104. destroyAllWindows();
  105. printf("Starting Calibration\n");
  106. Mat CM1 = Mat(3, 3, CV_64FC1);
  107. Mat CM2 = Mat(3, 3, CV_64FC1);
  108. Mat D1, D2;
  109. Mat R, T, E, F;
  110.  
  111. stereoCalibrate(object_points, imagePoints1, imagePoints2,
  112. CM1, D1, CM2, D2, img1.size(), R, T, E, F,
  113. cvTermCriteria(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS, 100, 1e-5),
  114. CV_CALIB_SAME_FOCAL_LENGTH | CV_CALIB_ZERO_TANGENT_DIST);
  115.  
  116. FileStorage fs1("mystereocalib.yml", FileStorage::WRITE);
  117. fs1 << "CM1" << CM1;
  118. fs1 << "CM2" << CM2;
  119. fs1 << "D1" << D1;
  120. fs1 << "D2" << D2;
  121. fs1 << "R" << R;
  122. fs1 << "T" << T;
  123. fs1 << "E" << E;
  124. fs1 << "F" << F;
  125.  
  126. printf("Done Calibration\n");
  127.  
  128. printf("Starting Rectification\n");
  129.  
  130. Mat R1, R2, P1, P2, Q;
  131. stereoRectify(CM1, D1, CM2, D2, img1.size(), R, T, R1, R2, P1, P2, Q);
  132. fs1 << "R1" << R1;
  133. fs1 << "R2" << R2;
  134. fs1 << "P1" << P1;
  135. fs1 << "P2" << P2;
  136. fs1 << "Q" << Q;
  137.  
  138. printf("Done Rectification\n");
  139.  
  140. printf("Applying Undistort\n");
  141.  
  142. Mat map1x, map1y, map2x, map2y;
  143. Mat imgU1, imgU2;
  144.  
  145. initUndistortRectifyMap(CM1, D1, R1, P1, img1.size(), CV_32FC1, map1x, map1y);
  146. initUndistortRectifyMap(CM2, D2, R2, P2, img2.size(), CV_32FC1, map2x, map2y);
  147.  
  148. printf("Undistort complete\n");
  149.  
  150. //StereoBM
  151. StereoBM StereoBMObj = StereoBM( 0, 128, 41);
  152. Mat disparity;
  153. Mat left, right, dispOutput;
  154. while(1)
  155. {
  156. cap1 >> img1;
  157. cap2 >> img2;
  158.  
  159. remap(img1, imgU1, map1x, map1y, INTER_LINEAR, BORDER_CONSTANT, Scalar());
  160. remap(img2, imgU2, map2x, map2y, INTER_LINEAR, BORDER_CONSTANT, Scalar());
  161.  
  162. cvtColor(imgU1, left, CV_RGB2GRAY);
  163. cvtColor(imgU2, right, CV_RGB2GRAY);
  164.  
  165. StereoBMObj(left, right, disparity, CV_32F);
  166. reprojectImageTo3D(disparity, dispOutput, Q);
  167.  
  168. imshow("image1", imgU1);
  169. imshow("image2", imgU2);
  170. imshow("Disparity", disparity);
  171. setMouseCallback("Disparity", onMouse, &dispOutput);
  172. k = waitKey(1);
  173. }
  174.  
  175. cap1.release();
  176. cap2.release();
  177.  
  178. return(0);
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement