当前位置:网站首页>Opencv learning log 31 -- background difference

Opencv learning log 31 -- background difference

2022-07-06 15:43:00 @Spring sauce

Preface

This article is mainly about opencv Background difference in image processing .

One 、 Background subtraction

// The first question is   Background subtraction 
#include <opencv2/opencv.hpp>
#include <iostream>
#include<cmath>
using namespace cv;
using namespace std;

int main()
{
    

	// Counter 
	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 {
    
		    // The second frame starts with background subtraction 
            // The background image is subtracted from the current image 
			absdiff(frame, background, subpic);
			// Binarization of difference results 
			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;

}

summary

1. The code can run directly , If you don't understand, you can leave a message .
2. Missing material pictures , Subsequent patch , thank you .

原网站

版权声明
本文为[@Spring sauce]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131317244643.html