Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Exception Handling

I am using a function like this;

#include <exception>
Mat large = imread(path+name);
Mat rgb;
if (large.rows > 2500 || large.cols > 1250)
{
    pyrDown(large, rgb);
}
else
{
    rgb = large.clone();
}
cv::Mat smallx;
cvtColor(rgb, smallx, CV_BGR2GRAY);
Mat grad;
Mat morphKernel = getStructuringElement(MORPH_ELLIPSE, Size(3, 3));
cv::morphologyEx(smallx, grad, MORPH_GRADIENT, morphKernel);

Mat bw;
cv::threshold(document, document, 100, 255, cv::THRESH_BINARY + cv::THRESH_OTSU);

cv::threshold(grad, bw, 100, 255, THRESH_BINARY + THRESH_OTSU);

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

Mat mask = Mat::zeros(bw.size(), CV_8UC1);
vector<vector<Point>> contours(50000);
vector<Vec4i> hierarchy(50000);
cv::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]);
    counter++;

    Mat maskROI(mask, rect);
    maskROI = Scalar(0, 0, 0);
    // fill the contour
    drawContours(mask, contours, idx, Scalar(255, 255, 255), CV_FILLED);
    // ratio of non-zero pixels in the filled region
    double r = (double)countNonZero(maskROI) / (rect.width*rect.height);

    if (r > .45 && (rect.height > 8 && rect.width > 8))
    {
        rectangle(rgb, rect, Scalar(0, 255, 255), 2);

        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) + "#");

    }
}
return coordinates;

Some input images prompt exception when function returns, it throws me to "_Mybase::deallocate(_Ptr, _Count);" code block in xmemory0 class. I must catch this kind of errors and prevent this process from unexpected suspensions

Exception Handling

I am using a function like this;

#include <exception>
Mat large = imread(path+name);
Mat rgb;
if (large.rows > 2500 || large.cols > 1250)
{
    pyrDown(large, rgb);
}
else
{
    rgb = large.clone();
}
cv::Mat smallx;
cvtColor(rgb, smallx, CV_BGR2GRAY);
Mat grad;
Mat morphKernel = getStructuringElement(MORPH_ELLIPSE, Size(3, 3));
cv::morphologyEx(smallx, grad, MORPH_GRADIENT, morphKernel);

Mat bw;
cv::threshold(document, document, 100, 255, cv::THRESH_BINARY + cv::THRESH_OTSU);

cv::threshold(grad, bw, 100, 255, THRESH_BINARY + THRESH_OTSU);

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

Mat mask = Mat::zeros(bw.size(), CV_8UC1);
vector<vector<Point>> contours(50000);
vector<Vec4i> hierarchy(50000);
cv::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]);
    counter++;

    Mat maskROI(mask, rect);
    maskROI = Scalar(0, 0, 0);
    // fill the contour
    drawContours(mask, contours, idx, Scalar(255, 255, 255), CV_FILLED);
    // ratio of non-zero pixels in the filled region
    double r = (double)countNonZero(maskROI) / (rect.width*rect.height);

    if (r > .45 && (rect.height > 8 && rect.width > 8))
    {
        rectangle(rgb, rect, Scalar(0, 255, 255), 2);

        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) + "#");

    }
}
return coordinates;

Some input images prompt exception when function returns, it throws me to "_Mybase::deallocate(_Ptr, _Count);" code block in xmemory0 class. I must catch this kind of errors and prevent this process from unexpected suspensions

Exception Handling

I am using a function like this;

Mat large = imread(path+name);
Mat rgb;
if (large.rows > 2500 || large.cols > 1250)
{
    pyrDown(large, rgb);
}
else
{
    rgb = large.clone();
}
cv::Mat smallx;
cvtColor(rgb, smallx, CV_BGR2GRAY);
Mat grad;
Mat morphKernel = getStructuringElement(MORPH_ELLIPSE, Size(3, 3));
cv::morphologyEx(smallx, grad, MORPH_GRADIENT, morphKernel);

Mat bw;
cv::threshold(document, document, 100, 255, cv::THRESH_BINARY + cv::THRESH_OTSU);

cv::threshold(grad, bw, 100, 255, THRESH_BINARY + THRESH_OTSU);

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

Mat mask = Mat::zeros(bw.size(), CV_8UC1);
vector<vector<Point>> contours(50000);
vector<Vec4i> hierarchy(50000);
cv::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]);
    counter++;

    Mat maskROI(mask, rect);
    maskROI = Scalar(0, 0, 0);
    // fill the contour
    drawContours(mask, contours, idx, Scalar(255, 255, 255), CV_FILLED);
    // ratio of non-zero pixels in the filled region
    double r = (double)countNonZero(maskROI) / (rect.width*rect.height);

    if (r > .45 && (rect.height > 8 && rect.width > 8))
    {
        rectangle(rgb, rect, Scalar(0, 255, 255), 2);

        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) + "#");

    }
}
return coordinates;

Some input images prompt exception when function returns, it throws me to "_Mybase::deallocate(_Ptr, _Count);" code block in xmemory0 class. I must catch this kind of errors and prevent this process from unexpected suspensions

--------------UPDATE-------------- After my debug, I found that exception thrown by cv::findContours(connected, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); statement.

Exception Handling

I am using a function like this;

Mat large = imread(path+name);
Mat rgb;
if (large.rows > 2500 || large.cols > 1250)
{
    pyrDown(large, rgb);
}
else
{
    rgb = large.clone();
}
cv::Mat smallx;
cvtColor(rgb, smallx, CV_BGR2GRAY);
Mat grad;
Mat morphKernel = getStructuringElement(MORPH_ELLIPSE, Size(3, 3));
cv::morphologyEx(smallx, grad, MORPH_GRADIENT, morphKernel);

