OpenCv - 内存位置C ++的异常

时间:2022-12-04 03:11:04

I have this program that loads in a video file, lets the user decide on the threshold value, and then writes out the video's "threshold representation" frame-by-frame. When i build and run it it works, but not before throwing the "Exception at memory location.." What's the problem? Thanks!

我有这个程序加载到视频文件中,让用户决定阈值,然后逐帧写出视频的“阈值表示”。当我构建并运行它时,它可以工作,但不是在抛出“内存位置的异常”之前。问题是什么?谢谢!

#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;


}

1 个解决方案

#1


0  

I think your problem is in switch never getting the value 27. Try this:

我认为你的问题是在切换永远不会得到值27.试试这个:

char key = waitKey(0);
switch (key){

The difference is that you store the result of waitkey in a char variable, so if you press ESC the value would be 27. The way you do it, it store the result of waitkey in a int variable so you get the value 1048603.

区别在于您将waitkey的结果存储在char变量中,因此如果按ESC键,则值为27.执行此操作的方法是将waitkey的结果存储在int变量中,以便获得值1048603。

You could also only change 27 by 1048603 in your code.

您也可以在代码中更改27到1048603。

#1


0  

I think your problem is in switch never getting the value 27. Try this:

我认为你的问题是在切换永远不会得到值27.试试这个:

char key = waitKey(0);
switch (key){

The difference is that you store the result of waitkey in a char variable, so if you press ESC the value would be 27. The way you do it, it store the result of waitkey in a int variable so you get the value 1048603.

区别在于您将waitkey的结果存储在char变量中,因此如果按ESC键,则值为27.执行此操作的方法是将waitkey的结果存储在int变量中,以便获得值1048603。

You could also only change 27 by 1048603 in your code.

您也可以在代码中更改27到1048603。