Ask Your Question
0

Unhandled exception on accessing pixel data in OpenCV

asked 2019-09-24 09:11:24 -0600

daqs gravatar image

I'm writing a program to find sort pixels in an image by colors. After applying CV functions on the MAT, I am unable to access it's pixel data. It keeps showing unhandled exception.

cv::Mat im = cv::imread("test.png");

    for (int i = 0; i < lm.rows; i++)
    {
        for (int j = 0; j < lm.cols; j++)
        {
            cv::Vec3b bgrPixel = lm.at<cv::Vec3b>(i, j);   // WORKS!
        }
    }

    cv::resize(im, im, cv::Size(8, 8));
    cv::Vec3b* p = im.ptr<cv::Vec3b>();  
    vector<cv::Vec3b> pix(p, p + im.total());  
    vector<int> labels;
    int unique = cv::partition(pix, labels, pixel_equal);
    cv::Mat lm = cv::Mat(labels).reshape(1, im.rows);

    for (int i = 0; i < lm.rows; i++)
    {
        for (int j = 0; j < lm.cols; j++)
        {
            cv::Vec3b bgrPixel = lm.at<cv::Vec3b>(i, j);   // UNHANDLED EXCEPTION!
        }
    }

static bool pixel_equal(const cv::Vec3b& a, const cv::Vec3b& b)
{
    return cv::norm(a, b) < 90;
}

Help is appreciated.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2019-09-24 10:37:19 -0600

sjhalayka gravatar image

updated 2019-09-24 10:53:58 -0600

LBerger gravatar image

What happens if you swap i and j inside of the loop body?

cv::Vec3b bgrPixel = lm.at<cv::Vec3b>(j, i);

doc is here

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-09-24 09:11:24 -0600

Seen: 198 times

Last updated: Sep 24 '19