Unexpected values for color at specific pixel

Hey everyone,

first of all thanks for reading my question!

I am new to OpenCV and currently I try to get the color at a specific pixel of my image. I looked at various other posts but I am very confused by the results.

Lets look at this test code:

    cv::Mat greenMat(cv::Size(300, 300), CV_8UC4);

    greenMat.setTo(cv::Scalar(0, 255, 0));

    for (int i = 0; i < greenMat.cols; i++) {
        for (int j = 0; j < greenMat.rows; j++) {
            Vec3b color = greenMat.at<Vec3b>(i, j);

            std::cout << "color: " << color << std::endl;

This creates a completely green image. Why is the output of the color not constantly a green one? Instead I get a mix of the following:

color: [255, 0, 0]
color: [0, 255, 0]
color: [0, 0, 255]
color: [0, 0, 0]

What am I misunderstanding here?

Thanks and best wishes,

You wrote CV_8UC4 it means four channels B,G,R,X x can bewhat you want.

cv::Scalar(0, 255, 0))

It’s only three channel and Ve3b too so it’s wrong
Use CV_8UC3 :

 cv::Mat greenMat(cv::Size(300, 300), CV_8UC3);

Hey Laurent,
You are correct, this works. Thanks so much for your answer!


1 Like