opencv 实现的椭圆检测的源代码.docx

上传人:牧羊曲112 文档编号:3061924 上传时间:2023-03-10 格式:DOCX 页数:9 大小:39.34KB
返回 下载 相关 举报
opencv 实现的椭圆检测的源代码.docx_第1页
第1页 / 共9页
opencv 实现的椭圆检测的源代码.docx_第2页
第2页 / 共9页
opencv 实现的椭圆检测的源代码.docx_第3页
第3页 / 共9页
opencv 实现的椭圆检测的源代码.docx_第4页
第4页 / 共9页
opencv 实现的椭圆检测的源代码.docx_第5页
第5页 / 共9页
亲,该文档总共9页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《opencv 实现的椭圆检测的源代码.docx》由会员分享,可在线阅读,更多相关《opencv 实现的椭圆检测的源代码.docx(9页珍藏版)》请在三一办公上搜索。

1、opencv 实现的椭圆检测的源代码1. / readbmp.cpp : Defines the entry point for the console application. 2. / 3. 4. #include stdafx.h 5. #include 6. 7. #include ipl.h 8. #include cv.h 9. /#include _cv.h 10. /#include vlgrfmts.h 11. 12. /- 13. /UTILITY FUNCTIONS 14. / 15. / Read in a BMP and convert it into an IPL

2、16. / fin The file name of the BMP with path 17. / plannar If set, convert the image to plannar 18. / flip If set, mirror the image around the horizontal axis, set 19. / IPL_ORIGIN_BL) 20. IplImage * readBMP2IPL(char *fin, int plannar = 0, int flip = 0) 21. 22. int filter; 23. int width=0,height=0,c

3、olor=55,hdr_ret=0,close_ret = 0,rd_ret = 0; 24. IplImage *I; 25. filter = gr_fmt_find_filter(fin ); /Get file type 26. hdr_ret = gr_fmt_read_header( filter, &width, &height, &color );/image params 27. I = cvCreateImageHeader( cvSize(width, height), IPL_DEPTH_8U, 3); /create image header 28. I-align

4、= IPL_ALIGN_QWORD; /fix the problem that cvCreateImage uses align 4 29. I-depth != IPL_DEPTH_32F ? iplAllocateImage( I, 0, 0 ) :iplAllocateImageFP( I, 0, 0 ); 30. rd_ret = gr_fmt_read_data(filter, I-imageData, I-widthStep, color); /read in the data, origin TL 31. close_ret = gr_fmt_close_filter( fil

5、ter ); 32. if(plannar) /If plannar order desired, turn it into plannar order 33. 34. IplImage *Ip = iplCreateImageHeader(3, 0, IPL_DEPTH_8U, RGB,BGR, IPL_DATA_ORDER_PLANE, I-origin, IPL_ALIGN_QWORD, width, height, NULL,NULL,NULL,NULL); 35. iplAllocateImage(Ip, 0,0); 36. iplConvert(I,Ip); 37. cvRelea

6、seImage( &I ); 38. I = Ip; 39. 40. if(flip) /User wants IPL_ORIGIN_BL 41. 42. iplMirror(I, I, 0); /flipporuni 43. I-origin = IPL_ORIGIN_BL; 44. 45. return I; 46. 47. 48. / 49. / Write IPL image to disk as a BMP image (not optimized but step by step simplistic) 50. / fout Pathfilename to write to 51.

7、 / I IPL image to be written 52. / 53. / Image fomat may be any of: 54. / dataOrder Plane or Pixel 55. / 1 channel (GRAY) or 3 (RGB) 56. / 8U, 8S, 32F 57. / Origin TL or BL (if BL, image is flipped to conform to TL bmps) 58. / 59. / If image is not RGB or GRAY, RGB is simply imposed on the Image. E.

8、g. if its YUV, 60. / the image will be stored as BGR with B=Y, G=U, and R=V 61. void writeIPL2BMP(char *fout, IplImage *I) 62. 63. IplImage *Isav,*Itmp; 64. Isav = iplCloneImage(I); 65. /Convert to pixel order if needed 66. if(I-dataOrder = IPL_DATA_ORDER_PLANE) 67. 68. Itmp = iplCreateImageHeader(I

9、-nChannels, 69. I-alphaChannel, I-depth, I-colorModel, 70. I-channelSeq, IPL_DATA_ORDER_PIXEL, I-origin, 71. I-align, 72. I-width, I-height, NULL, NULL,NULL,NULL); 73. if(I-depth = IPL_DEPTH_32F) 74. iplAllocateImageFP(Itmp, 1,0.0); 75. else 76. iplAllocateImage(Itmp, 1,0); 77. iplConvert(I, Itmp);

