24,854
社区成员
发帖
与我相关
我的任务
分享
#include <cv.h>
#include <highgui.h>
#include <cxcore.h>
#include <math.h>
#include <iostream>
using namespace std;
int char2int(char x);
int main()
{
int m=0;
int i,j;
CvCapture* capture;
capture = cvCaptureFromFile("F:/Clip2.avi"); //Laboratory_raw campus highwayI_raw campus
int Image_width=(int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH ); //读取视频的宽
int Image_height=(int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT ); //读取视频的高
CvSize imgSize; //定义图像大小
imgSize.width=Image_width;
imgSize.height=Image_height;
if(!capture)
{
cout<<"error"<<endl;
return -1;
}
IplImage* image= cvCreateImage(imgSize, IPL_DEPTH_8U,3);
IplImage* image1= cvCreateImage(imgSize, IPL_DEPTH_8U,3);
IplImage* image2= cvCreateImage(imgSize, IPL_DEPTH_8U,3);
IplImage* image3= cvCreateImage(imgSize, IPL_DEPTH_8U,3);
IplImage* fr1= cvCreateImage(imgSize, IPL_DEPTH_8U,1);
IplImage* fr2= cvCreateImage(imgSize, IPL_DEPTH_8U,1);
IplImage* fr3 = cvCreateImage(imgSize, IPL_DEPTH_8U,1);
IplImage* res1 = cvCreateImage(imgSize, IPL_DEPTH_8U,1);
IplImage* res2 = cvCreateImage(imgSize, IPL_DEPTH_8U,1);
IplImage* res = cvCreateImage(imgSize, IPL_DEPTH_8U,1);
image->origin=1;
image2->origin=1;
image3->origin=1;
res->origin=1;
cvNamedWindow("res",1);
cvNamedWindow("image",3);
cvNamedWindow("image2",3);
cvNamedWindow("image3",3);
for(;;)
{ cout<<m<<endl;
//新的读进来
cvZero(res1); //清除上一次结果
cvZero(res2);
cvZero(res);
cvCopy(fr2,fr3,NULL); //传递图像
cvCopy(fr1,fr2,NULL);
cvCopy(image2,image3,NULL);
cvCopy(image,image2,NULL);
image = cvQueryFrame( capture );
cvCvtColor(image,fr1, CV_BGR2GRAY); //彩色变灰度
cvAbsDiff(fr1,fr2,res1); //差分
cvThreshold(res1, res1, 10, 255, CV_THRESH_BINARY); //阈值划分
cvAbsDiff(fr2,fr3,res2);
cvThreshold(res2, res2, 10, 255, CV_THRESH_BINARY);
for(j=0;j<Image_height;j++)
{
for(i=0;i<Image_width;i++)
{
if(char2int(*(res1->imageData+j*Image_width+i))==255&&
char2int(*(res2->imageData+j*Image_width+i))==255)
{
*(res->imageData+j*Image_width+i)=255;//这是利用三幅图的帧差法,这句话的意思是如果res1和res2都是 白色的情况下,res才是白色。为了更加准确的得到前景区域。
}
}
}
cvShowImage("res",res);
cvShowImage("image",image);
cvShowImage("image2",image2);
cvShowImage("image3",image3);//显示
cvWaitKey(1); //
m++;//每行起始的帧编号
}
return 0;
}
int char2int(char x)//通过逻辑运算将字符型数据转换为整型
{
int t=255;
t=t&x;
return(t);
}