您好,欢迎访问三七文档
特征提取代码总结颜色提取颜色直方图提取:Code:#includecv.h#includehighgui.h#includeiostreamusingnamespacestd;intmain(intargc,char**argv){IplImage*src=cvLoadImage(E:\\Download\\test1.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:\\Download\\test.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:#includestdio.h#includecv.h#includehighgui.h#defineMAX_CORNERS100intmain(void){intcornersCount=MAX_CORNERS;//得到的角点数目CvPoint2D32fcorners[MAX_CORNERS];//输出角点集合IplImage*srcImage=0,*grayImage=0,*corners1=0,*corners2=0;inti;CvScalarcolor=CV_RGB(255,0,0);cvNamedWindow(image,1);//LoadtheimagetobeprocessedsrcImage=cvLoadImage(E:\\Download\\1.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:%d\n,cornersCount);//开始画出每个点if(cornersCount0){for(i=0;icornersCount;i++){cvCircle(srcImage,cvPoint((int)(corners[i].x),(int)(corners[i].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:#includecv.h#includehighgui.h#includemath.hintmain(intargc,char**argv){IplImage*src=cvLoadImage(E:\\Download\\2.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;iMIN(lines-total,100);i++){float*line=(float*)cvGetSeqElem(lines,i);floatrho=line[0];floattheta=line[1];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;ilines-total;i++){CvPoint*line=(CvPoint*)cvGetSeqElem(lines,i);cvLine(color_dst,line[0],line[1],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:#includecv.h#includehighgui.h#includemath.h#includeiostreamusingnamespacestd;intmain(intargc,char**argv){IplImage*img;img=cvLoadImage(E:\\Download\\3.jpg,1);IplImage*gray=cvCreateImage(cvGetSize(img),8,1);CvMemSto
本文标题:特征提取代码精选
链接地址:https://www.777doc.com/doc-3846022 .html