opencv 图像帧差法(图像相减) 代码

来源:http://hi.baidu.com/ren_jian_ke/item/150f17743bc8d5316f29f6dc

opencv 图像帧差法(图像相减) 代码

/* 说明这种方法经过调试在vc下是可以的,但在codeblocks下不可以,问题出在height变量上,如果height的数值改的小些
则可以,但有部分图像未得到处理,而且使用cvSet()和cvGet()函数处理速度慢*/

#include
#include
#include
#include "cv.h"
#include "highgui.h"
IplImage * img0=NULL;
IplImage * img1=NULL;
IplImage * img2=NULL;
void onTrackerSlid(int pos)
{  
int i,j;

   CvScalar s0,s1,s2;
   for(i=0;iheight;i++)
        for(j=0;jwidth;j++)

   {
            s0=cvGet2D(img0,i,j); // get the (i,j) pixel value
      s1=cvGet2D(img1,i,j);
      s2=cvGet2D(img2,i,j);
       // for(k=0;k    if(fabs(s1.val[0]-s0.val[0])>pos)
   {
           s2.val[0]=0;
           cvSet2D(img2,i,j,s2); // set the (i,j) pixel value
   }
   else
   {
     s2.val[0]=255;
            cvSet2D(img2,i,j,s2);
   }
   }
cvShowImage( "result", img2 );
}
int main( int argc, char** argv )

{    int thresh = 80;
    //载入图像
    img0=cvLoadImage(argv[1],0);//不可以再一次用IplImage定义,因为其已经是全局变量
img1=cvLoadImage(argv[2],0);
img2=cvCreateImage(cvGetSize(img0),8,1);
// int channel = img->nChannels;
   // printf("the image is %d X %d wiht %d channels",height,width,channel);
   

   
     cvNamedWindow( "imge0", CV_WINDOW_AUTOSIZE);//创建窗口
   cvNamedWindow( "imge1", CV_WINDOW_AUTOSIZE);
     cvNamedWindow( "result", CV_WINDOW_AUTOSIZE);
    cvShowImage( "imge0", img0 );//显示图像
     cvShowImage( "imge1", img1 );
   

    cvCreateTrackbar("threshold","result",&thresh,255,onTrackerSlid);
        onTrackerSlid(thresh);

         cvWaitKey(0); //等待按键
         cvDestroyWindow( "Imge0" );//销毁窗口
   cvDestroyWindow( "Imge1" );
    cvDestroyWindow( "result" );
        cvReleaseImage( &img0); //释放图像
   cvReleaseImage( &img1);
   cvReleaseImage( &img2);
    return -1;
}

方法二:

/*#include
#include
#include
#include "cv.h"
#include "highgui.h"
IplImage * img0=NULL;
IplImage * img1=NULL;
IplImage * img2=NULL;
uchar *data0=NULL;
uchar *data1=NULL;
uchar *data2=NULL;
void onTrackerSlid(int pos)
{
     int i,j;
        int     height = img0->height;
     int width   = img0->width;
     int step    = img0->widthStep/sizeof(uchar);
        data0   = (uchar*)img0->imageData;
        data1   = (uchar*)img1->imageData;
        data2   = (uchar*)img2->imageData;
        for(i=0;i         {
            if(abs( data1[i*step+j+0]-data0[i*step+j+0])>pos)
            data2[i*step+j+0] =255;
            else
            data2[i*step+j+0] = 0;
        }
        cvShowImage( "result", img2 );
}
说明:对于这一部分可以利用cv自带的函数实现图像相减和阈值处理,方法是:
增加一个IplImage 对象img3,并使用cvCreateImage将其初始化,将此部分换成如下代码:
        cvAbsDiff(img1, img0, img2);//图像相减功能和使用 if(( data1[i*step+j+0]-data0[i*step+j+0])>pos)是一样的,没取绝对值
        cvThreshold(img2, img3, pos, 255, CV_THRESH_BINARY);//阈值处理
        cvShowImage( "result", img3 );//显示处理后的图像图像

int main( int argc, char** argv )

{    int thresh = 80;
    //载入图像
    img0=cvLoadImage(argv[1],0);
img1=cvLoadImage(argv[2],0);
img2=cvCreateImage(cvGetSize(img0),8,1);
    cvSmooth(img0, img0, CV_GAUSSIAN, 3, 0, 0,0);//高斯滤波平滑图像
// int channel = img->nChannels;
   // printf("the image is %d X %d wiht %d channels",height,width,channel);

     cvNamedWindow( "imge0", CV_WINDOW_AUTOSIZE);//创建窗口
   cvNamedWindow( "imge1", CV_WINDOW_AUTOSIZE);
     cvNamedWindow( "result", CV_WINDOW_AUTOSIZE);
    cvShowImage( "imge0", img0 );//显示图像
     cvShowImage( "imge1", img1 );


    cvCreateTrackbar("threshold","result",&thresh,255,onTrackerSlid);
        onTrackerSlid(thresh);

         cvWaitKey(0); //等待按键
         cvDestroyWindow( "Imge0" );//销毁窗口
   cvDestroyWindow( "Imge1" );
    cvDestroyWindow( "result" );
        cvReleaseImage( &img0); //释放图像
   cvReleaseImage( &img1);
   cvReleaseImage( &img2);
    return -1;
}*/

方法三:

/*使用矩阵减法做图像帧差,其代码如下,和使用cv自带的函数差不多*/
#include
#include
#include
#include "cv.h"
#include "highgui.h"
IplImage * img0=NULL;
IplImage * img1=NULL;
CvMat * img2=NULL;
CvMat *img3=NULL;
void onTrackerSlid(int pos)
{
        cvSub(img0, img1, img2,0);//图像相减
        cvThreshold(img2, img3, pos, 255, CV_THRESH_BINARY);//阈值处理
        cvShowImage( "result", img3 );//显示处理后的图像图像

}
int main( int argc, char** argv )

{    int thresh = 80;
    //载入图像
    img0=cvLoadImage(argv[1],0);
img1=cvLoadImage(argv[2],0);
img2=cvCreateMat(img0->height,img0->width,CV_8UC1);//矩阵指针的初始化,与cvCreareImage类似
img3=cvCreateMat(img0->height,img0->width,CV_8UC1);
    cvSmooth(img0, img0, CV_GAUSSIAN, 3, 0, 0,0);//高斯滤波平滑图像

     cvNamedWindow( "imge0", CV_WINDOW_AUTOSIZE);//创建窗口
   cvNamedWindow( "imge1", CV_WINDOW_AUTOSIZE);
     cvNamedWindow( "result", CV_WINDOW_AUTOSIZE);
    cvShowImage( "imge0", img0 );//显示图像
     cvShowImage( "imge1", img1 );
    cvCreateTrackbar("threshold","result",&thresh,255,onTrackerSlid);
        onTrackerSlid(thresh);
         cvWaitKey(0); //等待按键
         cvDestroyWindow( "Imge0" );//销毁窗口
   cvDestroyWindow( "Imge1" );
    cvDestroyWindow( "result" );
        cvReleaseImage( &img0); //释放图像
   cvReleaseImage( &img1);
   cvReleaseMat( &img2);
   cvReleaseMat( &img3);
    return -1;
}

//其效果是不一样的,原因是cvSub只是将像素相减,并未对差值取绝对值*/

#linux-c/c++

你可能感兴趣的:(opencv 图像帧差法(图像相减) 代码)