我有一个加载到视频文件中的程序,让用户决定阈值,然后写出视频"阈值表示"一帧一帧。当我构建并运行它时,它可以工作,但不会在内存位置抛出"异常之前。" 问题是什么? 谢谢!
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include <iostream>
using namespace cv;
using namespace std;
Mat frame3, frame4;
int tValue = 100;
//the function that is called when the tracker is moved
void callFunc(int, void*) {
threshold(frame3, frame4, tValue, 255, THRESH_BINARY);
imshow("Threshold Adjustment", frame4);
}
int main(int argc, const char** argv)
{
VideoCapture cap("C:/video/park.avi");
if (!cap.isOpened()) {
cout << "Cannot open the video file" << endl;
return -1;
}
int frm_count = cap.get(CAP_PROP_FRAME_COUNT);
Mat frame1;
cap.set(CAP_PROP_POS_FRAMES, 0);
cap >> frame1;
Mat frame2;
cap.set(CAP_PROP_POS_FRAMES, 1);
cap >> frame2;
//calculates the absolute difference between two frames
absdiff(frame1, frame2, frame3);
namedWindow("Threshold Adjustment", CV_WINDOW_AUTOSIZE);
createTrackbar("Value", "Threshold Adjustment", &tValue, 255, callFunc);
callFunc(0, 0);
switch (waitKey(0)) {
case 27:
Size framesize(cap.get(CV_CAP_PROP_FRAME_WIDTH), cap.get(CV_CAP_PROP_FRAME_HEIGHT));
VideoWriter output_cap("D:/out.avi", CV_FOURCC('D','I', 'V', '3'), cap.get(CV_CAP_PROP_FPS), framesize, 1);
for (int k = 0; k < frm_count; k++) {
cap.set(CAP_PROP_POS_FRAMES, k);
cap >> frame1;
cap.set(CAP_PROP_POS_FRAMES, k + 1);
cap >> frame2;
absdiff(frame1, frame2, frame3);
threshold(frame3, frame4, tValue, 255, THRESH_BINARY);
output_cap.write(frame4);
}
return 0;
}
return 0;
}
答案 0 :(得分:0)
我认为你的问题在于切换永远不会得到值27.试试这个:
char key = waitKey(0);
switch (key){
不同之处在于您将waitkey的结果存储在char变量中,因此如果按ESC键,则值为27.执行此操作的方法,它将waitkey的结果存储在int变量中,以便获取值1048603.
您也可以在代码中更改27到1048603.