Mat bw;
cv::threshold(document, document, 100, 255, cv::THRESH_BINARY + cv::THRESH_OTSU);

cv::threshold(grad, bw, 100, 255, THRESH_BINARY + THRESH_OTSU);

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

Mat mask = Mat::zeros(bw.size(), CV_8UC1);
vector<vector<Point>> contours(50000);
vector<Vec4i> hierarchy(50000);
cv::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]);
    counter++;

    Mat maskROI(mask, rect);
    maskROI = Scalar(0, 0, 0);
    // fill the contour
    drawContours(mask, contours, idx, Scalar(255, 255, 255), CV_FILLED);
    // ratio of non-zero pixels in the filled region
    double r = (double)countNonZero(maskROI) / (rect.width*rect.height);

    if (r > .45 && (rect.height > 8 && rect.width > 8))
    {
        rectangle(rgb, rect, Scalar(0, 255, 255), 2);

        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) + "#");

    }
}
return coordinates;

Some input images prompt exception when function returns, it throws me to "_Mybase::deallocate(_Ptr, _Count);" code block in xmemory0 class. I must catch this kind of errors and prevent this process from unexpected suspensions

--------------UPDATE-------------- --------------UPDATE--------------

After my debug, I found that exception thrown by

cv::findContours(connected, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

statement.

Exception HandlingFindcontours() deallocation error

I am using a function like this;

Mat large = imread(path+name);
Mat rgb;
if (large.rows > 2500 || large.cols > 1250)
{
    pyrDown(large, rgb);
}
else
{
    rgb = large.clone();
}
cv::Mat smallx;
cvtColor(rgb, smallx, CV_BGR2GRAY);
Mat grad;
grad,connected,bw;

Mat morphKernel = getStructuringElement(MORPH_ELLIPSE, Size(3, 3));
cv::morphologyEx(smallx, grad, MORPH_GRADIENT, morphKernel);

Mat bw;
cv::threshold(document, document, 100, 255, cv::THRESH_BINARY + cv::THRESH_OTSU);

cv::threshold(grad, bw, 100, 255, THRESH_BINARY + THRESH_OTSU);

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

Mat mask = Mat::zeros(bw.size(), CV_8UC1);
vector<vector<Point>> contours(50000);
vector<Vec4i> hierarchy(50000);
 cv::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]);
    counter++;

    Mat maskROI(mask, rect);
    maskROI = Scalar(0, 0, 0);
    // fill the contour
    drawContours(mask, contours, idx, Scalar(255, 255, 255), CV_FILLED);
    // ratio of non-zero pixels in the filled region
    double r = (double)countNonZero(maskROI) / (rect.width*rect.height);

    if (r > .45 && (rect.height > 8 && rect.width > 8))
    {
        rectangle(rgb, rect, Scalar(0, 255, 255), 2);

        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) + "#");

    }
}
return coordinates;

Some input images prompt exception when function returns, it throws me to "_Mybase::deallocate(_Ptr, _Count);" code block in xmemory0 class. I must catch this kind of errors and prevent this process from unexpected suspensions

--------------UPDATE--------------

After my debug, I found that exception thrown by

cv::findContours(connected, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

statement.

statement. --------------UPDATE2-------------- I tried that code block with 30 different input but it gave error in a few image. after findcontours it thrown an exception break and show me this message.

Unhandled exception at 0x74477fb2 (ucrtbase.dll) in ImageRecognizer.exe: 0xC0000409: 0xc0000409.

in that time "vector" class opening in my Visual Studio with those line ;

~vector() _NOEXCEPT
    {   // destroy the object
    _Tidy();
    }

Findcontours() deallocation error

I am using a function like this;

Mat large = imread(path+name);
Mat rgb;
if (large.rows > 2500 || large.cols > 1250)
{
    pyrDown(large, rgb);
}
else
{
    rgb = large.clone();
}
cv::Mat smallx;
cvtColor(rgb, smallx, CV_BGR2GRAY);
Mat grad,connected,bw;

Mat morphKernel = getStructuringElement(MORPH_ELLIPSE, Size(3, 3));
cv::morphologyEx(smallx, grad, MORPH_GRADIENT, morphKernel);
cv::threshold(grad, bw, 100, 255, THRESH_BINARY + THRESH_OTSU);
morphKernel = getStructuringElement(MORPH_RECT, Size(9, 1));
cv::morphologyEx(bw, connected, MORPH_CLOSE, morphKernel);

Mat mask = Mat::zeros(bw.size(), CV_8UC1);
vector<vector<Point>> contours(50000);
vector<Vec4i> hierarchy(50000);

cv::findContours(connected, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

Some input images prompt exception when function returns, it throws me to "_Mybase::deallocate(_Ptr, _Count);" code block in xmemory0 class. I must catch this kind of errors and prevent this process from unexpected suspensions

--------------UPDATE--------------

After my debug, I found that exception thrown by

cv::findContours(connected, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

statement. --------------UPDATE2-------------- I tried that code block with 30 different input but it gave error in a few image. after findcontours it thrown an exception break and show me this message.

Unhandled exception at 0x74477fb2 (ucrtbase.dll) in ImageRecognizer.exe: 0xC0000409: 0xc0000409.

in that time , "vector" class opening in my Visual Studio with those line these lines ;

~vector() _NOEXCEPT
    {   // destroy the object
    _Tidy();
    }

or "xmemory" class opening with these lines;

void deallocate(pointer _Ptr, size_type _Count)
    {   // deallocate object at _Ptr, ignore size
    _Mybase::deallocate(_Ptr, _Count);
    }