OPENCV特征提取代码总结.docx

上传人:wux****ua 文档编号:9098365 上传时间:2020-04-03 格式:DOCX 页数:21 大小:31.23KB
返回 下载 相关 举报
OPENCV特征提取代码总结.docx_第1页
第1页 / 共21页
OPENCV特征提取代码总结.docx_第2页
第2页 / 共21页
OPENCV特征提取代码总结.docx_第3页
第3页 / 共21页
点击查看更多>>
资源描述
特征提取代码总结来自http:/download.csdn.net/source/3208155#acomment特征提取代码总结颜色提取?颜色直方图提取:Code:#include#include#includeusingnamespacestd;intmain(intargc,char*argv)IplImage*src=cvLoadImage(E:Downloadtest1.jpg,1);IplImage*hsv=cvCreateImage(cvGetSize(src),8,3);IplImage*h_plane=cvCreateImage(cvGetSize(src),8,1);IplImage*s_plane=cvCreateImage(cvGetSize(src),8,1);IplImage*v_plane=cvCreateImage(cvGetSize(src),8,1);IplImage*planes=h_plane,s_plane;/*H分量划分为16个等级,S分量划分为8个等级*/inth_bins=16,s_bins=8;inthist_size=h_bins,s_bins;/*H分量的变化范围*/floath_ranges=0,180;/*S分量的变化范围*/floats_ranges=0,255;float*ranges=h_ranges,s_ranges;/*输入图像转换到HSV颜色空间*/cvCvtColor(src,hsv,CV_BGR2HSV);cvCvtPixToPlane(hsv,h_plane,s_plane,v_plane,0);/*创建直方图,二维,每个维度上均分*/CvHistogram*hist=cvCreateHist(2,hist_size,CV_HIST_ARRAY,ranges,1);/*根据H,S两个平面数据统计直方图*/cvCalcHist(planes,hist,0,0);/*获取直方图统计的最大值,用于动态显示直方图*/floatmax_value;cvGetMinMaxHistValue(hist,0,&max_value,0,0);/*设置直方图显示图像*/intheight=240;intwidth=(h_bins*s_bins*6);IplImage*hist_img=cvCreateImage(cvSize(width,height),8,3);cvZero(hist_img);/*用来进行HSV到RGB颜色转换的临时单位图像*/IplImage*hsv_color=cvCreateImage(cvSize(1,1),8,3);IplImage*rgb_color=cvCreateImage(cvSize(1,1),8,3);intbin_w=width/(h_bins*s_bins);for(inth=0;hh_bins;h+)for(ints=0;ss_bins;s+)inti=h*s_bins+s;/*获得直方图中的统计次数,计算显示在图像中的高度*/floatbin_val=cvQueryHistValue_2D(hist,h,s);intintensity=cvRound(bin_val*height/max_value);/*获得当前直方图代表的颜色,转换成RGB用于绘制*/cvSet2D(hsv_color,0,0,cvScalar(h*180.f/h_bins,s*255.f/s_bins,255,0);cvCvtColor(hsv_color,rgb_color,CV_HSV2BGR);CvScalarcolor=cvGet2D(rgb_color,0,0);cvRectangle(hist_img,cvPoint(i*bin_w,height),cvPoint(i+1)*bin_w,height-intensity),color,-1,8,0);cvNamedWindow(Source,1);cvShowImage(Source,src);cvNamedWindow(H-SHistogram,1);cvShowImage(H-SHistogram,hist_img);cvWaitKey(0);运行效果截图:形状提取?Candy算子对边缘提取:Code:#includecv.h#includecxcore.h#includehighgui.hintmain(intargc,char*argv)/声明IplImage指针IplImage*pImg=NULL;IplImage*pCannyImg=NULL;/载入图像,强制转化为GraypImg=cvLoadImage(E:Downloadtest.jpg,0);/为canny边缘图像申请空间pCannyImg=cvCreateImage(cvGetSize(pImg),IPL_DEPTH_8U,1);/canny边缘检测cvCanny(pImg,pCannyImg,50,150,3);/创建窗口cvNamedWindow(src,1);cvNamedWindow(canny,1);/显示图像cvShowImage(src,pImg);cvShowImage(canny,pCannyImg);/等待按键cvWaitKey(0);/销毁窗口cvDestroyWindow(src);cvDestroyWindow(canny);/释放图像cvReleaseImage(&pImg);cvReleaseImage(&pCannyImg);return0;运行效果截图:?角点提取:Code:#include#includecv.h#includehighgui.h#defineMAX_CORNERS100intmain(void)intcornersCount=MAX_CORNERS;/得到的角点数目CvPoint2D32fcornersMAX_CORNERS;/输出角点集合IplImage*srcImage=0,*grayImage=0,*corners1=0,*corners2=0;inti;CvScalarcolor=CV_RGB(255,0,0);cvNamedWindow(image,1);/LoadtheimagetobeprocessedsrcImage=cvLoadImage(E:Download1.jpg,1);grayImage=cvCreateImage(cvGetSize(srcImage),IPL_DEPTH_8U,1);/copythesourceimagetocopyimageafterconvertingtheformat/复制并转为灰度图像cvCvtColor(srcImage,grayImage,CV_BGR2GRAY);/createemptyimagesossamesizeasthecopiedimages/两幅临时位浮点图像,cvGoodFeaturesToTrack会用到corners1=cvCreateImage(cvGetSize(srcImage),IPL_DEPTH_32F,1);corners2=cvCreateImage(cvGetSize(srcImage),IPL_DEPTH_32F,1);cvGoodFeaturesToTrack(grayImage,corners1,corners2,corners,&cornersCount,0.05,30,/角点的最小距离是0,/整个图像3,0,0.4);printf(numcornersfound:%dn,cornersCount);/开始画出每个点if(cornersCount0)for(i=0;icornersCount;i+)cvCircle(srcImage,cvPoint(int)(cornersi.x),(int)(cornersi.y),2,color,2,CV_AA,0);cvShowImage(image,srcImage);cvSaveImage(imagedst.png,srcImage);cvReleaseImage(&srcImage);cvReleaseImage(&grayImage);cvReleaseImage(&corners1);cvReleaseImage(&corners2);cvWaitKey(0);return0;运行效果截图:?Hough直线提取:Code:#include#include#includeintmain(intargc,char*argv)IplImage*src=cvLoadImage(E:Download2.jpg,0);IplImage*dst;IplImage*color_dst;CvMemStorage*storage=cvCreateMemStorage(0);CvSeq*lines=0;inti;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);#if0lines=cvHoughLines2(dst,storage,CV_HOUGH_STANDARD,1,CV_PI/180,100,0,0);for(i=0;itotal,100);i+)float*line=(float*)cvGetSeqElem(lines,i);floatrho=line0;floattheta=line1;CvPointpt1,pt2;doublea=cos(theta),b=sin(theta);doublex0=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;itotal;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);return0;运行效果截图:?Hough圆提取:Code:#include#include#include#includeusingnamespacestd;intmain(intargc,char*argv)IplImage*img;img=cvLoadImage(E:Download3.jpg,1);IplImage*gray=cvCreateImage(cvGetSize(img),8,1);CvMemStorage*storage=cvCreateMemStorage(0);cvCvtColor(img,gray,CV_BGR2GRAY);cvSmooth(gray,gray,CV_GAUSSIAN,5,15);/smoothit,otherwisealotoffalsecirclesmaybedetectedCvSeq*circles=cvHoughCircles(gray,storage,CV_HOUGH_GRADIENT,2,gray-height/4,200,100);inti;for(i=0;itotal;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);cout圆心坐标x=cvRound(p0)endl圆心坐标y=cvRound(p1)endl;cout半径=cvRound(p2)endl;cout圆数量=totalendl;cvNamedWindow(circles,1);cvShowImage(circles,img);cvWaitKey(0);return0;运行效果截图:?Hough矩形提取:Code:#includecv.h#includehighgui.h#include#include#includeintthresh=50;IplImage*img=0;IplImage*img0=0;CvMemStorage*storage=0;CvPointpt4;constchar*wndname=SquareDetectionDemo;doubleangle(CvPoint*pt1,CvPoint*pt2,CvPoint*pt0)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)+1e-10);CvSeq*findSquares4(IplImage*img,CvMemStorage*storage)CvSeq*contours;inti,c,l,N=11;CvSizesz=cvSize(img-width&-2,img-height&-2);IplImage*timg=cvCloneImage(img);IplImage*gray=cvCreateImage(sz,8,1);IplImage*pyr=cvCreateImage(cvSize(sz.width/2,sz.height/2),8,3);IplImage*tgray;CvSeq*result;doubles,t;CvSeq*squares=cvCreateSeq(0,sizeof(CvSeq),sizeof(CvPoint),storage);cvSetImageROI(timg,cvRect(0,0,sz.width,sz.height);/down-scaleandupscaletheimagetofilteroutthenoisecvPyrDown(timg,pyr,7);cvPyrUp(pyr,timg,7);tgray=cvCreateImage(sz,8,1);/findsquaresineverycolorplaneoftheimagefor(c=0;c3;c+)cvSetImageCOI(timg,c+1);cvCopy(timg,tgray,0);for(l=0;ltotal=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=st?s:t;if(s0.3)for(i=0;ih_next;cvReleaseImage(&gray);cvReleaseImage(&pyr);cvReleaseImage(&tgray);cvReleaseImage(&timg);returnsquares;/thefunctiondrawsallthesquaresintheimagevoiddrawSquares(IplImage*img,CvSeq*squares)CvSeqReaderreader;IplImage*cpy=cvCloneImage(img);inti;cvStartReadSeq(squares,&reader,0);for(i=0;itotal;i+=4)CvPoint*rect=pt;intcount=4;memcpy(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);cvPolyLine(cpy,&rect,&count,1,1,CV_RGB(0,255,0),3,CV_AA,0);cvShowImage(wndname,cpy);cvReleaseImage(&cpy);voidon_trackbar(inta)if(img)drawSquares(img,findSquares4(img,storage);char*names=1.jpg,0;intmain(intargc,char*argv)inti,c;storage=cvCreateMemStorage(0);for(i=0;namesi!=0;i+)img0=cvLoadImage(namesi,1);if(!img0)printf(Couldntload%sn,namesi);continue;img=cvCloneImage(img0);cvNamedWindow(wndname,1);cvCreateTrackbar(cannythresh,wndname,&thresh,1000,on_trackbar);on_trackbar(0);c=cvWaitKey(0);cvReleaseImage(&img);cvReleaseImage(&img0);cvClearMemStorage(storage);if(c=27)break;cvDestroyWindow(wndname);return0;运行效果截图:?边缘直方图提取:Code:#includecv.h#includehighgui.h#include#include#definePI3.14intmain()IplImage*src=0;/sourceimagreIplImage*histimg=0;/histogramimageCvHistogram*hist=0;/definemulti_dementionhistogramIplImage*canny;CvMat*canny_m;IplImage*dx;/thesobelxdifferenceIplImage*dy;/thesobelydifferenceCvMat*gradient;/valueofgradientCvMat*gradient_dir;/directionofgradientCvMat*dx_m;/formattransformtomatrixCvMat*dy_m;CvMat*mask;CvSizesize;IplImage*gradient_im;inti,j;floattheta;inthdims=8;/划分HIST的个数,越高越精确floathranges_arr=-PI/2,PI/2;/直方图的上界和下界float*hranges=hranges_arr;floatmax_val;/intbin_w;src=cvLoadImage(E:Downloadtest.jpg,0);/forcetograyimageif(src=0)return-1;cvNamedWindow(Histogram,0);/cvNamedWindow(src,0);size=cvGetSize(src);canny=cvCreateImage(cvGetSize(src),8,1);/边缘图像dx=cvCreateImage(cvGetSize(src),32,1);/x方向上的差分/此处的数据类型为U不怕溢出吗?dy=cvCreateImage(cvGetSize(src),32,1);gradient_im=cvCreateImage(cvGetSize(src),32,1);/梯度图像canny_m=cvCreateMat(size.height,size.width,CV_32FC1);/边缘矩阵dx_m=cvCreateMat(size.height,size.width,CV_32FC1);dy_m=cvCreateMat(size.height,size.width,CV_32FC1);gradient=cvCreateMat(size.height,size.width,CV_32FC1);/梯度矩阵gradient_dir=cvCreateMat(size.height,size.width,CV_32FC1);/梯度方向矩阵mask=cvCreateMat(size.height,size.width,CV_32FC1);/掩码cvCanny(src,canny,60,180,3);/边缘检测cvConvert(canny,canny_m);/把图像转换为矩阵cvSobel(src,dx,1,0,3);/一阶X方向的图像差分:dxcvSobel(src,dy,0,1,3);/一阶Y方向的图像差分:dycvConvert(dx,dx_m);cvConvert(dy,dy_m);cvAdd(dx_m,dy_m,gradient);/valueofgradient/梯度不是等于根号下x的导数的平方加上y导数的平方吗?cvDiv(dx_m,dy_m,gradient_dir);/directionfor(i=0;isize.height;i+)for(j=0;jbins,hist-bins,max_val?255./max_val:0.,0);/缩放bin到区间0,255,比例系数cvZero(histimg);bin_w=histimg-width/16;/hdims:条的个数,则bin_w为条的宽度/画直方图for(i=0;ibins,i)*histimg-height/255);/返回单通道数组的指定元素,返回直方图第i条的大小,val为histimg中的i条的高度CvScalarcolor=CV_RGB(255,255,0);/(hsv2rgb(i*180.f/hdims);/直方图颜色cvRectangle(histimg,cvPoint(100+i*bin_w,histimg-height),cvPoint(100+(i+1)*bin_w,(int)(histimg-height-val),color,1,8,0);/画直方图画矩形,左下角,右上角坐标cvShowImage(src,src);cvShowImage(Histogram,histimg);cvWaitKey(0);cvDestroyWindow(src);cvDestroyWindow(Histogram);cvReleaseImage(&src);cvReleaseImage(&histimg);cvReleaseHist(&hist);return0;运行效果截图:?视频流中边缘检测:Code:#includehighgui.h#includecv.h#includestdio.h#includeintmain(intargc,char*argv)IplImage*laplace=0;IplImage*colorlaplace=0;IplImage*planes3=0,0,0;CvCapture*capture=0;/从摄像头读取/*if(argc=1|(argc=2&strlen(argv1)=1&isdigit(argv10)capture=cvCaptureFromCAM(argc=2?argv10-0:0);*/从文件中读取/*elseif(argc=2)*/capture=cvCaptureFromAVI(1.avi);if(!capture)fprintf(stderr,Couldnotinitializecapturing.n);return-1;cvNamedWindow(Laplacian,1);cvNamedWindow(video,1);/循环捕捉,直到用户按键跳出循环体for(;)IplImage*frame=0;/抓起一祯frame=cvQueryFrame(capture);if(!frame)break;if(!laplace)/创建图像for(inti=0;iwidth,frame-height),IPL_DEPTH_8U,1);laplace=cvCreateImage(cvSize(frame-width,frame-height),IPL_DEPTH_16S,1);colorlaplace=cvCreateImage(cvSize(frame-width,frame-height),IPL_DEPTH_8U,3);cvCvtPixToPlane(frame,planes0,planes1,planes2,0);for(inti=0;iorigin=frame-origin;/高斯滤波,平滑图像/cvSmooth(colorlaplace,colorlaplace,CV_GAUSSIAN,1,0,0);/形态学滤波,闭运算cvDilate(colorlaplace,colorlaplace,0,1);/膨胀cvErode(colorlaplace,colorlaplace,0,1);/腐蚀cvShowImage(video,frame);cvShowImage(Laplacian,colorlaplace);if(cvWaitKey(10)0)break;cvReleaseCapture(&capture);cvDestroyWindow(Laplacian);cvDestroyWindow(video);return0;运行效果截图:?纹理提取:Code:#include#include#includecv.h#includehighgui.hintmain(intargc,char*argv)inttmp8=0;intsum=0;intk=0;IplImage*img,*dst;img=cvLoadImage(E:Download2.jpg,0);CvScalars;cvNamedWindow(img,NULL);cvNamedWindow(dst,NULL);cvShowImage(img,img);uchar*data=(uchar*)img-imageData;intstep=img-widthStep;dst=cvCreateImage(cvSize(img-width,img-height),img-depth,1);dst-widthStep=img-widthStep;for(inti=1;iheight-1;i+)for(intj=1;jwidth-1;j+)if(data(i-1)*step+j-1datai*step+j)tmp0=1;elsetmp0=0;if(datai*step+(j-1)datai*step+j)tmp1=1;elsetmp1=0;if(data(i+1)*step+(j-1)datai*step+j)tmp2=1;elsetmp2=0;if(data(i+1)*step+jdatai*step+j)tmp3=1;elsetmp3=0;if(data(i+1)*step+(j+1)datai*step+j)tmp4=1;elsetmp4=0;if(datai*step+(j+1)datai*step+j)tmp5=1;elsetmp5=0;if(data(i-1)*step+(j+1)datai*step+j)tmp6=1;elsetmp6=0;if(data(i-1)*step+jdatai*step+j)tmp7=1;elsetmp7=0;for(k=0;k=7;k+)sum+=abs(tmpk-tmpk+1);sum=sum+abs(tmp7-tmp0);if(sum=2)s.val0=(tmp0*128+tmp1*64+tmp2*32+tmp3*16+tmp4*8+tmp5*4+tmp6*2+tmp7);elses.val0=59;cvSet2D(dst,i,j,s);cvShowImage(dst,dst);cvWaitKey(-1);return0;运行效果截图:分类:opencv学习
展开阅读全文
相关资源
相关搜索

当前位置:首页 > 管理文书 > 工作总结


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!