Ask Your Question

Revision history [back]

Unhandled exception on accessing pixel data in OpenCV

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.