对于模特走秀或者电影中的视频,很多时候会切换镜头,怎么来判断镜头的切换,可以采用帧差法,通过帧与帧之间的差值来判断是否切换镜头
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
VideoCapture capture("D:/jefen-sound-ok-cut.mp4");
if (!capture.isOpened())
{
cout << "No camera or video input!\n" << endl;
return -1;
}
Mat frame;
Mat gray; //当前帧灰度图
Mat differ,differ1;
int numFrames = capture.get(CV_CAP_PROP_FRAME_COUNT); //获取整个帧数
int num = numFrames;
Mat frame_0, frame_1;//Mat m(3, 5, CV_32FC1, 1);
int i = 0;
while (1)
{
capture >> frame;
imshow("frame_resize", frame);
cvtColor(frame, gray, CV_RGB2GRAY); //转化为灰度图
frame_1 = gray.clone();
if (i > 0)
{
absdiff(frame_1, frame_0, differ);//帧差法
imshow("frame_0", frame_0);
imshow("frame_1", frame_1);
imshow("differ", differ);
}
//cout << differ << endl;
threshold(frame_1, differ1, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU); //此处使用大津法 自适应取阈值
imshow("differ1", differ1);
//threshold(frame_1, differ, 30, 255, 0);
//imshow("differ2", differ);
//threshold(frame_1, differ, 0, 255, 0);//二值化通常设置为50 255
//imshow("differ3", differ);
frame_0 = gray.clone();
num--;
char c = waitKey(33);
if (c == 27) break;
if (num < 1)
return -1;
i++;
}
}