PageRenderTime 139ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/decoder2/fundamental.cpp

https://github.com/joshuaeckroth/SURV-I
C++ | 394 lines | 287 code | 100 blank | 7 comment | 24 complexity | edc48864f6c6cafdc88e4cbeed5c0359 MD5 | raw file
  1. #include <opencv/cv.h>
  2. #include <opencv/highgui.h>
  3. int main(int argc, char **argv)
  4. {
  5. int numpoints = 28;
  6. CvMat *east_points = cvCreateMat(2, numpoints, CV_32FC1);
  7. CvMat *west_points = cvCreateMat(2, numpoints, CV_32FC1);
  8. /* Set east points */
  9. cvmSet(east_points, 0, 0, 613.0);
  10. cvmSet(east_points, 1, 0, 134.0);
  11. cvmSet(east_points, 0, 1, 605.0);
  12. cvmSet(east_points, 1, 1, 137.0);
  13. cvmSet(east_points, 0, 2, 587.0);
  14. cvmSet(east_points, 1, 2, 141.0);
  15. cvmSet(east_points, 0, 3, 511.0);
  16. cvmSet(east_points, 1, 3, 158.0);
  17. cvmSet(east_points, 0, 4, 507.0);
  18. cvmSet(east_points, 1, 4, 163.0);
  19. cvmSet(east_points, 0, 5, 462.0);
  20. cvmSet(east_points, 1, 5, 173.0);
  21. cvmSet(east_points, 0, 6, 616.0);
  22. cvmSet(east_points, 1, 6, 161.0);
  23. cvmSet(east_points, 0, 7, 473.0);
  24. cvmSet(east_points, 1, 7, 168.0);
  25. cvmSet(east_points, 0, 8, 133.0);
  26. cvmSet(east_points, 1, 8, 139.0);
  27. cvmSet(east_points, 0, 9, 613.0);
  28. cvmSet(east_points, 1, 9, 162.0);
  29. cvmSet(east_points, 0, 10, 584.0);
  30. cvmSet(east_points, 1, 10, 112.0);
  31. cvmSet(east_points, 0, 11, 575.0);
  32. cvmSet(east_points, 1, 11, 114.0);
  33. cvmSet(east_points, 0, 12, 144.0);
  34. cvmSet(east_points, 1, 12, 108.0);
  35. cvmSet(east_points, 0, 13, 165.0);
  36. cvmSet(east_points, 1, 13, 113.0);
  37. cvmSet(east_points, 0, 14, 120.0);
  38. cvmSet(east_points, 1, 14, 113.0);
  39. cvmSet(east_points, 0, 15, 188.0);
  40. cvmSet(east_points, 1, 15, 132.0);
  41. cvmSet(east_points, 0, 16, 95.0);
  42. cvmSet(east_points, 1, 16, 127.0);
  43. cvmSet(east_points, 0, 17, 62.0);
  44. cvmSet(east_points, 1, 17, 148.0);
  45. cvmSet(east_points, 0, 18, 119.0);
  46. cvmSet(east_points, 1, 18, 137.0);
  47. cvmSet(east_points, 0, 19, 224.0);
  48. cvmSet(east_points, 1, 19, 139.0);
  49. cvmSet(east_points, 0, 20, 424.0);
  50. cvmSet(east_points, 1, 20, 138.0);
  51. cvmSet(east_points, 0, 21, 636.0);
  52. cvmSet(east_points, 1, 21, 139.0);
  53. cvmSet(east_points, 0, 22, 568.0);
  54. cvmSet(east_points, 1, 22, 136.0);
  55. cvmSet(east_points, 0, 23, 497.0);
  56. cvmSet(east_points, 1, 23, 96.0);
  57. cvmSet(east_points, 0, 24, 448.0);
  58. cvmSet(east_points, 1, 24, 126.0);
  59. cvmSet(east_points, 0, 25, 591.0);
  60. cvmSet(east_points, 1, 25, 130.0);
  61. cvmSet(east_points, 0, 26, 80.0);
  62. cvmSet(east_points, 1, 26, 70.0);
  63. cvmSet(east_points, 0, 27, 110.0);
  64. cvmSet(east_points, 1, 27, 88.0);
  65. /* West points */
  66. cvmSet(west_points, 0, 0, 169.0);
  67. cvmSet(west_points, 1, 0, 289.0);
  68. cvmSet(west_points, 0, 1, 154.0);
  69. cvmSet(west_points, 1, 1, 284.0);
  70. cvmSet(west_points, 0, 2, 137.0);
  71. cvmSet(west_points, 1, 2, 279.0);
  72. cvmSet(west_points, 0, 3, 76.0);
  73. cvmSet(west_points, 1, 3, 257.0);
  74. cvmSet(west_points, 0, 4, 62.0);
  75. cvmSet(west_points, 1, 4, 256.0);
  76. cvmSet(west_points, 0, 5, 40.0);
  77. cvmSet(west_points, 1, 5, 247.0);
  78. cvmSet(west_points, 0, 6, 72.0);
  79. cvmSet(west_points, 1, 6, 289.0);
  80. cvmSet(west_points, 0, 7, 52.0);
  81. cvmSet(west_points, 1, 7, 248.0);
  82. cvmSet(west_points, 0, 8, 108.0);
  83. cvmSet(west_points, 1, 8, 185.0);
  84. cvmSet(west_points, 0, 9, 70.0);
  85. cvmSet(west_points, 1, 9, 286.0);
  86. cvmSet(west_points, 0, 10, 173.0);
  87. cvmSet(west_points, 1, 10, 254.0);
  88. cvmSet(west_points, 0, 11, 161.0);
  89. cvmSet(west_points, 1, 11, 252.0);
  90. cvmSet(west_points, 0, 12, 134.0);
  91. cvmSet(west_points, 1, 12, 170.0);
  92. cvmSet(west_points, 0, 13, 144.0);
  93. cvmSet(west_points, 1, 13, 177.0);
  94. cvmSet(west_points, 0, 14, 121.0);
  95. cvmSet(west_points, 1, 14, 169.0);
  96. cvmSet(west_points, 0, 15, 125.0);
  97. cvmSet(west_points, 1, 15, 191.0);
  98. cvmSet(west_points, 0, 16, 91.0);
  99. cvmSet(west_points, 1, 16, 171.0);
  100. cvmSet(west_points, 0, 17, 78.0);
  101. cvmSet(west_points, 1, 17, 178.0);
  102. cvmSet(west_points, 0, 18, 100.0);
  103. cvmSet(west_points, 1, 18, 180.0);
  104. cvmSet(west_points, 0, 19, 66.0);
  105. cvmSet(west_points, 1, 19, 189.0);
  106. cvmSet(west_points, 0, 20, 74.0);
  107. cvmSet(west_points, 1, 20, 221.0);
  108. cvmSet(west_points, 0, 21, 148.0);
  109. cvmSet(west_points, 1, 21, 298.0);
  110. cvmSet(west_points, 0, 22, 150.0);
  111. cvmSet(west_points, 1, 22, 271.0);
  112. cvmSet(west_points, 0, 23, 226.0);
  113. cvmSet(west_points, 1, 23, 229.0);
  114. cvmSet(west_points, 0, 24, 136.0);
  115. cvmSet(west_points, 1, 24, 230.0);
  116. cvmSet(west_points, 0, 25, 178.0);
  117. cvmSet(west_points, 1, 25, 279.0);
  118. cvmSet(west_points, 0, 26, 233.0);
  119. cvmSet(west_points, 1, 26, 149.0);
  120. cvmSet(west_points, 0, 27, 189.0);
  121. cvmSet(west_points, 1, 27, 160.0);
  122. CvPoint2D32f __east_points[numpoints], __west_points[numpoints];
  123. CvMat _east_points = cvMat(1, numpoints, CV_32FC2, __east_points);
  124. CvMat _west_points = cvMat(1, numpoints, CV_32FC2, __west_points);
  125. for(int i = 0; i < numpoints; i++)
  126. {
  127. __east_points[i] = cvPoint2D32f(cvmGet(east_points, 0, i),
  128. cvmGet(east_points, 1, i));
  129. __west_points[i] = cvPoint2D32f(cvmGet(west_points, 0, i),
  130. cvmGet(west_points, 1, i));
  131. }
  132. IplImage *east = cvLoadImage("../videos/plse1.png");
  133. IplImage *west = cvLoadImage("../videos/plsw1.png");
  134. CvMat *fmat = cvCreateMat(3, 3, CV_32FC1);
  135. CvMat *status = cvCreateMat(1, numpoints, CV_8UC1);
  136. int fm_count = cvFindFundamentalMat(west_points, east_points, fmat,
  137. CV_FM_RANSAC, 3, 0.99, status);
  138. CvSize imgsize = cvSize(west->width, west->height);
  139. CvMat *east_homo = cvCreateMat(3, 3, CV_64F);
  140. CvMat *west_homo = cvCreateMat(3, 3, CV_64F);
  141. cvStereoRectifyUncalibrated(&_west_points, &_east_points, fmat,
  142. imgsize, west_homo, east_homo, 3);
  143. //cvSave("east_fmat.xml", fmat);
  144. cvSave("west_fmat.xml", fmat);
  145. printf("result of FindFundamentalMat: %d\n", fm_count);
  146. printf("point statuses:\n");
  147. for(int i = 0; i < numpoints; i++)
  148. {
  149. printf(" point %d: %f\n", i, cvGetReal1D(status, i));
  150. }
  151. for(int i = 0; i < 3; i++)
  152. {
  153. for(int j = 0; j < 3; j++)
  154. {
  155. printf("fmat[%d][%d] = %f\n", i, j, cvmGet(fmat, i, j));
  156. }
  157. }
  158. for(int i = 0; i < 3; i++)
  159. {
  160. for(int j = 0; j < 3; j++)
  161. {
  162. printf("east_homo[%d][%d] = %f\n", i, j, cvmGet(east_homo, i, j));
  163. }
  164. }
  165. for(int i = 0; i < 3; i++)
  166. {
  167. for(int j = 0; j < 3; j++)
  168. {
  169. printf("west_homo[%d][%d] = %f\n", i, j, cvmGet(west_homo, i, j));
  170. }
  171. }
  172. for(int view = 0; view < 2; view++)
  173. {
  174. CvMat *corrLines = cvCreateMat(3, numpoints, CV_32F);
  175. if(view == 1)
  176. {
  177. cvComputeCorrespondEpilines(west_points, 1, fmat, corrLines);
  178. }
  179. else
  180. {
  181. cvComputeCorrespondEpilines(east_points, 2, fmat, corrLines);
  182. }
  183. CvMat *epiLine = cvCreateMat(1, 3, CV_32F);
  184. for(int i = 0; i < numpoints; i++)
  185. {
  186. int j;
  187. for(j = 0; j < 3; j++)
  188. {
  189. cvmSet(epiLine, 0, j, cvmGet(corrLines, j, i));
  190. }
  191. CvPoint epPoint1, epPoint2;
  192. CvMat *a = cvCreateMat(3, 1, CV_32F);
  193. CvMat *b = cvCreateMat(3, 1, CV_32F);
  194. CvMat *c = cvCreateMat(3, 1, CV_32F);
  195. CvMat *d = cvCreateMat(3, 1, CV_32F);
  196. for(int j = 0; j < 3; j++)
  197. {
  198. cvSetReal2D(a, j, 0,
  199. cvGetReal2D(epiLine, 0, j) / cvGetReal2D(epiLine, 0, 2));
  200. }
  201. if (abs(cvGetReal2D(epiLine,0,0)) > abs(cvGetReal2D(epiLine,0,1)) )
  202. {
  203. double ylim = west->height;
  204. cvSetReal2D(b,0,0,0);
  205. cvSetReal2D(b,1,0,1);
  206. cvSetReal2D(b,2,0,0);
  207. cvCrossProduct(a,b,c);
  208. for(j = 0; j < 3; j++)
  209. {
  210. cvSetReal2D(c,j,0,cvGetReal2D(c,j,0)/cvGetReal2D(c,2,0));
  211. }
  212. cvSetReal2D(b,0,0,0);
  213. cvSetReal2D(b,1,0,-1.0/ylim);
  214. cvSetReal2D(b,2,0,1);
  215. cvCrossProduct(a,b,d);
  216. for(j= 0; j < 3; j++)
  217. {
  218. cvSetReal2D(d,j,0,cvGetReal2D(d,j,0)/cvGetReal2D(d,2,0));
  219. }
  220. }
  221. else
  222. {
  223. double xlim = west->width;
  224. cvSetReal2D(b,0,0,1);
  225. cvSetReal2D(b,1,0,0);
  226. cvSetReal2D(b,2,0,0);
  227. cvCrossProduct(a,b,c);
  228. for(j = 0; j < 3; j++)
  229. {
  230. cvSetReal2D(c,j,0,cvGetReal2D(c,j,0)/cvGetReal2D(c,2,0));
  231. }
  232. cvSetReal2D(b,0,0,-1.0/xlim);
  233. cvSetReal2D(b,1,0,0);
  234. cvSetReal2D(b,2,0,1);
  235. cvCrossProduct(a,b,d);
  236. for(j = 0; j < 3; j++)
  237. {
  238. cvSetReal2D(d,j,0,cvGetReal2D(d,j,0)/cvGetReal2D(d,2,0));
  239. }
  240. }
  241. CvPoint p;
  242. for(j = 0; j < numpoints; j++)
  243. {
  244. if(view == 1)
  245. {
  246. p.x = cvmGet(east_points, 0, j);
  247. p.y = cvmGet(east_points, 1, j);
  248. //cvCircle(east, p, 10, CV_RGB(255, 255, 255));
  249. }
  250. else
  251. {
  252. p.x = cvmGet(west_points, 0, j);
  253. p.y = cvmGet(west_points, 1, j);
  254. //cvCircle(west, p, 10, CV_RGB(255, 255, 255));
  255. }
  256. }
  257. epPoint1.x = cvmGet(c,0,0);
  258. epPoint1.y = cvmGet(c,1,0);
  259. epPoint2.x = cvmGet(d,0,0);
  260. epPoint2.y = cvmGet(d,1,0);
  261. if(view == 1)
  262. {
  263. //cvLine(east, epPoint1, epPoint2, CV_RGB(255,255,255));
  264. }
  265. else
  266. {
  267. //cvLine(west, epPoint1, epPoint2, CV_RGB(255,255,255));
  268. }
  269. }
  270. }
  271. IplImage *east_trans = cvCreateImage(imgsize, east->depth, east->nChannels);
  272. IplImage *west_trans = cvCreateImage(imgsize, west->depth, west->nChannels);
  273. cvWarpPerspective(east, east_trans, east_homo);
  274. cvWarpPerspective(west, west_trans, west_homo);
  275. IplImage *east_trans_8bit = cvCreateImage(imgsize, east->depth, 1);
  276. IplImage *west_trans_8bit = cvCreateImage(imgsize, west->depth, 1);
  277. cvCvtColor(east_trans, east_trans_8bit, CV_BGR2GRAY);
  278. cvCvtColor(west_trans, west_trans_8bit, CV_BGR2GRAY);
  279. CvStereoBMState *bm = cvCreateStereoBMState();
  280. IplImage *disparity = cvCreateImage(imgsize, IPL_DEPTH_16S, 1);
  281. IplImage *vdisparity = cvCreateImage(imgsize, IPL_DEPTH_8U, 1);
  282. cvFindStereoCorrespondenceBM(east_trans_8bit, west_trans_8bit, disparity, bm);
  283. cvNormalize(disparity, vdisparity, 0, 256, CV_MINMAX );
  284. cvNamedWindow("east", CV_WINDOW_AUTOSIZE);
  285. cvNamedWindow("west", CV_WINDOW_AUTOSIZE);
  286. cvShowImage("east", east_trans_8bit);
  287. cvShowImage("west", west_trans_8bit);
  288. cvSaveImage("east-trans.png", east_trans);
  289. cvSaveImage("west-trans.png", west_trans);
  290. cvNamedWindow("disparity");
  291. cvShowImage("disparity", vdisparity);
  292. cvWaitKey(0);
  293. return 0;
  294. }