Exception in xmemory _Mybase::deallocate(_Ptr, _Count);

asked 2017-04-17 05:36:10 -0500

renV gravatar image

This is my function. It has memory error after finis process at return statememt in some inputs.

    Mat large = imread(path);
Mat rgb;

pyrDown(large, rgb);

Mat small;
cvtColor(rgb, small, CV_BGR2GRAY);

Mat grad;
Mat morphKernel = getStructuringElement(MORPH_ELLIPSE, Size(3, 3));
morphologyEx(small, grad, MORPH_GRADIENT, morphKernel);

Mat bw;
threshold(grad, bw, 0.0, 255.0, THRESH_BINARY | THRESH_OTSU);

Mat connected;
morphKernel = getStructuringElement(MORPH_RECT, Size(9, 1));
morphologyEx(bw, connected, MORPH_CLOSE, morphKernel);

Mat mask = Mat::zeros(bw.size(), CV_8UC1);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(connected, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));


int counter = 0;
std::string coordinates = "";


for (int idx = 0; idx >= 0; idx = hierarchy[idx][0])
{   
    Rect rect = boundingRect(contours[idx]);


    Mat maskROI(mask, rect);
    maskROI = Scalar(0, 0, 0);

    drawContours(mask, contours, idx, Scalar(255, 255, 255), CV_FILLED);

    double r = (double)countNonZero(maskROI) / (rect.width*rect.height);

    if (r > .45  && (rect.height > 8 && rect.width > 8) )
    {
        rectangle(rgb, rect, Scalar(0, 255, 0), 1);
        coordinates = coordinates + (std::to_string(counter) + "," + std::to_string(rect.x) + ", " + std::to_string(rect.y) + ", " + std::to_string(rect.width) + ", " + std::to_string(rect.height) + "#");

        string disp =  std::to_string(rect.x) + ", " + std::to_string(rect.y) +  ", " + std::to_string(rect.width) + ", "  + std::to_string(rect.height);
        cv::putText(rgb, disp, Point(rect.x, rect.y), FONT_HERSHEY_SIMPLEX,0.35, Scalar(0, 0, 0),1);            
        counter++;
    }
}
return coordinates;
edit retag flag offensive close merge delete