我在从视频帧中提取前景时遇到问题。它也提取了一些背景对象。我从示例视频中拍摄了一张快照以用作背景图像。建议我应该做什么。是否为背景图像拍摄单独的图像而不是从视频中拍摄快照(假设,快照比图像分辨率低)或其他更好的代码。用于提取的代码是
int _tmain(int argc, _TCHAR* argv[])
{
IplImage *frame = NULL;
IplImage *img1 = NULL;
IplImage *grey = NULL;
IplImage *edges = NULL;
int delay = 0, key=0, i=0;
CvCapture *video = NULL;
CvCapture *video1 = NULL;
cvNamedWindow("window_name");
video = cvCreateFileCapture("sample.avi");
video1 = cvCreateFileCapture("sample.avi");
frame = cvQueryFrame(video);
img1 = cvQueryFrame(video1);
grey = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);
edges = cvCreateImage(cvGetSize(img1), IPL_DEPTH_8U, 1);
cvShowImage("backgrnd", img1);
//get height and width using OpenCV functions
const int &rows = img1->width;
const int &cols =frame->height;
BYTE *Pixel2=0;
cvGetRawData(img1,&Pixel2,0,0);
while (frame) {
BYTE *Pixel1=0;
//extract pixels using the OpenCV function cvGetRawData
cvGetRawData(frame,&Pixel1,0,0);
//register int to increase the speed
register int r,ri,c;
//to find diffrence of 2 images by pixel to pixel comparision
for(r = 0, ri = 0; r < rows*3; r++, ri += cols)
{
for(c = 0; c < cols; c++)
{
//get the difference in pixels
Pixel1[ri + c] = Pixel1[ri + c] - Pixel2[ri + c];
//set threshold value as 100 for comparision, it can be changed to values between 50 and 200, for getting binary image
if(Pixel1[ri + c] < 150)
{
Pixel1[ri + c]=0;
}
else
Pixel1[ri + c]=255;
}//for c
}//for r, ri
Return 0;
}