OpenCV Access violation reading location C++Cli

0

I'm developing a simple C++/CLI wrapper for OpenCV to use in my C# application. I am referring to Edge based template matching but the code available is using older openCV libraries ( cv200.dll, cxcore200.dll, highgui200.dll).

When I implement the same program using OpenCV3 ( opencv_ffmpeg348_64.dll, and others), I get the following error on one of the lines:

Exception thrown at 0x0000019972FBDB65 (opencv_world348.dll) in appname.exe: 0xC0000005: Access violation reading location 0x000001996DD8FFF8.

Here's the function I have:

int PatternMatching::FindPattern1(System::Drawing::Bitmap^ sourceImageB,
    System::Drawing::Bitmap^ templateImageB,
    int lowThreshold,
    int highThreashold,
    double minScore,
    double greediness) {

    cv::Point result;

    cv::Mat sourceMat = KudammUtils::BitmapToMat(sourceImageB);
    cv::Mat templateMat = KudammUtils::BitmapToMat(templateImageB);

    IplImage* templateImage = new IplImage(templateMat);
    IplImage* searchImage = new IplImage(sourceMat);

    cv::imwrite("tmp.jpg", templateMat); // this works
    cv::imwrite("sce.jpg", sourceMat); // this works as well

    if (templateImage == NULL)
    {
        cout << "\nERROR: Could not load Template Image";
        return 0;
    }

    if (searchImage == NULL)
    {
        cout << "\nERROR: Could not load Search Image";
        return 0;
    }

    CvSize templateSize = cvSize(templateImage->width, templateImage->height);
    IplImage* grayTemplateImg = cvCreateImage(templateSize, IPL_DEPTH_8U, 1);


    cout << "\nhere0";
    if (templateImage->nChannels == 3)
    {
        cvCvtColor(templateImage, grayTemplateImg, CV_RGB2GRAY);
    }
    else
    {
        cvCopy(templateImage, grayTemplateImg);
    }

    cout << "\nhere1";

    if (!GM->CreateGeoMatchModel(grayTemplateImg, lowThreshold, highThreashold))
    {
        cout << "ERROR: could not create model...";
        throw gcnew System::Exception("Could not create model");
    }

    cout << "\nhere2";

    GM->DrawContours(templateImage, CV_RGB(255, 0, 0), 1);
    cout << " Shape model created.."; // <- GETS PRINTED TILL HERE

    CvSize searchSize = cvSize(searchImage->width, searchImage->height);

    cout << "\nhere3"; // <- THIS DOESN'T GET PRINTED

    IplImage* graySearchImg = cvCreateImage(searchSize, IPL_DEPTH_8U, 1);

    cout << "\nhere4";

    // Convert color image to gray image. 
    if (searchImage->nChannels == 3)
        cvCvtColor(searchImage, graySearchImg, CV_RGB2GRAY);
    else
    {
        cvCopy(searchImage, graySearchImg);
    }

    clock_t start_time1 = clock();
    score = GM->FindGeoMatchModel(graySearchImg, minScore, greediness, &result);
    clock_t finish_time1 = clock();
    total_time = (double)(finish_time1 - start_time1) / CLOCKS_PER_SEC;

    if (score > minScore) // if score is atleast 0.4
    {
        cout << " Found at [" << result.x << ", " << result.y << "]\n Score = " << score << "\n Searching Time = " << total_time * 1000 << "ms";
        GM->DrawContours(searchImage, result, CV_RGB(0, 255, 0), 1);
    }
    else
        cout << " Object Not found";

    cv::Mat outputMat = cv::cvarrToMat(searchImage);
    System::Drawing::Bitmap^ outputBitmap = KudammUtils::MatToBitmap(outputMat);

    cvReleaseImage(&searchImage);
    cvReleaseImage(&templateImage);
    cvReleaseImage(&grayTemplateImg);
    cvReleaseImage(&graySearchImg);

    return 1;
}

enter image description here

Any idea what might be causing this error ? What am I doing wrong ?

c++
opencv
c++-cli
opencv3.0
asked on Stack Overflow Jan 8, 2020 by mrid

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0