10、78. iplDeallocate (I, IPL_IMAGE_ALL); 79. I = Itmp; 80. 81. /Image is now pixel order. Convert to 8U if needed. 82. if(I-depth = IPL_DEPTH_8S) 83. I-depth = IPL_DEPTH_8U; 84. else if(I-depth = IPL_DEPTH_32F) 85. 86. float min,max; 87. iplMinMaxFP (I, &min, &max); 88. Itmp = iplCreateImageHeader(I-nC

11、hannels, 89. I-alphaChannel, IPL_DEPTH_8U, I-colorModel, 90. I-channelSeq, IPL_DATA_ORDER_PIXEL, I-origin, 91. I-align, 92. I-width, I-height, NULL, NULL,NULL,NULL); 93. iplAllocateImage(Itmp, 1,0); 94. if(min = max) max = (float)(min + 1.0); 95. iplScaleFP (I, Itmp, min, max); 96. iplDeallocate (I,

12、 IPL_IMAGE_ALL); 97. I = Itmp; 98. 99. /Image is now pixel order, 8U. Convert to RGB if needed 100. if(I-nChannels = 3) 101. 102. strcpy(I-colorModel,RGB); 103. strcpy(I-channelSeq,BGR); 104. 105. else if(I-nChannels = 1) 106. 107. Itmp = iplCreateImageHeader(3, 108. I-alphaChannel, IPL_DEPTH_8U, RG

13、B, 109. BGR, IPL_DATA_ORDER_PIXEL, I-origin, I-align, 110. I-width, I-height, NULL, NULL,NULL,NULL); 111. iplAllocateImage(Itmp, 1,0); 112. iplGrayToColor (I, Itmp, 1.0, 1.0, 1.0); 113. iplDeallocate (I, IPL_IMAGE_ALL); 114. I = Itmp; 115. 116. else 117. return; 118. /Image is now pixel order, 8U, R

14、GB, flip it if the origin is BL 119. if(I-origin = IPL_ORIGIN_BL) 120. 121. iplMirror(I, I, 0); 122. 123. /FINALLY, WRITE THE IMAGE: 124. int write_ret = gr_fmt_write_image( I-imageData, I-widthStep, 125. I-width, I-height, I-colorModel0 =R?1:0,fout, bmp); 126. /Return image to its original state 12

15、7. iplDeallocate(I, IPL_IMAGE_ALL); 128. I = Isav; 129. 130. 131. /- 132. 133. void main 134. 135. IplImage * input_img=NULL; 136. int plannar=0; 137. int flip=0; 138. char fin=circles.bmp; 139. char fout=result.bmp; 140. 141. 142. /作一些声明 143. CvMemStorage *storage; 144. int header_size, i, count,le

16、ngth, width; 145. CvSeq *contour; 146. CvBox2D * myBox; /用于画圆和椭圆 147. CvPoint *PointArray; 148. CvPoint2D32f *PointArray32f; 149. CvPoint myCenter; 150. 151. / 分配内存 152. myBox= (CvBox2D *) malloc(sizeof(CvBox2D); 153. header_size = sizeof(CvContour); 154. storage = cvCreateMemStorage (1000); / For F

17、indContours. 155. 156. / 读入图象input_img并转换为IPLIMAGE格式 157. /已经定义 char fin=circles.bmp; 158. input_img=readBMP2IPL(fin,plannar, flip); 159. 160. cout The following is the attribute for current image:n; 161. coutNum of channels: nChannelsENDL; ?input_img- coutcenter.x; 162. myCenter.y = (int) myBox-cen

18、ter.y; 163. 164. /HEIGHT AND WITH (THE 2 AXES OF THE ELLIPSE) 165. length =(int)myBox-size.height; 166. width = (int)myBox-size.width; 167. 168. /AND THE ANGLE 169. float myAngle= myBox-angle; 170. 171. / 检查这个椭圆是否在图象内 172. if (myCenter.x 0) & (myCenter.y 0) 173. 174. /画圆 175. cvCircle(input_img,myCe

19、nter,(int)length/2 ,RGB(255,0,0),1); 176. 177. /画椭圆 178. cvEllipse(input_img, 179. myCenter, 180. cvSize (int)width/2,(int)length/2) , 181. myBox-angle, 182. 0,360,RGB(255,0,255),1); 183. 184. 185. 186. free(PointArray32f ); 187. free(PointArray ); 188. 189. / GOT TO THE NEXT CONTOUR (IF ANY) 190. /取下一个轮廓 191. contour = contour-h_next; 192. 193. 194. 195. free (contour); 196. free (myBox); 197. cvReleaseImage(&m_tmp8uC1); 198. cvReleaseMemStorage(&storage); 199. 200. writeIPL2BMP(fout,input_img);/已定义 char fout=result.bmp; 201. 202.

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号