Search code examples
opencvvisual-studio-2012pixelgrayscale

Accessing pixel value with .at<uchar> and .at<Vec3b> does not work


I am trying to rewrite the value for a pixel in a Mat-type grayscale image using Visual Studio Express 2012 for Windows Desktop. I've tried:

imgGrayComp.at<uchar>(5, 4) = 0; 

.

imgGrayComp.ptr(4)[5] = 0; 

.

imgGrayComp.at<Vec3b>(5, 4) = 0;

But it gives me this instead:

OpenCV Error: Assertion failed (dims <= 2 && data && (unsigned)i0 < (unsigned)size.p[0] && (unsigned)(i1DataType<_Tp>::channels) < (unsigned)(size.p[1]channels()) && ((((sizeof(size_t)<<28)|0x8442211) >> ((DataType<_Tp>::depth) & ((1 << 3) - 1))*4) & 15) == elemSize1()) in cv::Mat::at, file c:\opencv\build\include\opencv2\core\mat.hpp, line 537

==========================================================================================

EDIT - Here's the code:

Calling classify():

classify("./src/0.jpg", contour, hierarchy, mDatabase, '0');

In classify():

void classify(std::string imageFile, 
    vector<vector<Point> > contour,
    vector<Vec4i> hierarchy, float mDatabase [][charsToClassify],
    char whichChar)
{
    Mat image = imread(imageFile, 1);
    Mat imageGrayClassify;

    image.convertTo(imageGrayClassify, COLOR_BGR2GRAY);
    int numPoints = computeNumContourPts(imageGrayClassify); // calling compute...()
}

In compute...():

int computeNumContourPts(Mat imgGrayComp)
{

    // dilation x1 (increases workload):
    dilate(imgGrayComp, imgGrayComp, Mat(), Point(-1,-1), 2);
    // erosion x1 (decreases workload):
    erode(imgGrayComp, imgGrayComp, Mat(), Point(-1,-1), 1);

    imgGrayComp.at<uchar>(5, 4) = 0;
}

Solution

  • The problem lies in the fact that:

    Instead of using cv::cvtColor(image, imageGrayClassify, CV_BGR2GRAY);, you used image.convertTo() which is meant to convert the datatype of the image.

    In regard to the comment of a new error to Canberk's answer, it's simple, chances are your image is null/empty.

    Use

    CV_Assert(!image.empty()); //use this then the cvtColor, note this is a comment!!!
    
    cvtColor(image,imageGrayClassify, CV_BGR2GRAY);
    

    Alternatively, you can just use imshow to see if image is displayed successfully before even using cvtColor. Cheers.