【opencv+C++】在图像中找四边形

转载 2014年11月15日 14:02:40
  • 1562
[cpp] view plain copy
  1. /*
  2. 这个程序的基本思想是:对输入的图像进行滤波去掉噪音,然后进行canny边缘检测,之后进行膨胀,然后寻找轮廓,对轮廓进行多边形的逼近,检测多边形的点数是否是4而且各个角的的余弦是否是小于某个值,程序中认为是0.3,然后就判断该多边形是四边形,之后根据这四个点画出该图像。
  3. ps:我对程序中余弦定理的使用 感觉公式用错了
  4. */
  5. #include "stdafx.h"
  6. #include "cv.h"
  7. #include "highgui.h"
  8. #include <stdio.h>
  9. #include <math.h>
  10. #include <string.h>
  11. int thresh = 50;
  12. IplImage* img = 0;
  13. IplImage* img0 = 0;
  14. CvMemStorage* storage = 0;
  15. CvPoint pt[4];
  16. const char* wndname = "Square Detection Demo";
  17. // helper function:
  18. // finds a cosine of angle between vectors
  19. // from pt0->pt1 and from pt0->pt2
  20. double angle( CvPoint* pt1, CvPoint* pt2, CvPoint* pt0 )
  21. {
  22. double dx1 = pt1->x - pt0->x;
  23. double dy1 = pt1->y - pt0->y;
  24. double dx2 = pt2->x - pt0->x;
  25. double dy2 = pt2->y - pt0->y;
  26. //1e-10就是“aeb”的形式,表示a乘以10的b次方。
  27. //其中b必须是整数,a可以是小数。
  28. //?余弦定理CosB=(a^2+c^2-b^2)/2ac??所以这里的计算似乎有问题
  29. return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
  30. }
  31. // returns sequence of squares detected on the image.
  32. // the sequence is stored in the specified memory storage
  33. CvSeq* findSquares4( IplImage* img, CvMemStorage* storage )
  34. {
  35. CvSeq* contours;
  36. int i, c, l, N = 11;
  37. CvSize sz = cvSize( img->width & -2, img->height & -2 );
  38. IplImage* timg = cvCloneImage( img ); // make a copy of input image
  39. IplImage* gray = cvCreateImage( sz, 8, 1 );
  40. IplImage* pyr = cvCreateImage( cvSize(sz.width/2, sz.height/2), 8, 3 );
  41. IplImage* tgray;
  42. CvSeq* result;
  43. double s, t;
  44. // create empty sequence that will contain points -
  45. // 4 points per square (the square's vertices)
  46. CvSeq* squares = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvPoint), storage );
  47. // select the maximum ROI in the image
  48. // with the width and height divisible by 2
  49. cvSetImageROI( timg, cvRect( 0, 0, sz.width, sz.height ));
  50. // down-scale and upscale the image to filter out the noise
  51. //使用gaussian金字塔分解对输入图像向下采样,首先对它输入的图像用指定滤波器
  52. //进行卷积,然后通过拒绝偶数的行与列来下采样
  53. cvPyrDown( timg, pyr, 7 );
  54. //函数 cvPyrUp 使用Gaussian 金字塔分解对输入图像向上采样。首先通过在图像中插入 0 偶数行和偶数列,然后对得到的图像用指定的滤波器进行高斯卷积,其中滤波器乘以4做插值。所以输出图像是输入图像的 4 倍大小。
  55. cvPyrUp( pyr, timg, 7 );
  56. tgray = cvCreateImage( sz, 8, 1 );
  57. // find squares in every color plane of the image
  58. for( c = 0; c < 3; c++ )
  59. {
  60. // extract the c-th color plane
  61. //函数 cvSetImageCOI 基于给定的值设置感兴趣的通道。值 0 意味着所有的通道都被选定, 1 意味着第一个通道被选定等等。
  62. cvSetImageCOI( timg, c+1 );
  63. cvCopy( timg, tgray, 0 );
  64. // try several threshold levels
  65. for( l = 0; l < N; l++ )
  66. {
  67. // hack: use Canny instead of zero threshold level.
  68. // Canny helps to catch squares with gradient shading
  69. if( l == 0 )
  70. {
  71. // apply Canny. Take the upper threshold from slider
  72. // and set the lower to 0 (which forces edges merging)
  73. cvCanny( tgray, gray,60, 180, 3 );
  74. // dilate canny output to remove potential
  75. // holes between edge segments
  76. //使用任意结构元素膨胀图像
  77. cvDilate( gray, gray, 0, 1 );
  78. }
  79. else
  80. {
  81. // apply threshold if l!=0:
  82. //        tgray(x,y) = gray(x,y) < (l+1)*255/N ? 255 : 0
  83. //cvThreshold( tgray, gray, (l+1)*255/N, 255, CV_THRESH_BINARY );
  84. cvThreshold( tgray, gray, 50, 255, CV_THRESH_BINARY );
  85. }
  86. // find contours and store them all as a list
  87. cvFindContours( gray, storage, &contours, sizeof(CvContour),
  88. CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );
  89. // test each contour
  90. while( contours )
  91. {
  92. // approximate contour with accuracy proportional
  93. // to the contour perimeter
  94. //用指定精度逼近多边形曲线
  95. result = cvApproxPoly( contours, sizeof(CvContour), storage,
  96. CV_POLY_APPROX_DP, cvContourPerimeter(contours)*0.02, 0 );
  97. // square contours should have 4 vertices after approximation
  98. // relatively large area (to filter out noisy contours)
  99. // and be convex.
  100. // Note: absolute value of an area is used because
  101. // area may be positive or negative - in accordance with the
  102. // contour orientation
  103. //cvContourArea 计算整个轮廓或部分轮廓的面积
  104. //cvCheckContourConvexity测试轮廓的凸性
  105. if( result->total == 4 &&
  106. fabs(cvContourArea(result,CV_WHOLE_SEQ)) > 1000 &&
  107. cvCheckContourConvexity(result) )
  108. {
  109. s = 0;
  110. for( i = 0; i < 5; i++ )
  111. {
  112. // find minimum angle between joint
  113. // edges (maximum of cosine)
  114. if( i >= 2 )
  115. {
  116. t = fabs(angle(
  117. (CvPoint*)cvGetSeqElem( result, i ),
  118. (CvPoint*)cvGetSeqElem( result, i-2 ),
  119. (CvPoint*)cvGetSeqElem( result, i-1 )));
  120. s = s > t ? s : t;
  121. }
  122. }
  123. // if cosines of all angles are small
  124. // (all angles are ~90 degree) then write quandrange
  125. // vertices to resultant sequence
  126. if( s < 0.3 )
  127. for( i = 0; i < 4; i++ )
  128. cvSeqPush( squares,
  129. (CvPoint*)cvGetSeqElem( result, i ));
  130. }
  131. // take the next contour
  132. contours = contours->h_next;
  133. }
  134. }
  135. }
  136. // release all the temporary images
  137. cvReleaseImage( &gray );
  138. cvReleaseImage( &pyr );
  139. cvReleaseImage( &tgray );
  140. cvReleaseImage( &timg );
  141. return squares;
  142. }
  143. // the function draws all the squares in the image
  144. void drawSquares( IplImage* img, CvSeq* squares )
  145. {
  146. CvSeqReader reader;
  147. IplImage* cpy = cvCloneImage( img );
  148. int i;
  149. // initialize reader of the sequence
  150. cvStartReadSeq( squares, &reader, 0 );
  151. // read 4 sequence elements at a time (all vertices of a square)
  152. for( i = 0; i < squares->total; i += 4 )
  153. {
  154. CvPoint* rect = pt;
  155. int count = 4;
  156. // read 4 vertices
  157. memcpy( pt, reader.ptr, squares->elem_size );
  158. CV_NEXT_SEQ_ELEM( squares->elem_size, reader );
  159. memcpy( pt + 1, reader.ptr, squares->elem_size );
  160. CV_NEXT_SEQ_ELEM( squares->elem_size, reader );
  161. memcpy( pt + 2, reader.ptr, squares->elem_size );
  162. CV_NEXT_SEQ_ELEM( squares->elem_size, reader );
  163. memcpy( pt + 3, reader.ptr, squares->elem_size );
  164. CV_NEXT_SEQ_ELEM( squares->elem_size, reader );
  165. // draw the square as a closed polyline
  166. cvPolyLine( cpy, &rect, &count, 1, 1, CV_RGB(0,255,0), 3, CV_AA, 0 );
  167. }
  168. // show the resultant image
  169. cvShowImage( wndname, cpy );
  170. cvReleaseImage( &cpy );
  171. }
  172. void on_trackbar( int a )
  173. {
  174. if( img )
  175. drawSquares( img, findSquares4( img, storage ) );
  176. }
  177. char* names[] = { "E:\\1.jpg", "E:\\2.jpg", "E:\\3.jpg",
  178. "E:\\4.jpg", "E:\\5.jpg", 0 };
  179. int main(int argc, char** argv)
  180. {
  181. int i, c;
  182. // create memory storage that will contain all the dynamic data
  183. storage = cvCreateMemStorage(0);
  184. for( i = 0; names[i] != 0; i++ )
  185. {
  186. // load i-th image
  187. img0 = cvLoadImage( names[i], 1 );
  188. if( !img0 )
  189. {
  190. printf("Couldn't load %s\n", names[i] );
  191. continue;
  192. }
  193. img = cvCloneImage( img0 );
  194. // create window and a trackbar (slider) with parent "image" and set callback
  195. // (the slider regulates upper threshold, passed to Canny edge detector)
  196. cvNamedWindow( wndname,0 );
  197. cvCreateTrackbar( "canny thresh", wndname, &thresh, 1000, on_trackbar );
  198. // force the image processing
  199. on_trackbar(0);
  200. // wait for key.
  201. // Also the function cvWaitKey takes care of event processing
  202. c = cvWaitKey(0);
  203. // release both images
  204. cvReleaseImage( &img );
  205. cvReleaseImage( &img0 );
  206. // clear memory storage - reset free space position
  207. cvClearMemStorage( storage );
  208. if( c == 27 )
  209. break;
  210. }
  211. cvDestroyWindow( wndname );
  212. return 0;
  213. }

