Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Access violation writing location

I want to develop a sketch matching game using OpenCV. What I am trying to do is match an image and user's drawing using the Shape Context matching. I can successfully compile and run my project but when I call the findContour function the program will crash and will produce First-chance exception at 0x5C78DE7A (msvcp120d.dll) in DTPproject.exe: 0xC0000005: Access violation writing location 0x00000010 error.

here is my code

vector<cv::Point> CComputeDistance::simpleContour(const Mat& currentQuery, int n = 300)
{
    vector<vector<Point> > _contoursQuery;
    vector <Point> contoursQuery;
    findContours(currentQuery, _contoursQuery, RETR_LIST, CHAIN_APPROX_NONE);
    for (size_t border = 0; border<_contoursQuery.size(); border++)
    {
        for (size_t p = 0; p<_contoursQuery[border].size(); p++)
        {
            contoursQuery.push_back(_contoursQuery[border][p]);
        }
    }

    // In case actual number of points is less than n
    int dummy = 0;
    for (int add = (int)contoursQuery.size() - 1; add<n; add++)
    {
        contoursQuery.push_back(contoursQuery[dummy++]); //adding dummy values
    }

    // Uniformly sampling
    random_shuffle(contoursQuery.begin(), contoursQuery.end());
    vector<Point> cont;
    for (int i = 0; i<n; i++)
    {
        cont.push_back(contoursQuery[i]);
    }

    return cont;
}

void CComputeDistance::computeShapeDistance(Mat image, Mat drawing, float &result)
{
    Ptr <ShapeContextDistanceExtractor> mySC = createShapeContextDistanceExtractor();

    vector<cv::Point> a = simpleContour(image);
    vector<cv::Point> b = simpleContour(drawing);

    result = mySC->computeDistance(a, b);
}

void CComputeDistance::loopCompute(Mat image, Mat drawing, float &sum)
{
    int p = 20;
    for (int i = 0; i < 5; i++)
    {
        float shapeDistance;
        computeShapeDistance(image, drawing, shapeDistance);

        sum = sum + shapeDistance;
        p = p + 20;
    }
}

what I am really trying to achieve is get the value of "sum" after calling the method loopCompute in which it iterates 5x in loop.

Here is the code that call will the method

CComputeDistance::loopCompute( image, drawing, sum);

Im using visual studio 2013 community and this is what the local variable looks like after the break on debug mode.

image description

The program above runs perfectly if I run it in android but it fails on windows. I think it has something to do with the configuration of Visual Studio to OpenCV.

Any ideas?

Access violation writing location

I want to develop a sketch matching game using OpenCV. What I am trying to do is match an image and user's drawing using the Shape Context matching. I can successfully compile and run my project but when I call the findContour function the program will crash and will produce First-chance exception at 0x5C78DE7A (msvcp120d.dll) in DTPproject.exe: 0xC0000005: Access violation writing location 0x00000010 error.

here is my code

vector<cv::Point> CComputeDistance::simpleContour(const Mat& currentQuery, int n = 300)
{
    vector<vector<Point> > _contoursQuery;
    vector <Point> contoursQuery;
    findContours(currentQuery, _contoursQuery, RETR_LIST, CHAIN_APPROX_NONE);
    for (size_t border = 0; border<_contoursQuery.size(); border++)
    {
        for (size_t p = 0; p<_contoursQuery[border].size(); p++)
        {
            contoursQuery.push_back(_contoursQuery[border][p]);
        }
    }

    // In case actual number of points is less than n
    int dummy = 0;
    for (int add = (int)contoursQuery.size() - 1; add<n; add++)
    {
        contoursQuery.push_back(contoursQuery[dummy++]); //adding dummy values
    }

    // Uniformly sampling
    random_shuffle(contoursQuery.begin(), contoursQuery.end());
    vector<Point> cont;
    for (int i = 0; i<n; i++)
    {
        cont.push_back(contoursQuery[i]);
    }

    return cont;
}

void CComputeDistance::computeShapeDistance(Mat image, Mat drawing, float &result)
{
    Ptr <ShapeContextDistanceExtractor> mySC = createShapeContextDistanceExtractor();

    vector<cv::Point> a = simpleContour(image);
    vector<cv::Point> b = simpleContour(drawing);

    result = mySC->computeDistance(a, b);
}

void CComputeDistance::loopCompute(Mat image, Mat drawing, float &sum)
{
    int p = 20;
    for (int i = 0; i < 5; i++)
    {
        float shapeDistance;
        computeShapeDistance(image, drawing, shapeDistance);

        sum = sum + shapeDistance;
        p = p + 20;
    }
}

what I am really trying to achieve is get the value of "sum" after calling the method loopCompute in which it iterates 5x in loop.

Here is the code that call will the method

CComputeDistance::loopCompute( image, drawing, sum);

Im using visual studio 2013 community and this is what the local variable looks like after the break on debug mode.

image description

The program above runs perfectly if I run it in android but it fails on windows. I think it has something to do with the configuration of Visual Studio to OpenCV.

Any ideas?

EDIT: Here's what the CALL STACK looks like in Visual Studio. VISUAL STUDIO