C++ Video Frame Acquisition and Background Subtraction
This function captures video frames from a specified URL, converts them to grayscale, and performs background subtraction using the absolute difference method. It also applies a threshold to create a binary image, and passes it on to another function named 'background_separation'. Finally, it releases the capture object and returns a boolean value indicating success or failure.
bool video_frame_acquisition(const string& url, Mat& currentframe, Mat& diffframe) {
VideoCapture capture;
bool result = capture.open(url);
//检查是否成功打开
if (!capture.isOpened())
{
cout << "打开文摄像头失败,url:" << url << endl;
return 0;
}
Mat frame, tempframe, previousframe;
int framenum = 0;
Mat img1, img2;
while (true) {
tempframe = capture.read(frame);
capture >> frame;
tempframe = frame;
framenum += 1;
cvtColor(tempframe, currentframe, COLOR_BGR2GRAY);
//capture.set(CAP_PROP_POS_FRAMES, framenum);
if (framenum == 1) {
cvtColor(tempframe, previousframe, COLOR_BGR2GRAY);
}
else {
absdiff(currentframe, previousframe, diffframe);
double minVal, maxVal;
minMaxLoc(diffframe, &minVal, &maxVal);
//cout << maxVal << endl;
threshold(diffframe, img1, 45, 255.0, THRESH_BINARY);
GasDetect::background_separation(img1, previousframe);
//threshold(currentframe, img2, 150, 255.0, THRESH_BINARY_INV);
//bitwise_and(img1, img2, currentframe);
}
cvtColor(tempframe, previousframe, COLOR_BGR2GRAY);
waitKey(10);
}
capture.release();
return 0;
}
原文地址: https://www.cveoy.top/t/topic/odui 著作权归作者所有。请勿转载和采集!