




已阅读5页,还剩13页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
opencv 检测直线、线段、圆、矩形检测直线:cvHoughLines,cvHoughLines2检测圆:cvHoughCircles检测矩形:opencv中没有对应的函数,下面有段代码可以检测矩形,是通过先找直线,然后找到直线平行与垂直的四根线。检测直线代码:/* This is a standalone program. Pass an image name as a first parameter of the program.Switch between standard and probabilistic Hough transform by changing #if 1 to #if 0 and back */#include #include #include int main(int argc, char* argv)const char* filename = argc = 2 ? argv1 : pic1.png;IplImage* src = cvLoadImage( filename, 0 );IplImage* dst;IplImage* color_dst;CvMemStorage* storage = cvCreateMemStorage(0);CvSeq* lines = 0;int i;if( !src )return -1;dst = cvCreateImage( cvGetSize(src), 8, 1 );color_dst = cvCreateImage( cvGetSize(src), 8, 3 );cvCanny( src, dst, 50, 200, 3 );cvCvtColor( dst, color_dst, CV_GRAY2BGR );#if 0lines = cvHoughLines2( dst, storage, CV_HOUGH_STANDARD, 1, CV_PI/180, 100, 0, 0 );for( i = 0; i total,100); i+ )float* line = (float*)cvGetSeqElem(lines,i);float rho = line0;float theta = line1;CvPoint pt1, pt2;double a = cos(theta), b = sin(theta);double x0 = a*rho, y0 = b*rho;pt1.x = cvRound(x0 + 1000*(-b);pt1.y = cvRound(y0 + 1000*(a);pt2.x = cvRound(x0 - 1000*(-b);pt2.y = cvRound(y0 - 1000*(a);cvLine( color_dst, pt1, pt2, CV_RGB(255,0,0), 3, CV_AA, 0 );#elselines = cvHoughLines2( dst, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI/180, 50, 50, 10 );for( i = 0; i total; i+ )CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);cvLine( color_dst, line0, line1, CV_RGB(255,0,0), 3, CV_AA, 0 );#endifcvNamedWindow( Source, 1 );cvShowImage( Source, src );cvNamedWindow( Hough, 1 );cvShowImage( Hough, color_dst );cvWaitKey(0);return 0;检测圆代码:#include #include #include int main(int argc, char* argv)IplImage* img;if( argc = 2 & (img=cvLoadImage(argv1, 1)!= 0)IplImage* gray = cvCreateImage( cvGetSize(img), 8, 1 );CvMemStorage* storage = cvCreateMemStorage(0);cvCvtColor( img, gray, CV_BGR2GRAY );cvSmooth( gray, gray, CV_GAUSSIAN, 9, 9 ); / smooth it, otherwise a lot of false circles may be detectedCvSeq* circles = cvHoughCircles( gray, storage, CV_HOUGH_GRADIENT, 2, gray-height/4, 200, 100 );int i;for( i = 0; i total; i+ )float* p = (float*)cvGetSeqElem( circles, i );cvCircle( img, cvPoint(cvRound(p0),cvRound(p1), 3, CV_RGB(0,255,0), -1, 8, 0 );cvCircle( img, cvPoint(cvRound(p0),cvRound(p1), cvRound(p2), CV_RGB(255,0,0), 3, 8, 0 );cvNamedWindow( circles, 1 );cvShowImage( circles, img );return 0;检测矩形代码:/*在程序里找寻矩形*/#ifdef _CH_#pragma package #endif#ifndef _EiC#include cv.h#include highgui.h#include #include #include #endif int thresh = 50;IplImage* img = 0;IplImage* img0 = 0;CvMemStorage* storage = 0;CvPoint pt4;const char* wndname = Square Detection Demo; / helper function:/ finds a cosine of angle between vectors/ from pt0-pt1 and from pt0-pt2double angle( CvPoint* pt1, CvPoint* pt2, CvPoint* pt0 ) double dx1 = pt1-x - pt0-x; double dy1 = pt1-y - pt0-y; double dx2 = pt2-x - pt0-x;double dy2 = pt2-y - pt0-y;return (dx1*dx2 + dy1*dy2)/sqrt(dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10); / returns sequence of squares detected on the image./ the sequence is stored in the specified memory storageCvSeq* findSquares4( IplImage* img, CvMemStorage* storage )CvSeq* contours;int i, c, l, N = 11;CvSize sz = cvSize( img-width & -2, img-height & -2 );IplImage* timg = cvCloneImage( img ); / make a copy of input imageIplImage* gray = cvCreateImage( sz, 8, 1 );IplImage* pyr = cvCreateImage( cvSize(sz.width/2, sz.height/2), 8, 3 );IplImage* tgray;CvSeq* result;double s, t;/ create empty sequence that will contain points -/ 4 points per square (the squares vertices)CvSeq* squares = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvPoint), storage );/ select the maximum ROI in the image/ with the width and height divisible by 2cvSetImageROI( timg, cvRect( 0, 0, sz.width, sz.height ); / down-scale and upscale the image to filter out the noisecvPyrDown( timg, pyr, 7 );cvPyrUp( pyr, timg, 7 );tgray = cvCreateImage( sz, 8, 1 ); / find squares in every color plane of the imagefor( c = 0; c 3; c+ ) / extract the c-th color planecvSetImageCOI( timg, c+1 );cvCopy( timg, tgray, 0 ); / try several threshold levels for( l = 0; l N; l+ ) / hack: use Canny instead of zero threshold level. / Canny helps to catch squares with gradient shading if( l = 0 ) / apply Canny. Take the upper threshold from slider / and set the lower to 0 (which forces edges merging)cvCanny( tgray, gray, 0, thresh, 5 ); / dilate canny output to remove potential / holes between edge segments cvDilate( gray, gray, 0, 1 ); else / apply threshold if l!=0:/ tgray(x,y) = gray(x,y) total = 4 &fabs(cvContourArea(result,CV_WHOLE_SEQ) 1000 &cvCheckContourConvexity(result) )s = 0;for( i = 0; i = 2 )t = fabs(angle(CvPoint*)cvGetSeqElem( result, i ),(CvPoint*)cvGetSeqElem( result, i-2 ),(CvPoint*)cvGetSeqElem( result, i-1 );s = s t ? s : t;/ if cosines of all angles are small / (all angles are 90 degree) then write quandrange/ vertices to resultant sequenceif( s 0.3 )for( i = 0; i h_next;/ release all the temporary imagescvReleaseImage( &gray );cvReleaseImage( &pyr );cvReleaseImage( &tgray );cvReleaseImage( &timg );return squares;/ the function draws all the squares in the imagevoid drawSquares( IplImage* img, CvSeq* squares ) CvSeqReader reader;IplImage* cpy = cvCloneImage( img );int i;/ initialize reader of the sequencecvStartReadSeq( squares, &reader, 0 );/ read 4 sequence elements at a time (all vertices of a square)for( i = 0; i total; i += 4 )CvPoint* rect = pt;int count = 4;/ read 4 verticesmemcpy( pt, reader.ptr, squares-elem_size );CV_NEXT_SEQ_ELEM( squares-elem_size, reader );memcpy( pt + 1, reader.ptr, squares-elem_size );CV_NEXT_SEQ_ELEM( squares-elem_size, reader );memcpy( pt + 2, reader.ptr, squares-elem_size );CV_NEXT_SEQ_ELEM( squares-elem_size, reader );memcpy( pt + 3, reader.ptr, squares-elem_size );CV_NEXT_SEQ_ELEM( squares-elem_size, reader );/ draw the square as a closed polylinecvPolyLine( cpy, &rect, &count, 1, 1, CV_RGB(0,255,0), 3, CV_AA, 0 );/ show the resultant imagecvShowImage( wndname, cpy );cvReleaseImage( &cpy ); void on_trackbar( int a )if( img )drawSquares( img, findSquares4( img, storage ) ); char* names = pic1.png, pic2.png, pic3.png,pic4.png, pic5.png, pic6.png, 0 ;int main(int argc, char* argv)int i, c;/ create memory storage that will contain all the dynamic datastorage = cvCreateMemStorage(0);for( i = 0; namesi != 0; i+ )/ load i-th imageimg0 = cvLoadImage( namesi, 1 );if( !img0 )printf(Couldnt load %s/n, namesi );continue;img = cvCloneImage( img0 );/ create window and a trackbar (slider) with parent image and set callback/ (the slider regulates upper threshold, passed to Canny edge detector)cvNamedWindow( wndname, 1 );cvCreateTrackbar( canny thresh, wndname, &thresh, 1000, on_trackbar );/ force the image processingon_trackbar(0);/ wait for key./ Also the function cvWaitKey takes care of event processingc = cvWaitKey(0);/ release both imagescvReleaseImage( &img );cvReleaseImage( &img0 );/ clear memory storage - reset free space positioncvClearMemStorage( storage );if( c = 27 )break; cvDestroyWindow( wndname ); return 0;#ifdef _EiCmain(1,squares.c);#endif其它参考博客:1、/superdont/article/details/66642542、/%CE%C4%BF%A1%B5%C4%CF%A3%CD%FB/blog/item/3a5cb2079158b304738b65f2.html#include #include #include int main()IplImage* src;if( (src=cvLoadImage(5.bmp, 1) != 0)IplImage* dst = cvCreateImage( cvGetSize(src), 8, 1 );IplImage* color_dst = cvCreateImage( cvGetSize(src), 8, 3 );CvMemStorage* storage = cvCreateMemStorage(0);/存储检测到线段,当然可以是N*1的矩阵数列,如果实际的直线数量多余N,那么最大可能数目的线段被返回CvSeq* lines = 0;int i;IplImage* src1=cvCreateImage(cvSize(src-width,src-height),IPL_DEPTH_8U,1);cvCvtColor(src, src1, CV_BGR2GRAY); /把src转换成灰度图像保存在src1中,注意进行边缘检测一定要换成灰度图cvCanny( src1, dst, 50, 200, 3 );/参数50,200的灰度变换cvCvtColor( dst, color_dst, CV_GRAY2BGR );#if 1lines = cvHoughLines2( dst, storage, CV_HOUGH_STANDARD, 1, CV_PI/180, 150, 0, 0 );/标准霍夫变换后两个参数为0,由于line_storage是内存空间,所以返回一个CvSeq序列结构的指针for( i = 0; i total; i+ )float* line = (float*)cvGetSeqElem(lines,i);/用GetSeqElem得到直线float rho = line0;float theta = line1;/对于SHT和MSHT(标准变换)这里line0,line1是rho(与像素相关单位的距离精度)和theta(弧度测量的角度精度)CvPoint pt1, pt2;double a = cos(theta), b = sin(theta);if( fabs(a) height;else if( fabs(b) width;elsept1.x = 0;pt1.y = cvRound(rho/b);pt2.x = cvRound(rho/a);pt2.y = 0;cvLine( color_dst, pt1, pt2, CV_RGB(255,0,0), 3, 8 );#elselines = cvHoughLines2( dst, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI/180, 80, 30, 10 );for( i = 0; i total; i+ )CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);cvLine( color_dst, line0, line1, CV_RGB(255,0,0), 3, 8 );#endifcvNamedWindow( Source, 1 );cvShowImage( Source, src );cvNamedWindow( Hough, 1 );cvShowImage( Hough, color_dst );cvWaitKey(0);line_storage 检测到的线段存储仓. 可以是内存存储仓 (此种情况下,一个线段序列在存储仓中被创建,并
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 肿瘤影像组学分析-洞察及研究
- 2025中级导游等级考试(汉语言文学知识)复习题及答案
- 2025年征兵心理纸笔测试试题及答案
- 航空燃油喷射工程中的环境影响分析-洞察及研究
- 农遗法律保护框架-洞察及研究
- 2025年度员工正式聘用合同协议
- 2025年度供货协议合同
- 德阳高二期末考试卷子及答案
- 出入境检验检疫
- 2025建筑混凝土用碎石采购合同
- GB/T 20801.6-2020压力管道规范工业管道第6部分:安全防护
- GB/T 19355.2-2016锌覆盖层钢铁结构防腐蚀的指南和建议第2部分:热浸镀锌
- 主编-孙晓岭组织行为学-课件
- 核心素养视角下教师专业发展课件
- 企业信用信息公告系统年度报告模板:非私营其他企业
- 施工员钢筋工程知识培训(培训)课件
- 质量管理体系审核中常见的不合格项
- 共用水电费分割单模板
- 《阿房宫赋》全篇覆盖理解性默写
- 学校体育学(第三版)ppt全套教学课件
- NCStudioGen6A编程手册
评论
0/150
提交评论