简体   繁体   中英

OpenCV Gives an error when using the imgproc functions

when I compile and run this code, I get an error. It compiles, but when I try to run it, it gives the following error:

The application has requested the Runtime to terminate in an unusual way.

This is the code:

#include <opencv2/opencv.hpp>
#include <string>

int main() {
    cv::VideoCapture c(0);
    double rate = 10;
    bool stop(false);
    cv::Mat frame;
    cv::namedWindow("Hi!");
    int delay = 1000/rate;
    cv::Mat corners;
    while(!stop){
        if(!c.read(frame))
            break;
        cv::cornerHarris(frame,corners,3,3,0.1);
        cv::imshow("Hi!",corners);
        if(cv::waitKey(delay)>=0)
            stop = true;
    }
    return 0;
}

BTW, I get the same error when using the Canny edge detector.

Your corners matrix is declared as a variable, but there is no memory allocated to it. The same with your frame variable. First you have to create a matrix big enough for the image to fit into it.

I suggest you first take a look at cvCreateImage so you can learn how basic images are created and handled, before you start working with video streams.

Make sure the capture is ready, and the image is ok:

if(!cap.IsOpened())
   break;

if(!c.read(frame))
        break;

if(frame.empty())
    break;

You need to convert the image to grayscale before you use the corner detector:

cv::Mat frameGray;
cv::cvtColor(frame, frameGray, CV_RGB2GRAY);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM