图像特征提取的总结(续)

来源:互联网 发布:有线网络连接不上 编辑:程序博客网 时间:2024/05/04 07:25

续上一章

6,hough矩形提取

#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <math.h>
#include <string.h>
int thresh = 50;
IplImage* img = 0;
IplImage* img0 = 0;
CvMemStorage* storage = 0;
CvPoint pt[4];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 - pt0->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* 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.width, 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; l < N; l++ )     
{          
if( l == 0 )     
{               
cvCanny( tgray, gray, 0, thresh, 5 );        
cvDilate( gray, gray, 0, 1 );      
}           
else       
{             
cvThreshold( tgray, gray, (l+1)*255/N, 255, CV_THRESH_BINARY );
}                       
cvFindContours( gray, storage, &contours, sizeof(CvContour),CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );          
while( contours )    
{              
  result = cvApproxPoly( contours, sizeof(CvContour), storage,CV_POLY_APPROX_DP, cvContourPerimeter(contours)*0.02, 0 ); 
if( result->total == 4 && fabs(cvContourArea(result,CV_WHOLE_SEQ)) > 1000 &&  cvCheckContourConvexity(result) )  
{               
s = 0;      
for( i = 0; i < 5; i++ )  
{                   
if( 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( s < 0.3 )                      
for( i = 0; i < 4; i++ )              
cvSeqPush( squares,                    
(CvPoint*)cvGetSeqElem( result, i ));     
}                                      
contours = contours->h_next;      
}   

}
cvReleaseImage( &gray );   
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, 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 + 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 );
}
void on_trackbar( int a )
{   
if( img )    
drawSquares( img, findSquares4( img, storage ) );
}
char* names[] = { "a5.png", 0 };
int main(int argc, char** argv)
{    
int i, c; 
storage = cvCreateMemStorage(0);    
for( i = 0; names[i] != 0; i++ )   
{     
img0 = cvLoadImage( names[i], 1 );   
if( !img0 )    
{        
printf("Couldn't load %s\n", names[i] );    
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 )       
break;   
}       
cvDestroyWindow( wndname );   
return 0;




7,边缘直方图提取

#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 image 
    CvHistogram *hist = 0; // define multi_demention histogram
    IplImage* canny;
    CvMat* canny_m;
    IplImage* dx; // the sobel x difference 
    IplImage* dy; // the sobel y difference 
    CvMat* gradient; // value of gradient
    CvMat* gradient_dir; // direction of gradient
    CvMat* dx_m; // format transform to matrix
    CvMat* dy_m;
    CvMat* mask;
    CvSize  size;
    IplImage* gradient_im;
    int 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("h3.png", 0); // force to gray image
       if(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方向的图像差分: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.width;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);  
      }
      else
      {
         cvmSet(gradient_dir,i,j,0);
      }
         
    }
   hist = cvCreateHist( 1, &hdims, CV_HIST_ARRAY, &hranges, 1 );  
// 创建一个指定尺寸的直方图,并返回创建的直方图指针
   histimg = 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. / 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(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 );
    
    return 0;
}




8,视频流中边缘检测

#include "highgui.h"
#include "cv.h"
#include "stdio.h"
#include <ctype.h> 
int main(int argc,char ** argv)
{
       IplImage * laplace = 0;
       IplImage * colorlaplace = 0;
       IplImage * planes[3] = {0,0,0};
       CvCapture *capture = 0;
      //从摄像头读取
       /*if(argc == 1 ||( argc==2 && strlen(argv[1])==1 && isdigit(argv[1][0]) ))
           capture = cvCaptureFromCAM(argc == 2 ? argv[1][0] -'0':0);*/
       //从文件中读取
      /* else if(argc == 2)*/
           capture = cvCaptureFromAVI("vedio1.avi");
       if(!capture)
       {
           fprintf(stderr,"Could 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++)
               planes[i] = cvCreateImage(cvSize(frame->width,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,planes[0],planes[1],planes[2],0);
             for(int i=0;i<3;i++)
             {
                //交换,如通道变换
                cvLaplace(planes[i],laplace,3);
                //使用线性变换转换输入函数元素成为无符号整形
                cvConvertScaleAbs(laplace,planes[i],1,0);
              }
            cvCvtPlaneToPix(planes[0],planes[1],planes[2],0,colorlaplace);
            //结构相同(- 顶—左结构,1 - 底—左结构)
            colorlaplace->origin = 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");
       return 0;
}


9,纹理提取

#include <iostream>
#include <math.h>
#include "cv.h"
#include "highgui.h"
int main(int argc, char* argv[])
{
int tmp[8]={0};
int sum=0;int k=0;
IplImage* img,*dst;
img=cvLoadImage("h3.png",0);
CvScalar s;
cvNamedWindow("img",NULL);
cvNamedWindow("dst",NULL);
cvShowImage("img",img);
uchar* data=(uchar*)img->imageData;
int step=img->widthStep;
dst=cvCreateImage(cvSize(img->width,img->height),img->depth,1);
dst->widthStep=img->widthStep;
for(int i=1;i<img->height-1;i++)
for(int j=1;j<img->width-1;j++)
{
if(data[(i-1)*step+j-1]>data[i*step+j]) tmp[0]=1;
else tmp[0]=0;
if(data[i*step+(j-1)]>data[i*step+j]) tmp[1]=1;
else tmp[1]=0;
if(data[(i+1)*step+(j-1)]>data[i*step+j]) tmp[2]=1;
else tmp[2]=0;
if (data[(i+1)*step+j]>data[i*step+j]) tmp[3]=1;
else tmp[3]=0;
if (data[(i+1)*step+(j+1)]>data[i*step+j]) tmp[4]=1;
else tmp[4]=0;
if(data[i*step+(j+1)]>data[i*step+j]) tmp[5]=1;
else tmp[5]=0;
if(data[(i-1)*step+(j+1)]>data[i*step+j]) tmp[6]=1;
else tmp[6]=0;
if(data[(i-1)*step+j]>data[i*step+j]) tmp[7]=1;
else tmp[7]=0;
for(k=0;k<=7;k++)
sum+=abs(tmp[k]-tmp[k+1]);
sum=sum+abs(tmp[7]-tmp[0]);
if (sum<=2)
s.val[0]=(tmp[0]*128+tmp[1]*64+tmp[2]*32+tmp[3]*16+tmp[4]*8+tmp[5]*4+tmp[6]*2+tmp[7]);
else s.val[0]=59; 
cvSet2D(dst,i,j,s);
}
cvShowImage("dst",dst);
cvWaitKey(-1);
return 0;
}



(完)


0 0
原创粉丝点击