OPENCV特征提取代码总结_第1页
OPENCV特征提取代码总结_第2页
OPENCV特征提取代码总结_第3页
免费预览已结束,剩余20页可下载查看

下载本文档

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

文档简介

1、特征提取代码总结来自 特征提取代码总结颜色提取颜色直方图提取:code:#include <cv.h> #include <highgui.h> #include <iostream> using namespace std;int main( int argc, char* argv )iplimage * src= cvloadimage(“e:downloadtest1.jpg“,1);iplimage* hsv = cvcreateimage( cvgetsize(src), 8, 3 ); iplimage* h_plane = cvcreatei

2、mage( 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 个等级*/ int h_bins = 16, s_bins = 8;int hist_size = h_bins, s_bins;/* h 重量的变化范围*/float h_

3、ranges = 0, 180 ;/* s 重量的变化范围*/float s_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 );

4、/* 依据 h,s 两个平面数据统计直方图*/ cvcalchist( planes, hist, 0, 0 );/* 猎取直方图统计的最大值,用于动态显示直方图*/ float max_value;cvgetminmaxhistvalue( hist, 0, &max_value, 0, 0 );/* 设置直方图显示图像*/ int height = 240;int width = (h_bins*s_bins*6);iplimage* hist_img = cvcreateimage( cvsize(width,height), 8, 3 ); cvzero( hist_img )

5、;/* 用来进行 hsv 到 rgb 颜色转换的临时单位图像*/ iplimage * hsv_color = cvcreateimage(cvsize(1,1),8,3); iplimage * rgb_color = cvcreateimage(cvsize(1,1),8,3); int bin_w = width / (h_bins * s_bins);for(int h = 0; h < h_bins; h+)for(int s = 0; s < s_bins; s+)int i = h*s_bins + s;/* 获得直方图中的统计次数,计算显示在图像中的高度*/ flo

6、at bin_val = cvqueryhistvalue_2d( hist, h, s ); int intensity = 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);cvscalar color = cvget2d(rgb_color,0,0);cvrectangle( hist

7、_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-s histogram“, 1 ); cvshowimage( “h-s histogram“, hist_img );cvwaitkey(0);运行效果截图:外形提取candy 算子对边缘提取:code:#include “cv.h“ #include “cxcore

8、.h“ #include “highgui.h“int main( int argc, 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, 1

9、50, 3);/创建窗口cvnamedwindow(“src“, 1); cvnamedwindow(“canny“,1);/显示图像cvshowimage( “src“, pimg ); cvshowimage( “canny“, pcannyimg );/等待按键cvwaitkey(0);/销毁窗口cvdestroywindow( “src“ );cvdestroywindow( “canny“ );/释放图像cvreleaseimage( &pimg );cvreleaseimage( &pcannyimg ); return 0;运行效果截图:角点提取:code:#in

10、clude <stdio.h> #include “cv.h“ #include “highgui.h“ #define max_corners 100 int main(void)int cornerscount=max_corners;/得到的角点数目cvpoint2d32f cornersmax_corners;/输出角点集合iplimage *srcimage = 0,*grayimage = 0,*corners1 = 0,*corners2 = 0; int i;cvscalar color = cv_rgb(255,0,0); cvnamedwindow(“image

11、“,1);/load the image to be processedsrcimage = cvloadimage(“e:download1.jpg“,1);grayimage = cvcreateimage(cvgetsize(srcimage),ipl_depth_8u,1);/copy the source image to copy image after converting the format/复制并转为灰度图像cvcvtcolor(srcimage,grayimage,cv_bgr2gray);/create empty images os same size as the

12、copied images/两幅临时位浮点图像,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(“num corn

13、ers found: %dn“,cornerscount);/开头画出每个点if (cornerscount>0)for (i=0;i<cornerscount;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)

14、; cvreleaseimage(&corners1); cvreleaseimage(&corners2); cvwaitkey(0);return 0;运行效果截图:h ough 直线提取:code:#include <cv.h> #include <highgui.h> #include <math.h>int main(int argc, char* argv)iplimage* src = cvloadimage( “e:download2.jpg“ , 0 ); iplimage* dst;iplimage* color_dst;

15、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

16、, cv_hough_standard, 1, cv_pi/180, 100, 0, 0 );for( i = 0; i < min(lines->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;#elsept1.x = cvround(x0 + 1000*(-b

17、); 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 );lines = cvhoughlines2( dst, storage, cv_hough_probabilistic, 1, cv_pi/180, 50, 50, 10 ); for( i = 0; i < lines->total; i+ )#endifcvpoint*

18、line = (cvpoint*)cvgetseqelem(lines,i);cvline( color_dst, line0, line1, cv_rgb(255,0,0), 3, cv_aa, 0 );cvnamedwindow( “source“, 1 ); cvshowimage( “source“, src );cvnamedwindow( “hough“, 1 ); cvshowimage( “hough“, color_dst );cvwaitkey(0);return 0;运行效果截图:hough 圆提取:code:#include <cv.h> #include

