0

I have wrote a shape detection code with c++. I am using Visual Studio 2013 Express Desktop Edition.

When I run the program it will give following error.

First-chance exception at 0x54EE3C77 (opencv_imgproc244d.dll) in Final.exe: 0xC0000005: Access violation reading location 0x05958000.

If there is a handler for this exception, the program may be safely continued.

Following is the code sample.

#include "stdafx.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <thread>  
#include <cv.h>
#include <highgui.h>
#include <windows.h>




void shapeDetectionNew(IplImage* img)
{

try
{

    //converting the original image into grayscale
    IplImage* imgGrayScale = cvCreateImage(cvGetSize(img), 8, 1);
    cvCvtColor(img, imgGrayScale, CV_BGR2GRAY);

    //thresholding the grayscale image to get better results
    cvThreshold(imgGrayScale, imgGrayScale, 128, 255, CV_THRESH_BINARY);

    CvSeq* contour;  //hold the pointer to a contour
    CvSeq* result;   //hold sequence of points of a contour
    CvMemStorage *storage = cvCreateMemStorage(0); //storage area for all contours

    //finding all contours in the image
    cvFindContours(imgGrayScale, storage, &contour, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));

    //***********************************
    cv::Mat img = cv::imread("F:\\My Works\\Opencv\\Shape_Detection_Images\\shape.jpg");

    //convert IplImage to Mat
    //cv::Mat img(iplImg);

    cv::Mat gray;
    cv::cvtColor(img, gray, CV_BGR2GRAY);

    // Use Canny instead of threshold to catch squares with gradient shading
    cv::Mat bw;
    cv::Canny(gray, bw, 0, 50, 5);

    // Find contours
    std::vector<std::vector<cv::Point> > contours;
    cv::findContours(bw.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);

    std::vector<cv::Point> approx;
    cv::Mat dst = img.clone();

    //***********************************

    for (int i = 0; i < contours.size(); i++)
    {
        // Approximate contour with accuracy proportional
        // to the contour perimeter
        cv::approxPolyDP(cv::Mat(contours[i]), approx, cv::arcLength(cv::Mat(contours[i]), true)*0.02, true);

        // Skip small or non-convex objects 
        if (std::fabs(cv::contourArea(contours[i])) < 100 || !cv::isContourConvex(approx))
            continue;

        if (approx.size() == 3)
        {
            setLabel(dst, "TRI", contours[i]);    // Triangles
        }
        else if (approx.size() >= 4 && approx.size() <= 6)
        {
            // Number of vertices of polygonal curve
            int vtc = approx.size();

            // Get the cosines of all corners
            std::vector<double> cos;
            for (int j = 2; j < vtc + 1; j++)
                cos.push_back(angle(approx[j%vtc], approx[j - 2], approx[j - 1]));

            // Sort ascending the cosine values
            std::sort(cos.begin(), cos.end());

            // Get the lowest and the highest cosine
            double mincos = cos.front();
            double maxcos = cos.back();

            // Use the degrees obtained above and the number of vertices
            // to determine the shape of the contour
            if (vtc == 4 && mincos >= -0.1 && maxcos <= 0.3)
                setLabel(dst, "RECT", contours[i]);
            else if (vtc == 5 && mincos >= -0.34 && maxcos <= -0.27)
                setLabel(dst, "PENTA", contours[i]);
            else if (vtc == 6 && mincos >= -0.55 && maxcos <= -0.45)
                setLabel(dst, "HEXA", contours[i]);
        }
        else
        {
            // Detect and label circles
            double area = cv::contourArea(contours[i]);
            cv::Rect r = cv::boundingRect(contours[i]);
            int radius = r.width / 2;

            if (std::abs(1 - ((double)r.width / r.height)) <= 0.2 &&
                std::abs(1 - (area / (CV_PI * std::pow((double)radius, 2)))) <= 0.2)
                setLabel(dst, "CIR", contours[i]);
        }
    }

    cv::imshow("Shape_Detection", dst);


}
catch (int e)
{
    throw e;
}
}

int main()
{
    try
    {

    // Create CvCapture object to grab data from the webcam
    CvCapture* pCapture;

    // Start capturing data from the webcam
    pCapture = cvCaptureFromCAM(1);

    // Define the IplImage pointers we're going to use as globals
    IplImage* pFrame;

    IplImage* pProcessedFrame;
    IplImage* tempFrame;
    pFrame = cvQueryFrame(pCapture);

    //Create the low threshold slider
    // Format: Slider name, window name, reference to variable for slider, max value of slider, callback function
    cvCreateTrackbar("Low Threshold", "Edge_Detection", &lowSliderPosition, maxLowThreshold, onLowThresholdSlide);

    // Create the high threshold slider
    cvCreateTrackbar("High Threshold", "Edge_Detection", &highSliderPosition, maxHighThreshold, onHighThresholdSlide);

    // Create a greyscale image which is the size of our captured image
    pProcessedFrame = cvCreateImage(cvSize(pFrame->width, pFrame->height), IPL_DEPTH_8U, 1);

    // Create a frame to use as our temporary copy of the current frame but in grayscale mode
    tempFrame = cvCreateImage(cvSize(pFrame->width, pFrame->height), IPL_DEPTH_8U, 1);

    char keypress;
    bool quit = false;
    int counterCheck = 1;

    while (quit == false)
    {


        // Make an image from the raw capture data
        // Note: cvQueryFrame is a combination of cvGrabFrame and cvRetrieveFrame
        pFrame = cvQueryFrame(pCapture);

        // Draw the original frame in our window
        cvShowImage("Live_Cam", pFrame);




        shapeDetectionNew(pFrame);



    } // End of while loop

    cvDestroyAllWindows();

}
catch (Exception ex)
{

}

return 1;
}
las
  • 67
  • 1
  • 3
  • 13
  • I don't know MSVS, but somehow "access violation" makes me doubt that there's "nothing wrong with the code"...so, maybe [this answer](http://stackoverflow.com/a/9313101/2689986) might help you. – shad0w_wa1k3r Nov 17 '13 at 04:30
  • Thank you for reply, I will update this with the code – las Nov 17 '13 at 05:35
  • Did you try reproducing in the debugger? Where in your code does the backtrace/callstack show the crash happening? – kfsone Nov 17 '13 at 06:59
  • 1
    I would like some clarifications. 1. How are you linking the OpenCV libraries.? 2. Are you pre-built binaries? 3. What is your system config? 4. Are you aware that [VS 2013 is still not officially supported by OpenCV](http://code.opencv.org/issues/3266#note-6) {I know this because I was trying to work on VS 2013 trial and I couldn't even build the binaries}? – scap3y Nov 17 '13 at 08:45
  • please, avoid using the old c-api – berak Nov 17 '13 at 14:29
  • thnx for the replies, @scap3y I am using pre-built binaries with Windows 7 OpenCV version is 2.4.4 – las Nov 17 '13 at 15:25
  • The thing is (as I said before), VS 2013 is still not officially supported by OpenCV. You need to try and build your own binaries and if you get it to work, let me know how you did it so that I can do the same. :D – scap3y Nov 17 '13 at 16:29
  • I have build the binaries using VS2010, after that i am using that project with VS2013 – las Nov 18 '13 at 00:09

1 Answers1

0

You are not checking that the result of

 pFrame = cvQueryFrame(pCapture);

is not null. If it is, eventuallu this will get passed to cvCvtColor() and hence the exception.

Completely separate problem:imshow() will not display anything without a subsequent call to waitKey()

Bull
  • 11,771
  • 9
  • 42
  • 53