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

方法一:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#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;i<img0->height;i++)
		for(j=0;j<img0->width;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<channel;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("c:\\line110.jpg",0);//不可以再一次用IplImage定义,因为其已经是全局变量
	img1=cvLoadImage("c:\\line111.jpg",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;
}

结果 :

opencv 图像帧差法(图像相减) 代码_第1张图片
方法二:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#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<height;i++) for(j=0;j<width;j++)
	{
		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);

	img0=cvLoadImage("c:\\line110.jpg",0);//不可以再一次用IplImage定义,因为其已经是全局变量
	img1=cvLoadImage("c:\\line111.jpg",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;
}

结果:

opencv 图像帧差法(图像相减) 代码_第2张图片
方法三:

/*使用矩阵减法做图像帧差,其代码如下,和使用cv自带的函数差不多*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#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,0 CV_THRESH_BINARY);//阈值处理
	cvShowImage( "result", img3 );//显示处理后的图像图像
	
}
int main( int argc, char** argv )
{    int thresh = 80;
//载入图像
img0=cvLoadImage("c:\\line110.jpg",0);//不可以再一次用IplImage定义,因为其已经是全局变量
img1=cvLoadImage("c:\\line111.jpg",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;
}
结果: opencv 图像帧差法(图像相减) 代码_第3张图片

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