19、<highgui.h> #include <math.h> #include <iostream> using namespace std;int main(int argc, 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_

20、bgr2gray );cvsmooth( gray, gray, cv_gaussian, 5, 15 );/ smooth it, otherwise a lot of false circles may be detectedcvseq* circles = cvhoughcircles( gray, storage, cv_hough_gradient, 2, gray->height/4, 200, 1 00 );int i;for( i = 0; i < circles->total; i+ )float* p = (float*)cvgetseqelem( cir

21、cles, 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<<“半径=“<

22、;<cvround(p2)<<endl;cout<<“圆数量=“<<circles->total<<endl; cvnamedwindow( “circles“, 1 ); cvshowimage( “circles“, img ); cvwaitkey(0);return 0;运行效果截图:hough 矩形提取:code:#include “cv.h“ #include “highgui.h“ #include <stdio.h> #include <math.h> #include <string.h

23、> int thresh = 50; iplimage* img = 0; iplimage* img0 = 0;cvmemstorage* storage = 0;cvpoint pt4;const char* wndname = “square detection demo“; double angle( cvpoint* pt1, cvpoint* pt2, cvpoint* pt0 )double dx1 = pt1->x - pt0->x; double dy1 = pt1->y - pt0->y; double dx2 = pt2->x - pt

24、0->x; double dy2 = 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;int i, c, l, n = 11;cvsize sz = cvsize( img->width & -2, img->height & -2 ); iplimage*

25、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; double s, t;cvseq* squares = cvcreateseq( 0, sizeof(cvseq), sizeof(cvpoint), storage ); cvsetimageroi( timg, cvrect( 0, 0, sz.w

26、idth, sz.height );/ down-scale and upscale the image to filter out the noise cvpyrdown( timg, pyr, 7 );cvpyrup( pyr, timg, 7 );tgray = cvcreateimage( sz, 8, 1 );/ find squares in every color plane of the image for( c = 0; c < 3; c+ )cvsetimagecoi( timg, c+1 ); cvcopy( timg, tgray, 0 ); for( l = 0

27、; l < n; l+ )if( l = 0 )cvcanny( tgray, gray, 0, thresh, 5 ); cvdilate( gray, gray, 0, 1 );elsecvthreshold( tgray, gray, (l+1)*255/n, 255, cv_thresh_binary );cvfindcontours( gray, storage, &contours, sizeof(cvcontour),cv_retr_list, cv_chain_approx_si mple, cvpoint(0,0) );while( contours )resu

28、lt = cvapproxpoly( contours, sizeof(cvcontour), storage,cv_poly_approx_dp, cvcontourper imeter(contours)*0.02, 0 );if( result->total = 4 && fabs(cvcontourarea(result,cv_whole_seq) > 1000 &&cvcheckcont ourconvexity(result) )s = 0;for( i = 0; i < 5; i+ )if( i >= 2 )t = fabs

29、(angle( (cvpoint*)cvgetseqelem( result, i ),(cvpoint*)cvgetseqelem( result, i-2 ),(c vpoint*)cvgetseqelem( result, i-1 );s = s > t ? s : t;if( s < 0.3 )for( i = 0; i < 4; i+ ) cvseqpush( squares,(cvpoint*)cvgetseqelem( result, i );contours = contours->h_next;cvreleaseimage( &gray );

30、cvreleaseimage( &pyr ); cvreleaseimage( &tgray ); cvreleaseimage( &timg ); return squares;/ the function draws all the squares in the image void drawsquares( iplimage* img, cvseq* squares )cvseqreader reader;iplimage* cpy = cvcloneimage( img ); int i;cvstartreadseq( squares, &reader,

31、 0 ); for( i = 0; i < squares->total; i += 4 )cvpoint* rect = pt; int count = 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

32、 + 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

33、); cvreleaseimage( &cpy );void on_trackbar( int a )if( img )drawsquares( img, findsquares4( img, storage ) );char* names = “1.jpg“, 0 ; int main(int argc, char* argv)int i, c;storage = cvcreatememstorage(0); for( i = 0; namesi != 0; i+ )img0 = cvloadimage( namesi, 1 ); if( !img0 )printf(“couldn”

34、t load %sn“, namesi ); continue;img = cvcloneimage( img0 ); cvnamedwindow( wndname, 1 );cvcreatetrackbar( “canny thresh“, wndname, &thresh, 1000, on_trackbar ); on_trackbar(0);c = cvwaitkey(0); cvreleaseimage( &img ); cvreleaseimage( &img0 ); cvclearmemstorage( storage ); if( c = 27 )bre

35、ak;cvdestroywindow( wndname ); return 0;运行效果截图:边缘直方图提取:code:#include “cv.h“ #include “highgui.h“ #include <stdio.h>#include <ctype.h> #define pi 3.14 int main()iplimage *src = 0; / source imagre iplimage *histimg = 0; / histogram imagecvhistogram *hist = 0; / define multi_demention histo

36、gram iplimage* canny;cvmat* canny_m;iplimage* dx; / the sobel x difference iplimage* dy; / the sobel y difference cvmat* gradient; / value of gradientcvmat* gradient_dir; / direction of gradient cvmat* dx_m; / format transform to matrix cvmat* dy_m;cvmat* mask; cvsizesize;iplimage* gradient_im; int

37、i,j;float theta;int hdims = 8;/ 划分 hist 的个数,越高越精确float hranges_arr = -pi/2,pi/2; / 直方图的上界和下界float* hranges = hranges_arr;float max_val; / int bin_w;src=cvloadimage(“e:downloadtest.jpg“, 0); / force to gray image if(src=0) return -1;cvnamedwindow( “histogram“, 0 );/cvnamedwindow( “src“, 0); size=cvge

38、tsize(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,s

39、ize.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,ca

40、nny_m);/把图像转换为矩阵cvsobel(src,dx,1,0,3);/ 一阶 x 方向的图像差分:dx cvsobel(src,dy,0,1,3);/ 一阶 y 方向的图像差分:dy cvconvert(dx,dx_m);cvconvert(dy,dy_m);cvadd(dx_m,dy_m,gradient); / value of gradient/梯度不是等于根号下x 的导数的平方加上y 导数的平方吗?cvdiv(dx_m,dy_m,gradient_dir); / direction for(i=0;i<size.height;i+) for(j=0;j<size.w

41、idth;j+)if(cvmget(canny_m,i,j)!=0 && cvmget(dx_m,i,j)!=0)/此行是什么意思?只看边缘上的方向?theta=cvmget(gradient_dir,i,j); theta=atan(theta);cvmset(gradient_dir,i,j,theta);elsecvmset(gradient_dir,i,j,0);hist = cvcreatehist( 1, &hdims, cv_hist_array, &hranges, 1 );/ 创建一个指定尺寸的直方图,并返回创建的直方图指针histimg =

42、cvcreateimage( cvsize(320,200), 8, 3 ); / 创建一个图像,通道cvzero( histimg ); / 清; cvconvert(gradient_dir,gradient_im);/把梯度方向矩阵转化为图像cvcalchist( &gradient_im, hist, 0, canny ); / 计算直方图cvgetminmaxhistvalue( hist, 0, &max_val, 0, 0 ); / 只找最大值cvconvertscale( hist->bins, hist->bins, max_val ? 255.

43、/ max_val : 0., 0 );/ 缩放 bin 到区间0,255 ,比例系数cvzero( histimg );bin_w = histimg->width /16; / hdims: 条的个数,则 bin_w 为条的宽度/ 画直方图for( i = 0; i < hdims; i+ )double val = ( cvgetreal1d(hist->bins,i)*histimg->height/255 );/ 返回单通道数组的指定元素, 返回直方图第 i 条的大小,val 为 histimg 中的 i 条的高度cvscalar color = cv_rgb

44、(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“);

45、 cvdestroywindow(“histogram“); cvreleaseimage( &src ); cvreleaseimage( &histimg );cvreleasehist ( &hist );return 0;运行效果截图:视频流中边缘检测:code:#include “highgui.h“ #include “cv.h“ #include “stdio.h“ #include <ctype.h>int main(int argc,char * argv)iplimage * laplace = 0; iplimage * colorla

46、place = 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);*/从文件中读取/* else if(argc = 2)*/capture = cvcapturefromavi(“1.avi“); if(!capture)fprintf(stderr,“cou

47、ld not initialize capturing.n“); return -1;cvnamedwindow(“laplacian“,1); cvnamedwindow(“video“,1);/循环捕获,直到用户按键跳出循环体for(;)iplimage * frame =0;/抓起一祯frame = cvqueryframe(capture); if(!frame)break; if(!laplace)/创建图像for(int i=0;i<3;i+)planesi = cvcreateimage(cvsize(frame->width,frame->height),ip

48、l_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(int i=0;i<3;i+)/交换,如通道变换cvlaplace(planesi,laplace,3);/使用线性变换转换输入函数元素成为无符号整形cvconvertscaleabs(laplace,planesi,1,0);cvcvtplanetopix(planes0,planes1,planes2,0,colorlaplace);/结构相同(- 顶左结构,1 - 底左结构)colorlaplace->origin = frame->origin;/高斯滤波,平滑图像/ cvsmooth(colo

温馨提示

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

最新文档

评论

0/150

提交评论