基于OpenCV实现的工件轮廓检测系统_第1页
基于OpenCV实现的工件轮廓检测系统_第2页
基于OpenCV实现的工件轮廓检测系统_第3页
基于OpenCV实现的工件轮廓检测系统_第4页
基于OpenCV实现的工件轮廓检测系统_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

基于OpenCV实现的工件轮廓检测系统功能介绍获取USB摄像机中的实时视频流,检测出符合特征的标准形状工件外轮廓,包括圆形,三角形,四边形,五边形,六边形等规则形状工件的检测。开发环境为Win764Bit+VS2013+OpenCV2.4.9。实现代码包含main.cpp,shapeDetection.cpp,shapeDetection.h三个源文件。/******************************************************************************************//*shapeDetection.cpp--Projet2016/11/23*//*--ByXiaoYufei *//******************************************************************************************/#include"shapeDetection.h"/***triangles,rectanglesandcirclesdetection*/SimpleShapesdetectShapes(intnum_cam,intlowThreshold){ staticVideoCapturecap(num_cam);//openthewebcam staticMatimg_src,img_dst;//scr:source&dst:destination img_src.release();img_dst.release(); staticMatimg_gray,img_canny; img_gray.release();img_canny.release(); staticSimpleShapesshapes; staticvector<vector<Point>>contours;contours.clear(); staticvector<Point>vertices_contour;vertices_contour.clear(); staticvector<vector<Point>>triangles;triangles.clear(); staticvector<RotatedRect>rectangles;rectangles.clear(); staticvector<vector<Point>>pentas;pentas.clear(); staticvector<vector<Point>>hexa;hexa.clear(); staticvector<Cerc>cercles;cercles.clear(); staticintRATIO=3; staticintkernel_size=3; //checkingthewebcam if(!cap.isOpened()){ cout<<"Error-cameracannotbeopenned"<<endl; exit(-1); } //newframefromthewebcam cap>>img_src; //smooththeimageanddownsampleit Matpyr; pyrDown(img_src,pyr); //imshow("downsample",pyr); //waitKey(10); //upsampletheimageandthensmoothit Matpyr_up; pyrUp(pyr,pyr_up); //imshow("upsample",pyr_up); //waitKey(10); //converttoGrayscale cvtColor(img_src,img_gray,CV_BGR2GRAY); //binarisation(Cannyfilter) Canny(img_gray,img_canny,lowThreshold,RATIO*lowThreshold,kernel_size); //findcoutours findContours(img_canny.clone(),contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE); img_dst=img_src.clone(); //findshapesS for(inti=0;i<contours.size();i++){ //approximatecontourwithaccuracyproportional //tothecontourperimeter approxPolyDP(Mat(contours[i]),vertices_contour,arcLength(Mat(contours[i]),true)*0.02,true); //skipsmallornon-convexobjects滤除小的或非凸面的物体 if(fabs(contourArea(contours[i]))<100||!isContourConvex(vertices_contour)){ continue; } if(vertices_contour.size()==3){//TRIANGLES triangles.push_back(vertices_contour);//addtothesetofdetectedtriangles //drawing for(inti=0;i<3;i++) line(img_dst,vertices_contour[i],vertices_contour[(i+1)%3],Scalar(0,128,255),3); //label setLabel(img_dst,"TRI",contours[i]); } elseif(vertices_contour.size()>=4&&vertices_contour.size()<=6) { //拟合出的多边形包含了4-6个顶点,说明为多边形(矩形,五边形,六边形) intnb_vertices=vertices_contour.size(); //calculationof"cos"fromallcorners vector<double>cos; for(intj=2;j<nb_vertices+1;j++){ cos.push_back(angleCos(vertices_contour[j%nb_vertices],vertices_contour[j-2],vertices_contour[j-1])); } //storageof"cos"inascendingorder sort(cos.begin(),cos.end());//以升序排列Cosin角度值 //savetheminandmaxvaluesof"cos" doublemincos=cos.front();//最小值 doublemaxcos=cos.back();//最大值 //checkthatthecornersarerightangles if(nb_vertices==4&&mincos>=-0.1&&maxcos<=0.3) {//RECTANGLES,90度对应的cosin值为0,误差容许范围为0.3。 RotatedRectrotRect=minAreaRect(contours[i]); rectangles.push_back(rotRect);//addtothesetofdetectedrectangles //drawing Point2fcoins_rect[4]; rotRect.points(coins_rect); for(inti=0;i<4;i++) line(img_dst,coins_rect[i],coins_rect[(i+1)%4],Scalar(147,20,255),2); //label stringstreamsentence; sentence<<"RECTrot="<<rotRect.angle;//旋转角度 stringrotLabel=sentence.str(); setLabel(img_dst,rotLabel,contours[i]); } elseif(nb_vertices==5&&mincos>=-0.34&&maxcos<=-0.27)//五边形 { pentas.push_back(vertices_contour); //string setLabel(img_dst,"PENTA",contours[i]); vector<Point>::const_iteratoritp=vertices_contour.begin(); while(itp!=(vertices_contour.end()-1)) { line(img_dst,*itp,*(itp+1),Scalar(255,255,0),2); ++itp; } line(img_dst,*(vertices_contour.begin()),*(vertices_contour.end()-1),Scalar(255,255,0),2); } elseif(nb_vertices==6&&mincos>=-0.55&&maxcos<=-0.45)//六边形 { setLabel(img_dst,"HEXA",vertices_contour); hexa.push_back(vertices_contour); vector<Point>::const_iteratoritp=vertices_contour.begin(); while(itp!=(vertices_contour.end()-1)) { line(img_dst,*itp,*(itp+1),Scalar(0,255,0),2); ++itp; } line(img_dst,*(vertices_contour.begin()),*(vertices_contour.end()-1),Scalar(0,255,0),2); } } else{//CIRCLES doublearea=contourArea(contours[i]);//计算面积 Rectr=boundingRect(contours[i]);//包围矩形 intradius=r.width/2; //判断宽高比(圆的宽高比为1)和面积大小(圆的面积PI*radius^2) if(abs(1-((double)r.width/r.height))<=0.15&&abs(1-(area/(CV_PI*pow(radius,2))))<=0.15){ Cerccercle; minEnclosingCircle(contours[i],cercle.center,cercle.radius); cercles.push_back(cercle);//addtothesetofdetectedcircles //drawing circle(img_dst,cercle.center,cercle.radius,CV_RGB(255,255,0),2); //label setLabel(img_dst,"CIR",contours[i]); } } } //savedata shapes.img_src=img_src; shapes.img_dst=img_dst; shapes.img_bw=img_canny; shapes.rectangles=rectangles; shapes.triangles=triangles; shapes.cercles=cercles; shapes.hexa=hexa; shapes.penta=pentas; //printfsomeinformations if(triangles.size()==0&&rectangles.size()==0&&cercles.size()==0&&pentas.size()==0&&hexa.size()==0)printf("NOTHING\n"); else{ if(triangles.size()>0) printf("TRIANGLE\n"); if(rectangles.size()>0) printf("RECTANGLE\n"); if(cercles.size()>0) printf("CERCLE\n"); if(pentas.size()>0) printf("PENTAGON\n"); if(hexa.size()>0) printf("HEXAGON\n"); } //senddata returnshapes;}/***Helperfunctiontofindacosineofanglebetweenvectors*frompt0->pt1andpt0->pt2*/doubleangleCos(Pointpt1,Pointpt2,Pointpt0){ doubledx1=pt1.x-pt0.x; doubledy1=pt1.y-pt0.y; doubledx2=pt2.x-pt0.x; doubledy2=pt2.y-pt0.y; return(dx1*dx2+dy1*dy2)/sqrt((dx1*dx1+dy1*dy1)*(dx2*dx2+dy2*dy2));}/***Viewing"label"onthecontourdetectedinthepicture"img"*/voidsetLabel(Mat&img,conststringlabel,vector<Point>&contour){ staticintfontface=FONT_HERSHEY_SIMPLEX; staticdoublescale=0.4; staticintthickness=1; staticintbaseline=0; Sizetext=getTextSize(label,fontface,scale,thickness,&baseline); Rectr=boundingRect(contour);//returnsthesmallestrectanglecontainingthe"contour" Pointpt(r.x+((r.width-text.width)/2),r.y+((r.height+text.height)/2)); rectangle(img,pt+Point(0,baseline),pt+Point(text.width,-text.height),CV_RGB(255,255,255),CV_FILLED); putText(img,label,pt,fontface,scale,CV_RGB(0,0,0),thickness,8);}/******************************************************************************************//*shapeDetection.h--Projet2016/11/23*//*--ByXiaoYufei*//******************************************************************************************/#include<opencv2/core/core.hpp>#include<opencv2/highgui/highgui.hpp>#include<opencv2/imgproc/imgproc.hpp>#include<cmath>#include<iostream>usingnamespacecv;usingnamespacestd;structCerc{ Point2fcenter; floatradius;};structSimpleShapes{ Matimg_src; Matimg_dst; Matimg_bw; vector<RotatedRect>rectangles;//矩形 vector<vector<Point>>triangles;//三角形 vector<Cerc>cercles;//圆形 vector<vector<Point>>penta;//五边形 vector<vector<Point>>hexa;//六边形};SimpleShapesdetectShapes(int,int);doubleangleCos(Point,Point,Point);voidsetLabel(Mat&,conststring,vector<Point>&);/******************************************************************************************//*main.cpp--Projet2016/11/23 *//*--ByXiaoYufei *//******************************************************************************************/#include"shapeDetection.h"//GlobalvariablesSimpleShapesshapes;intedgeTh

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论