crash in Mat::release()

asked 2020-09-30 07:37:59 -0600

Dudi gravatar image

updated 2020-09-30 19:18:00 -0600

Hi, I have a function that creates a histogram:

double median_(const cv::Mat& channel)
{
    double m = (channel.rows*channel.cols) / 2;
    int bin = 0;
    double med = -1.0;

    int histSize = 256;
    float range[] = { 0, 256 };
    const float* histRange = { range };
    bool uniform = true;
    bool accumulate = false;
    cv::Mat hist;
    cv::calcHist(&channel, 1, 0, cv::Mat(), hist, 1, &histSize, &histRange, uniform, accumulate);

    for (int i = 0; i < histSize && med < 0.0; ++i)
    {
        bin += cvRound(hist.at< float >(i));
        if (bin > m && med < 0.0)
            med = i;
    }

    hist.release();
    return med;
}

Calling hist.release() is crashing. Can someone help please?

edit retag flag offensive close merge delete

Comments

Do not call release(), let it go out of scope.

Der Luftmensch gravatar imageDer Luftmensch ( 2020-09-30 19:20:28 -0600 )edit

Why do you put & in front of channel argument? There is usually no reason to pass cv::Mat pointers.

Der Luftmensch gravatar imageDer Luftmensch ( 2020-10-01 11:36:14 -0600 )edit

Hi Der, thanks for your reply... Ignore the sample above and check the simple sample below: void f6() { cv::Mat src = cv::imread("d:\file.jpg"); cv::Mat scaled; float scale = 0.25; cv::resize(src, scaled, cv::Size(), scale, scale, CV_INTER_CUBIC); }

The function crashes while the destruction of cv::Mat is executed. It crashes in the function deallocate() in Mat::release(). I don't understand the reason for the crash.

Dudi gravatar imageDudi ( 2020-10-04 03:02:54 -0600 )edit