前言
这篇文章主要讲述opencv图像处理中的背景差分。
一、背景差分
//第一题 背景差分
#include <opencv2/opencv.hpp>
#include <iostream>
#include<cmath>
using namespace cv;
using namespace std;
int main()
{
//计数器
int cnt = 0;
Mat frame;
Mat background;
Mat subpic;
Mat bin_subpic;
VideoCapture cap;
cap.open("C://Users//john//Desktop//VID.mp4");
while (1)
{
cap.read(frame);
cvtColor(frame, frame, COLOR_BGR2GRAY);
if (cnt == 0) {
frame.copyTo(background);
}
else {
//第二帧开始背景差分
//背景图像和当前图像相减
absdiff(frame, background, subpic);
//差分结果二值化
threshold(subpic, bin_subpic, 50, 144, CV_THRESH_BINARY);
imshow("subpic", subpic);
imshow("bin_subpic", bin_subpic);
imshow("frame", frame);
waitKey(30);
}
cnt++;
}
return 0;
}
总结
1.代码可以直接运行,如果有不懂的可以留言哦。
2.缺少素材图片,后续补上,谢谢。