【opencv+C++】在图像中找四边形相关推荐

  1. opencv 从原始的图像中找出ROI区域

    http://blog.csdn.net/chentravelling/article/details/45331225 opencv 从原始的图像中找出ROI区域 #include <iost ...

  2. OpenCV实现在图像中写入汉字

    由于OpenCV自带的cvInitFont和cvPutText函数不支持向图像中写入中文,参考http://www.opencv.org.cn/forum/viewtopic.php?t=2083 中 ...

  3. OpenCV访问Mat图像中每个像素的值

    原文:http://blog.csdn.net/xiaowei_cqu/article/details/7771760 matlab中, a=[1,2,3;4,5,6;7,8,9] a(1,2) 第一 ...

  4. 在图像中找任意曲线记录

    总结 传统方法找线的工作量在单一环境下是少于深度学习的.但是遇到复杂情况,深度学习的工作量远小于传统方法. 传统方法缺陷分析 传统方法在复杂环境下存在的本质问题是,传统方法的假设,比如我看了最新的论文 ...

  5. 使用 PyTesseract 和 OpenCV 从表格图像中提取文本

    Text Extraction from a Table Image, using PyTesseract and OpenCV – Fazlur Rahmanhttps://fazlurnu.com ...

  6. Python Opencv cv2提取图像中某种特定颜色区域(例如黑字白纸背景下的红色公章提取),并将纯色背景透明化

    拜拜PHOTOSHOP- 领导突然让我帮他把公章从图片中抠出来,在确保了不是要做坏事的情况下,我打开了PHOTOSHOP,用魔棒工具一点一点抠,但由于魔棒工具的原理是对比临近区域像素差值,导致封闭字体 ...

  7. opencv之对图像中的点做几何变换

    代码展示 # -*- coding:utf-8 -*- import cv2 import numpy as npdef homo(mat):if 2 == mat.ndim:return np.vs ...

  8. python opencv 分别取出图像中黑白部分

    将图片中的黑白部分分别提出来需要用到cv2.threshold(src, thresh, maxval, type)函数,其中参数分别表示为: src:表示的是图片源 thresh:表示的是阈值(起始 ...

  9. OpenCV 检测出图像中的“对号”

    测试图片: 测试结果: int main(int argc, char* argv[]) {IplImage* src , *srcContours ,*srcHErode,*srcVErode ,* ...

  10. 《OpenCv视觉之眼》Python图像处理十六:Opencv图像处理实战一之图像中的硬币检测

    本专栏主要介绍如果通过OpenCv-Python进行图像处理,通过原理理解OpenCv-Python的函数处理原型,在具体情况中,针对不同的图像进行不同等级的.不同方法的处理,以达到对图像进行去噪.锐 ...

最新文章

  1. 各种oracle索引类型介绍,各种Oracle索引类型介绍
  2. MD5计算,一个扩展类,哪里都能用
  3. 8、play框架中持久层操作
  4. TDD开发模式实现代码功能逻辑(自己总结,持续更新)
  5. Mac安装metasploit-framework【亲测有用】
  6. 【白皮书分享】2022新职业教育洞察白皮书:“职”成机遇,“育”见未来.pdf...
  7. 广告智能定向技术lookalike
  8. python自动化接口测试excel脚本_python+requests+excel 实现接口自动化测试
  9. Linux网络命令合集
  10. 问卷java_Java 问卷调查
  11. 职场必备:十句外企 office 常用英语
  12. 关于log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFa
  13. KafkaController机制(六):Zookeeper Listener之TopicDeletionManager与DeleteTopicsListener
  14. 交通流模型仿真(Traffic flow)
  15. 获取上一个月的同一天
  16. 区块链国家队BSN落地杭州,5大应用加速“城市大脑”进化
  17. 破解企业卓越运营难点,做好研发质量管理闭环,从“救火战役”,到“一次做对”
  18. HTML简单汇总(不全)
  19. 解决vue项目中重复点击导航路由报错
  20. Android:玩转ADB命令(ADB命令使用大全)

热门文章

  1. SqlServer支持多表关联的分页存储过程
  2. onerror捕获异常
  3. 成都文理学院计算机一级还没考过,两次查成绩不一致,合格成不合格?成都文理学院官方回应...
  4. 计算机关机后耗电问题,笔记本电脑关机后电池还会耗电怎么解决
  5. lgg8各个版本_LG正式推出G8SThinQ 搭载骁龙855
  6. 判别多项式有无重因式的方法_几种有理分式分解的方法
  7. flutter用英语怎么说_碍手碍脚用英语怎么说?
  8. 检测同心圆_两“心”携手,共抗心衰——“同心圆”心衰俱乐部走进临安图书馆~...
  9. php for 循环 try_重新学习php基础之循环遍历(for循环和while循环)(六)
  10. HTTP:一次完整的HTTP服务过程