C++ access violation exception in the function findContours()

asked 2017-12-26 04:56:30 -0600

Davidfun gravatar image

I use visual studio 2017, and in opencv 3.3. I try to run the program below and get an error that I do not know what the reason might be.

The problem is in the function findContours().

The code is taken fromReal-Time Object Tracking Without Colour

Exception thrown at 0x00007FFE57B53FB8 (KernelBase.dll) in opencvTry.exe: 0xC0000005: Access violation reading location 0x0000000000000000.

  #include "opencv2/imgproc/imgproc.hpp"
    #include "opencv2/highgui/highgui.hpp"
    #include <stdlib.h>
    #include <stdio.h>
    #include <iostream>
    #include <string>
    using namespace std;
    using namespace cv;

    void searchOfMoov(Mat , Mat &);
    string intToString(int);
    std::string i=" ";
    int theObject[2] = { 0,0 };
    VideoCapture capture;
    Mat frame1, frame2;
    Mat grayimage1, grayimage2;
    Mat diffrentimage;
    Mat thresholdimage;
    Rect objectBoundingRectangle = Rect(0, 0, 0, 0);
    string intToString(int number) {

        this function has a number input and string output
        std::stringstream ss;
        ss << number;
        return ss.str();
    }
    void searchOfMoov(Mat thresholdImage, Mat &cameraFeed) {
        bool objectDetected = false;
        Mat temp;
        thresholdImage.copyTo(temp);
        //these two vectors needed for output of findContours
        vector< vector<Point> > contours;
        vector<Vec4i> hierarchy;
        //find contours of filtered image using openCV findContours function
        findContours(temp,contours,hierarchy,CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE );// retrieves all contours
        findContours(temp, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);// retrieves external contours

                                                                                          //if contours vector is not empty, we have found some objects
        if (contours.size()>0)objectDetected = true;
        else objectDetected = false;

        if (objectDetected) {
            the largest contour is found at the end of the contours vector
            we will simply assume that the biggest contour is the object we are looking for.
            vector< vector<Point> > largestContourVec;
            largestContourVec.push_back(contours.at(contours.size() - 1));
            make a bounding rectangle around the largest contour then find its centroid
            this will be the object's final estimated position.
            objectBoundingRectangle = boundingRect(largestContourVec.at(0));
            int xpos = objectBoundingRectangle.x + objectBoundingRectangle.width / 2;
            int ypos = objectBoundingRectangle.y + objectBoundingRectangle.height / 2;

            update the objects positions by changing the 'theObject' array values
            theObject[0] = xpos, theObject[1] = ypos;
        }
        make some temp x and y variables so we dont have to type out so much
        int x = theObject[0];
        int y = theObject[1];
        draw some crosshairs on the object
        circle(cameraFeed, Point(x, y), 20, Scalar(0, 255, 0), 2);
        line(cameraFeed, Point(x, y), Point(x, y - 25), Scalar(0, 255, 0), 2);
        line(cameraFeed, Point(x, y), Point(x, y + 25), Scalar(0, 255, 0), 2);
        line(cameraFeed, Point(x, y), Point(x - 25, y), Scalar(0, 255, 0), 2);
        line(cameraFeed, Point(x, y), Point(x + 25, y), Scalar(0, 255, 0), 2);
        putText(cameraFeed, "Tracking object at (" + intToString(x) + "," + intToString(y) + ")", Point(x, y), 1, 1, Scalar(255, 0, 0), 2);



    }

    int main(){
        some boolean variables for added functionality
        bool objectDetected = false;
        these two can be toggled by pressing 'd' or 't'
        bool debugMode = false;
        bool trackingEnabled = false;
        pause and resume code
        bool pause = false;
        while (1) {
            capture.open("17-12-20_074100.avi");

            while (capture.get(CV_CAP_PROP_POS_FRAMES) < capture.get(CV_CAP_PROP_FRAME_COUNT) - 1) { //if video not end
                capture.read(frame1);
                cvtColor(frame1, grayimage1, COLOR_BGR2GRAY);
                capture.read(frame2);
                cvtColor(frame2, grayimage2, COLOR_BGR2GRAY ...
(more)
edit retag flag offensive close merge delete

Comments

"I use visual studio 2017" -- did you build the opencv libs locally ? (the prebuild libs are for vs2015 ONLY

(it's not your code, your vs project is broken)

berak gravatar imageberak ( 2017-12-26 05:16:45 -0600 )edit

This is code I took at the link I attached. So the reason is because of Visual Studio version?

Davidfun gravatar imageDavidfun ( 2017-12-26 05:21:23 -0600 )edit

"So the reason is because of Visual Studio version?" -- yes. either use vs2015, or build opencv libs from src, using cmake.

then make sure to ONLY use debug libs with a debug project, and release libs with RELEASE.

berak gravatar imageberak ( 2017-12-26 05:22:45 -0600 )edit

Thank you for your reference

Davidfun gravatar imageDavidfun ( 2017-12-26 05:37:02 -0600 )edit