MyOpenCV.cpp
上传用户:quan1896
上传日期:2013-04-17
资源大小:94k
文件大小:9k
源码类别:

2D图形编程

开发平台:

Visual C++

  1. // MyOpenCV.cpp : my opencv functions
  2. //
  3. /////////////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "MyOpenCV.h"
  6. #include <math.h>
  7. // 画十字形标记函数
  8. void quPlotCross(CvArr* img,CvPoint pt,double color,int thickness,int radius)
  9. {
  10. CvPoint left,right,up,down;
  11. left.x=pt.x-radius;left.y=pt.y;
  12. right.x=pt.x+radius;right.y=pt.y;
  13. up.x=pt.x;up.y=pt.y+radius;
  14. down.x=pt.x;down.y=pt.y-radius;
  15. cvLine(img,left,right,color,thickness,0);
  16. cvLine(img,up,down,color,thickness,0);
  17. }
  18. // 特征点初始匹配函数
  19. void quCornermatcher(IplImage* grayImage1,IplImage* grayImage2,int patch_radius,int max_disparity,
  20.  CvPoint2D32f* corners1,CvPoint2D32f* corners2,int cornerCount,
  21.  CvPoint2D32f* matches1,CvPoint2D32f* matches2,int* nmatches,int* mat12,int* mat21)
  22. {
  23. int x1,y1,x2,y2;
  24. double norm,minnorm;
  25. IplImage* patchimg1=0;
  26. IplImage* patchimg2=0;
  27. *nmatches=0;
  28. patchimg1=cvCreateImage(cvSize(patch_radius*2+1,patch_radius*2+1),IPL_DEPTH_8U,1);
  29. patchimg2=cvCreateImage(cvSize(patch_radius*2+1,patch_radius*2+1),IPL_DEPTH_8U,1);
  30. iplSetBorderMode(grayImage1,IPL_BORDER_CONSTANT,IPL_SIDE_ALL,0);
  31. iplSetBorderMode(grayImage2,IPL_BORDER_CONSTANT,IPL_SIDE_ALL,0);
  32. //正向相关匹配(1->2)
  33. for(int i=0;i<cornerCount;i++)
  34. {
  35. minnorm=1.0e8;
  36. mat12[i]=-1;//if mat*[i]<0 canot found matches
  37. x1=cvRound(corners1[i].x);y1=cvRound(corners1[i].y);
  38. cvSetImageROI(grayImage1,cvRect(x1-patch_radius,y1-patch_radius,patch_radius*2+1,patch_radius*2+1));
  39. cvSetZero(patchimg1);
  40. iplCopy(grayImage1,patchimg1);
  41. cvSetImageROI(grayImage1,cvRect(0,0,grayImage1->width,grayImage1->height));
  42. for(int j=0;j<cornerCount;j++)
  43. {
  44. x2=cvRound(corners2[j].x);y2=cvRound(corners2[j].y);
  45. if((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)<max_disparity*max_disparity)
  46. {
  47. cvSetImageROI(grayImage2,cvRect(x2-patch_radius,y2-patch_radius,patch_radius*2+1,patch_radius*2+1));
  48. cvSetZero(patchimg2);
  49. iplCopy(grayImage2,patchimg2);
  50. cvSetImageROI(grayImage2,cvRect(0,0,grayImage2->width,grayImage2->height));
  51. norm=iplNorm(patchimg1,patchimg2,IPL_L2);//相关系数(图像2范数)
  52. if(norm<minnorm)
  53. {
  54. minnorm=norm;
  55. mat12[i]=j;
  56. }
  57. }
  58. }
  59. cvSetImageROI(grayImage1,cvRect(0,0,grayImage1->width,grayImage1->height));
  60. cvSetImageCOI(grayImage1,0);
  61. }
  62. //反向相关匹配(2->1)
  63. for(i=0;i<cornerCount;i++)
  64. {
  65. minnorm=1.0e8;
  66. mat21[i]=-1;//if mat*[i]<0 canot found matches
  67. x2=cvRound(corners2[i].x);y2=cvRound(corners2[i].y);
  68. cvSetImageROI(grayImage2,cvRect(x2-patch_radius,y2-patch_radius,patch_radius*2+1,patch_radius*2+1));
  69. cvSetZero(patchimg2);
  70. iplCopy(grayImage2,patchimg2);
  71. cvSetImageROI(grayImage2,cvRect(0,0,grayImage2->width,grayImage2->height));
  72. for(int j=0;j<cornerCount;j++)
  73. {
  74. x1=cvRound(corners1[j].x);y1=cvRound(corners1[j].y);
  75. if((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)<max_disparity*max_disparity)
  76. {
  77. cvSetImageROI(grayImage1,cvRect(x1-patch_radius,y1-patch_radius,patch_radius*2+1,patch_radius*2+1));
  78. cvSetZero(patchimg1);
  79. iplCopy(grayImage1,patchimg1);
  80. cvSetImageROI(grayImage1,cvRect(0,0,grayImage1->width,grayImage1->height));
  81. norm=iplNorm(patchimg1,patchimg2,IPL_L2);//相关系数(图像2范数)
  82. if(norm<minnorm)
  83. {
  84. minnorm=norm;
  85. mat21[i]=j;
  86. }
  87. }
  88. }
  89. cvSetImageROI(grayImage2,cvRect(0,0,grayImage2->width,grayImage2->height));
  90. cvSetImageCOI(grayImage2,0);
  91. }
  92. //重组匹配点矩阵
  93. for(i=0;i<cornerCount;i++)
  94. {
  95. //if(mat12[i]>=0)//(1->2)
  96. if(mat12[i]>=0&&mat21[mat12[i]]==i)//(1->2)&&(2->1)
  97. {
  98. matches1[*nmatches].x=corners1[i].x;matches1[*nmatches].y=corners1[i].y;
  99. matches2[*nmatches].x=corners2[mat12[i]].x;matches2[*nmatches].y=corners2[mat12[i]].y;
  100. (*nmatches)++;
  101. }
  102. }
  103. cvReleaseImage(&patchimg1);
  104. cvReleaseImage(&patchimg2);
  105. }
  106. // 基于F的引导匹配函数
  107. void quGuidematcher(IplImage* grayImage1,IplImage* grayImage2,int patch_radius,int max_disparity,double epidis,
  108.  CvPoint2D32f* corners1,CvPoint2D32f* corners2,int cornerCount,CvMat* fundMatr,
  109.  CvPoint2D32f* matches1,CvPoint2D32f* matches2,int* nmatches,int* mat12,int* mat21)
  110. {
  111. int x1,y1,x2,y2;
  112. double norm,minnorm;
  113. CvMat* points1=0;
  114.     CvMat* points2=0;
  115.     CvMat* wpoints1=0;
  116.     CvMat* wpoints2=0;
  117.     CvMat* corrLines1=0;
  118.     CvMat* corrLines2=0;
  119. IplImage* patchimg1=0;
  120. IplImage* patchimg2=0;
  121. *nmatches=0;
  122.     points1=cvCreateMat(2,cornerCount,CV_32F);
  123.     points2=cvCreateMat(2,cornerCount,CV_32F);
  124.     wpoints1=cvCreateMat(3,cornerCount,CV_64F);
  125.     wpoints2=cvCreateMat(3,cornerCount,CV_64F);
  126.     corrLines1=cvCreateMat(3,cornerCount,CV_64F);
  127.     corrLines2=cvCreateMat(3,cornerCount,CV_64F);
  128. for(int i=0;i<cornerCount;i++)
  129. {
  130. cvmSet(points1,0,i,corners1[i].x); cvmSet(points1,1,i,corners1[i].y);
  131. cvmSet(points2,0,i,corners2[i].x); cvmSet(points2,1,i,corners2[i].y);
  132. }
  133. cvMake3DPoints(points1,wpoints1);
  134.     cvMake3DPoints(points2,wpoints2);
  135. patchimg1=cvCreateImage(cvSize(patch_radius*2+1,patch_radius*2+1),IPL_DEPTH_8U,1);
  136. patchimg2=cvCreateImage(cvSize(patch_radius*2+1,patch_radius*2+1),IPL_DEPTH_8U,1);
  137. iplSetBorderMode(grayImage1,IPL_BORDER_CONSTANT,IPL_SIDE_ALL,0);
  138. iplSetBorderMode(grayImage2,IPL_BORDER_CONSTANT,IPL_SIDE_ALL,0);
  139. cvComputeCorrespondEpilines(wpoints1,1,fundMatr,corrLines2);
  140. cvComputeCorrespondEpilines(wpoints2,2,fundMatr,corrLines1);
  141. //正向引导匹配(1->2)
  142. for(i=0;i<cornerCount;i++)
  143. {
  144. minnorm=1.0e8;
  145. mat12[i]=-1;//if mat*[i]<0 canot found matches
  146. x1=cvRound(corners1[i].x);y1=cvRound(corners1[i].y);
  147. cvSetImageROI(grayImage1,cvRect(x1-patch_radius,y1-patch_radius,patch_radius*2+1,patch_radius*2+1));
  148. cvSetZero(patchimg1);
  149. iplCopy(grayImage1,patchimg1);
  150. cvSetImageROI(grayImage1,cvRect(0,0,grayImage1->width,grayImage1->height));
  151. CvMat pnt1,pnt2;
  152. CvMat lin1,lin2;
  153. cvGetCol(wpoints1,&pnt1,i);
  154. cvGetCol(corrLines2,&lin2,i);
  155. for(int j=0;j<cornerCount;j++)
  156. {
  157. x2=cvRound(corners2[j].x);y2=cvRound(corners2[j].y);
  158. if((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)<max_disparity*max_disparity)
  159. {
  160.                 cvGetCol(wpoints2,&pnt2,j);
  161.                 cvGetCol(corrLines1,&lin1,j);
  162.                 double dist1,dist2;
  163.                 dist1=fabs(cvDotProduct(&pnt1,&lin1));
  164.                 dist2=fabs(cvDotProduct(&pnt2,&lin2));
  165. if(dist1<epidis && dist2<epidis)
  166. {
  167. cvSetImageROI(grayImage2,cvRect(x2-patch_radius,y2-patch_radius,patch_radius*2+1,patch_radius*2+1));
  168. cvSetZero(patchimg2);
  169. iplCopy(grayImage2,patchimg2);
  170. cvSetImageROI(grayImage2,cvRect(0,0,grayImage2->width,grayImage2->height));
  171. norm=iplNorm(patchimg1,patchimg2,IPL_L2);//相关系数(图像2范数)
  172. if(norm<minnorm)
  173. {
  174. minnorm=norm;
  175. mat12[i]=j;
  176. }
  177. }
  178. }
  179. }
  180. cvSetImageROI(grayImage1,cvRect(0,0,grayImage1->width,grayImage1->height));
  181. cvSetImageCOI(grayImage1,0);
  182. }
  183. //反向引导匹配(2->1)
  184. for(i=0;i<cornerCount;i++)
  185. {
  186. minnorm=1.0e8;
  187. mat21[i]=-1;//if mat*[i]<0 canot found matches
  188. x2=cvRound(corners2[i].x);y2=cvRound(corners2[i].y);
  189. cvSetImageROI(grayImage2,cvRect(x2-patch_radius,y2-patch_radius,patch_radius*2+1,patch_radius*2+1));
  190. cvSetZero(patchimg2);
  191. iplCopy(grayImage2,patchimg2);
  192. cvSetImageROI(grayImage2,cvRect(0,0,grayImage2->width,grayImage2->height));
  193. CvMat pnt1,pnt2;
  194. CvMat lin1,lin2;
  195. cvGetCol(wpoints2,&pnt2,i);
  196. cvGetCol(corrLines1,&lin1,i);
  197. for(int j=0;j<cornerCount;j++)
  198. {
  199. x1=cvRound(corners1[j].x);y1=cvRound(corners1[j].y);
  200. if((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)<max_disparity*max_disparity)
  201. {
  202.                 cvGetCol(wpoints1,&pnt1,j);
  203.                 cvGetCol(corrLines2,&lin2,j);
  204.                 double dist1,dist2;
  205.                 dist1=fabs(cvDotProduct(&pnt1,&lin1));
  206.                 dist2=fabs(cvDotProduct(&pnt2,&lin2));
  207. if(dist1<epidis && dist2<epidis)
  208. {
  209. cvSetImageROI(grayImage1,cvRect(x1-patch_radius,y1-patch_radius,patch_radius*2+1,patch_radius*2+1));
  210. cvSetZero(patchimg1);
  211. iplCopy(grayImage1,patchimg1);
  212. cvSetImageROI(grayImage1,cvRect(0,0,grayImage1->width,grayImage1->height));
  213. norm=iplNorm(patchimg1,patchimg2,IPL_L2);//相关系数(图像2范数)
  214. if(norm<minnorm)
  215. {
  216. minnorm=norm;
  217. mat21[i]=j;
  218. }
  219. }
  220. }
  221. }
  222. cvSetImageROI(grayImage2,cvRect(0,0,grayImage2->width,grayImage2->height));
  223. cvSetImageCOI(grayImage2,0);
  224. }
  225. //重组匹配点矩阵
  226. for(i=0;i<cornerCount;i++)
  227. {
  228. if(mat12[i]>=0)//(1->2)
  229. if(mat12[i]>=0&&mat21[mat12[i]]==i)//(1->2)&&(2->1)
  230. {
  231. matches1[*nmatches].x=corners1[i].x;matches1[*nmatches].y=corners1[i].y;
  232. matches2[*nmatches].x=corners2[mat12[i]].x;matches2[*nmatches].y=corners2[mat12[i]].y;
  233. (*nmatches)++;
  234. }
  235. }
  236.     cvReleaseMat(&points1);
  237.     cvReleaseMat(&points2);
  238.     cvReleaseMat(&wpoints1);
  239.     cvReleaseMat(&wpoints2);
  240.     cvReleaseMat(&corrLines1);
  241.     cvReleaseMat(&corrLines2);
  242. cvReleaseImage(&patchimg1);
  243. cvReleaseImage(&patchimg2);
  244. }