Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2013
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.25 KB | None | 0 0
  1. #include <opencv2/core/core.hpp>
  2. #include <opencv2/imgproc/imgproc.hpp>
  3. #include <opencv2/highgui/highgui.hpp>
  4. #include <opencv2/calib3d/calib3d.hpp>
  5. #include <iostream>
  6. #include <string>
  7.  
  8. using namespace cv;
  9. using namespace std;
  10.  
  11. void show(const char* windowname, Mat image)
  12. {
  13.     namedWindow(windowname, CV_WINDOW_AUTOSIZE);
  14.     imshow(windowname, image);
  15. }
  16.  
  17. //TRACKBAR FUNCTIONS - BEGIN
  18. /*
  19. int pfs=0;
  20. int pfs_max=63;
  21. int pfc=0;
  22. int pfc_max=63;
  23. int sws=0;
  24. int sws_max=63;
  25. //int md=0;
  26. //int md_max=10;
  27. int nod=0;
  28. int nod_max=128;
  29. int tt=0;
  30. int tt_max=50;
  31. int ur=0;
  32. int ur_max=20;
  33. char* threshWin = "Threshold Window";
  34.  
  35. int a=31;
  36. int b=31;
  37. int c=35;
  38. int d=0;
  39. int e=48;
  40. int f=20;
  41. int g=15;
  42.  
  43. void onPreFilterSizeTrackbar(int, void*)
  44. {
  45. cout << "new pfs " << pfs << endl;
  46. if((pfs%2)!=0 && pfs>=5)
  47. a = pfs;
  48. }
  49. void onPreFilterCapTrackbar(int, void*)
  50. {
  51. cout << "new pfc " << pfc << endl;
  52. if((pfc%2)!=0 && pfc>=5)
  53. b = pfc;
  54. }
  55. void onSADWindowSizeTrackbar(int, void*)
  56. {
  57. cout << "new sws " << sws << endl;
  58. if((sws%2)!=0 && sws>=5)
  59. c = sws;
  60. if(sws < 5)
  61. c = 5;
  62. }
  63. /*
  64. void onMinDisparityTrackbar(int, void*)
  65. {
  66. cout << "new md " << md << endl;
  67. d = md;
  68. }
  69. */
  70. /*
  71. void onNumberOfDisparitiesTrackbar(int, void*)
  72. {
  73. cout << "new nod " << nod << endl;
  74. if((nod%16)==0 && nod>0)
  75. e = nod;
  76. if(nod==0)
  77. e=16;
  78. }
  79. void onTextureThresholdTrackbar(int, void*)
  80. {
  81. cout << "new tt " << tt << endl;
  82. f = tt;
  83. }
  84. void onUniquenessRatioTrackbar(int, void*)
  85. {
  86. cout << "new ur " << ur << endl;
  87. g = ur;
  88. }
  89. */
  90. //TRACKBAR FUNCTIONS - END
  91.  
  92. int main()
  93. {
  94.     Mat image1, image2;
  95.  
  96.     Mat camMat1 = (Mat_<double>(3,3) << 793.1338, 0, 337.2309, 0, 792.0555, 256.9991, 0, 0, 1);
  97.     Mat camMat2 = (Mat_<double>(3,3) << 799.1271, 0, 319.8581, 0, 797.2460, 243.4638, 0, 0, 1);
  98.     Mat dispCoeffs1 = (Mat_<double>(1,5) << 0.0033, -0.1320, -0.0019, 0.0026, 0);
  99.     Mat dispCoeffs2 = (Mat_<double>(1,5) << -0.0109, -0.0188, -0.0014, -0.0055, 0);
  100.     Mat RotMat = (Mat_<double>(3,3) << 0.9998, -0.0023, 0.0221, 0.0022, 1, 0.0031, -0.0221, -0.0031, 0.9998);
  101.     Mat TransMat = (Mat_<double>(3,1) << 374.2306, -1.8319, 5.5745);
  102.  
  103.     //Rectify
  104.     Mat R1, R2, P1, P2, Q;
  105.     stereoRectify(camMat1, dispCoeffs1, camMat2, dispCoeffs2, Size(640,480), RotMat, TransMat, R1, R2, P1, P2, Q, CV_CALIB_ZERO_DISPARITY, 1, Size(640,480));
  106.  
  107.     //Define the mapping to the done
  108.     Mat rx1, ry1;
  109.     Mat rx2, ry2;
  110.     initUndistortRectifyMap(camMat1, dispCoeffs1, R1, P1, Size(640,480), CV_16SC2, rx1, ry1);
  111.     initUndistortRectifyMap(camMat2, dispCoeffs2, R2, P2, Size(640,480), CV_16SC2, rx2, ry2);
  112.  
  113.     //SET THE BM STATE VARIABLES BEGIN - DONE GLOBALLY
  114.     StereoBM bm;
  115.     bm.state->preFilterSize = 31;
  116.     bm.state->preFilterCap = 63;
  117.     bm.state->SADWindowSize = 9;
  118.     bm.state->minDisparity = -128;
  119.     //bm.state->disp12MaxDiff = 2;
  120.     bm.state->numberOfDisparities = 128;
  121.     bm.state->textureThreshold = 50;
  122.     bm.state->uniquenessRatio = 15;
  123.     bm.state->speckleWindowSize = 100;
  124.     bm.state->speckleRange = 16;
  125.     //SET THE BM STATE VARIABLES END
  126.  
  127.     /*
  128.     namedWindow(threshWin);
  129.     createTrackbar("Pre Filter Size", threshWin, &pfs, pfs_max, onPreFilterSizeTrackbar);
  130.     createTrackbar("Pre Filter Cap", threshWin, &pfc, pfc_max, onPreFilterCapTrackbar);  
  131.     createTrackbar("SAD Window Size", threshWin, &sws, sws_max, onSADWindowSizeTrackbar);
  132.     //createTrackbar("Minimum Disparity", threshWin, &md, md_max, onMinDisparityTrackbar);
  133.     createTrackbar("Number Of Disparities", threshWin, &nod, nod_max, onNumberOfDisparitiesTrackbar);
  134.     createTrackbar("Texture Threshold", threshWin, &tt, tt_max, onTextureThresholdTrackbar);
  135.     createTrackbar("Uniqueness Ratio", threshWin, &ur, ur_max, onUniquenessRatioTrackbar);
  136.     */
  137.  
  138.     VideoCapture cap3 = VideoCapture(0);
  139.     VideoCapture cap4 = VideoCapture(1);
  140.    
  141.     //cap3.set(CV_CAP_PROP_FRAME_WIDTH, 320);
  142.     //cap3.set(CV_CAP_PROP_FRAME_HEIGHT, 240);
  143.     //cap4.set(CV_CAP_PROP_FRAME_WIDTH, 320);
  144.     //cap4.set(CV_CAP_PROP_FRAME_HEIGHT, 240);
  145.    
  146.     cap3 >> image1;
  147.     cap4 >> image2;
  148.     Size imageSize = image1.size();
  149.  
  150.     Mat gray_image1;
  151.     Mat gray_image2;
  152.     Mat frame1r;
  153.     //frame1r.create(image1.size(), CV_8U);
  154.     Mat frame2r;
  155.     //frame2r.create(image2.size(), CV_8U);
  156.     Mat frame1rf;
  157.     Mat frame2rf;
  158.     //Mat disp(image1.size(), CV_16S);
  159.     //Mat vdisp(image1.size(), CV_8U);
  160.     Mat disp, vdisp;
  161.     //Mat image3d(image1.size(), CV_32FC3);
  162.     Mat image3d;
  163.     Mat rectified_pair;
  164.     rectified_pair.create(imageSize.height, (imageSize.width)*2, CV_8UC3);
  165.  
  166.     //Actually do the mapping -- based on the mapping definition
  167.  
  168.     while(1)
  169.     {
  170.         /*
  171.         bm.state->preFilterSize = a;
  172.         bm.state->preFilterCap = b;
  173.         bm.state->SADWindowSize = c;
  174.         //bm.state->minDisparity = d;
  175.         bm.state->disp12MaxDiff = 2;
  176.         bm.state->numberOfDisparities = e;
  177.         bm.state->textureThreshold = f;
  178.         bm.state->uniquenessRatio = g;
  179.         bm.state->speckleWindowSize = 50;
  180.         bm.state->speckleRange = 32;
  181.         */
  182.  
  183.         bm.state->preFilterSize = 31;
  184.         bm.state->preFilterCap = 63;
  185.         bm.state->SADWindowSize = 21;
  186.         bm.state->minDisparity = -128;
  187.         //bm.state->disp12MaxDiff = 2;
  188.         bm.state->numberOfDisparities = 64;
  189.         bm.state->textureThreshold = 20;
  190.         bm.state->uniquenessRatio = 10;
  191.         bm.state->speckleWindowSize = 100;
  192.         bm.state->speckleRange = 32;
  193.  
  194.         cvtColor(image1, gray_image1, CV_BGR2GRAY);
  195.         cvtColor(image2, gray_image2, CV_BGR2GRAY);
  196.         remap(gray_image1, frame1r, rx1, ry1, CV_INTER_LINEAR);
  197.         remap(gray_image2, frame2r, rx2, ry2, CV_INTER_LINEAR);
  198.         bm(frame1r, frame2r, disp);
  199.         normalize(disp, vdisp, 0, 255, NORM_MINMAX, CV_8U);
  200.         //convertScaleAbs(vdisp, vdisp, 1, 0);
  201.         disp.convertTo(vdisp, CV_8U, 255/(64*16.));
  202.         show("disparity", vdisp);
  203.         //reprojectImageTo3D(disp, image3d, Q, true);
  204.         //show("depth map", image3d);
  205.  
  206.         //display image side by side for rectified window
  207.         //copy frame1r to the left side
  208.         cvtColor(frame1r, frame1rf, CV_GRAY2BGR);
  209.         frame1rf.copyTo(rectified_pair(Rect(0,0,imageSize.width, imageSize.height)));
  210.         //copy frame2r to the right side
  211.         cvtColor(frame2r, frame2rf, CV_GRAY2BGR);
  212.         frame2rf.copyTo(rectified_pair(Rect(imageSize.width,0,imageSize.width, imageSize.height)));
  213.         for(int i=0; i<imageSize.height; i+=32)
  214.             line(rectified_pair, Point(0,i), Point((imageSize.width)*2, i), CV_RGB(0,255,0));
  215.         show("rectified", rectified_pair);
  216.  
  217.         cap3 >> image1;
  218.         cap4 >> image2;
  219.         if(waitKey(15) == 27)
  220.             break;
  221.     }
  222.  
  223.     return 0;
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement