Ask Your Question
0

Microsoft C++ exception: cv::Exception at memory location

asked 2016-08-12 05:21:26 -0600

scholar gravatar image

Iwant to implement a simple thresholding algorithm by comparing the pixel values of an image with some threshold and then assign either 0 or 1 to the pixels of output value correspondingly. a part of the code is:

cv::Mat gray , binary; // gray is input and binary is output image.

binary = Mat::zeros(gray.size(), gray.type());

for (int i=0;i<=row;i++)
{
    for (int j = 0; j <= col;j++)
    {

        if ((gray.at<uchar>(i, j) > NiblackThreshold)
        {
            binary.at<uchar>(i, j) = 1;
        }
        else
        {
            binary.at<uchar>(i, j) = 0;
        }

    }

Exceptions is at: template<typename _tp=""> inline _Tp& Mat::at(int i0, int i1) { CV_DbgAssert( dims <= 2 && data && (unsigned)i0 < (unsigned)size.p[0] && (unsigned)(i1DataType<_Tp>::channels) < (unsigned)(size.p[1]channels()) && CV_ELEM_SIZE1(DataType<_Tp>::depth) == elemSize1()); return ((_Tp)(data + step.p[0]i0))[i1]; }

where is the problem?????

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
1

answered 2016-08-12 05:31:28 -0600

berak gravatar image

updated 2016-08-12 05:42:45 -0600

"I want to implement a simple thresholding algorithm" - please use the builtin threshold() function, do not reinvent the wheel. writing "per-pixel loops" is absolutely an anti-pattern.

both : ì <= rows and j <= cols will be out of bounds. again, please do not write such loops.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-08-12 05:20:24 -0600

Seen: 1,387 times

Last updated: Aug 12 '16