OpenCV之图像处理中的背景建模(五)

1,直接的背景减除,这种方法的主要要求之一是对象的颜色和亮度要与背景的颜色和亮度差异足够多,影响此类算法的因素是图像噪声、光照条件和相机中的自动对焦。
2,帧差分,在实时视频流中取得连续帧之间的差异,并显示这些差异。

Mat frameDiff(Mat prevFrame, Mat curFrame, Mat nextFrame)
{
	Mat diffFrames1, diffFrames2, output;
	//Computer absolute difference between current frame and the next absdiff(nextFrame, curFrame, diffFrames1);
	absdiff(nextFrame, curFrame, diffFrames1);
	absdiff(curFrame, prevFrame, diffFrames2);
	//Bitwise "AND" operation between the previous two diff images
	bitwise_and(diffFrames1, diffFrames2, output);
	return output;
}

计算当前帧与前一帧,当前帧与下一帧之间的绝对差值,帧差异应用按位AND运算符。突出显示图像中移动部分。
调整帧大小转换为灰度

Mat getFrame(VideoCapture cap, float scalingFactor)
{
	Mat frame, output;
	//Capture the current frame
	cap >> frame;
	//Resize the frame
	resize(frame, frame, Size(), scalingFactor, scalingFactor, INTER_AREA);
	//Convert to grayscale
	cvtColor(frame, output, COLOR_BGR2GRAY);
	return output;
}

int main(int argc, char* argv[])
{
	Mat frame, prevFrame, curFrame, nextFrame;
	char ch;

	//Create the capture object
	//0->input arg that specifies it should take the input from the webcam
	VideoCapture cap(0);
	//If you cannot open the webcam, stop the execution!
	if (!cap.isOpened())
		return -1;

	namedWindow("Frame");
	//Scaling factor to resize the input frames from the webcam
	float scalingFactor = 0.75;

	prevFrame = getFrame(cap, scalingFactor);
	curFrame = getFrame(cap, scalingFactor);
	nextFrame = getFrame(cap, scalingFactor);

	//Iterate until the user presses the Esc key
	while (true)
	{
		//Show the object movement
		imshow("Object Movement", frameDiff(prevFrame, curFrame, nextFrame));
		//Update the variables and grab the next frame
		prevFrame = curFrame;
		curFrame = nextFrame;
		nextFrame = getFrame(cap, scalingFactor);

		ch = waitKey(30);
		if (ch == 27)
			break;
	}
	//Release the video capture object
	cap.release();

	destroyAllWindows();
	return 0;
}

帧差分可以快速适应照明变化或相机移动,如果一个对象进入帧并停留在那里,在未来的帧中将不会检测它。缺点是在检测均匀着色的对象时,它只能检测均匀着色对象的边缘,如果对象的很大一部分有着非常低的像素差异,对象略移动,在该对象上标记的像素非常少,且难以检测对象是否朝向相机或远离相机移动。
3,高斯混合方法,OpenCV提供了高斯混合方法多种算法实现,其中一个称为MOG,另一个称为MOG2。

int main(int argc, char* argv[])
{
	//Current frame;
	Mat frame;
	//Foreground mask generated by MOG2 method
	Mat fgMaskMOG2;
	//MOG2 Background subtractor
	Ptr<BackgroundSubtractor> pMOG2;
	char ch;

	//Create the capture object
	//0->input arg that specifies it should take the input from the webcam
	VideoCapture cap(0);
	//If you cannot open the webcam, stop the execution!
	if (!cap.isOpened())
		return -1;

	namedWindow("Frame");
	namedWindow("FG Mask MOG 2"); 

	//Create MOG2 Background Subtractor object
	pMOG2 = createBackgroundSubtractorMOG2();
	//Scaling factor to resize the input frames from the webcam
	float scalingFactor = 0.75;
	//Iterate until the user presses the Esc key
	while (true)
	{
		//Capture the current frame
		cap >> frame;
		//Resize the frame
		resize(frame, frame, Size(), scalingFactor, scalingFactor, INTER_AREA);
		//Update the MOG2 background model based on the current frame
		pMOG2->apply(frame, fgMaskMOG2);
		//Show the current frame
		imshow("Frame", frame);
		//Show the MOG2 foreground mask
		imshow("FG Mask MOG 2", fgMaskMOG2);

		ch = waitKey(30);
		if (ch == 27)
			break;
	}
	//Release the video capture object
	cap.release();
	destroyAllWindows();
	return 0;
}

高斯混合模型创建背景减法器对象,该对象为当从网络摄像头遇新帧时更新的模型。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值