Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

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);
                absdiff(grayimage1, grayimage2, diffrentimage);

                threshold(diffrentimage, thresholdimage, 30, 255, THRESH_BINARY);
                threshold(thresholdimage, thresholdimage, 30, 255, THRESH_BINARY);

                blur(thresholdimage, thresholdimage, Size(10, 10));
                if (trackingEnabled) {
                    searchOfMoov(thresholdimage, frame1);
                }
                imshow("Frame1", frame1);
                imshow("Diffrent immage", diffrentimage);
                imshow("thresholdimage", thresholdimage);

                switch (waitKey(10)) {

                case 27: //'esc' key has been pressed, exit program.
                    return 0;
                case 116: //'t' has been pressed. this will toggle tracking
                    trackingEnabled = !trackingEnabled;
                    if (trackingEnabled == false) cout << "Tracking disabled." << endl;
                    else cout << "Tracking enabled." << endl;
                    break;
                case 100: //'d' has been pressed. this will debug mode
                    debugMode = !debugMode;
                    if (debugMode == false) cout << "Debug mode disabled." << endl;
                    else cout << "Debug mode enabled." << endl;
                    break;
                case 112: //'p' has been pressed. this will pause/resume the code.
                    pause = !pause;
                    if (pause == true) {
                        cout << "Code paused, press 'p' again to resume" << endl;
                        while (pause == true) {
                            stay in this loop until 
                            switch (waitKey()) {
                                a switch statement inside a switch statement? Mind blown.
                            case 112:
                                change pause back to false
                                pause = false;
                                cout << "Code resumed." << endl;
                                break;
                            }
                        }
                    }
                }

            }
            capture.release();
        }
        return 0;
    }

I tried the paths of the libraries and found no problem. Maybe I'm wrong...

I will thank you for any help.