# Unexpected values for color at specific pixel

**URL:** <https://forum.opencv.org/t/unexpected-values-for-color-at-specific-pixel/1547>\
**Category:** Uncategorized\
**Created:** [February 14, 2021, 5:05pm UTC](https://forum.opencv.org/t/unexpected-values-for-color-at-specific-pixel/1547 "2021-02-14T17:05:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![justphilipp](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@justphilipp](https://forum.opencv.org/u/justphilipp)\
**Post date:** [February 14, 2021, 5:05pm UTC](https://forum.opencv.org/t/unexpected-values-for-color-at-specific-pixel/1547/1 "2021-02-14T17:05:17Z")

</div>

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.  
[https://answers.opencv.org/question/1870/find-pixel-color-out-of-cvmat-on-specific-position/](https://answers.opencv.org/question/1870/find-pixel-color-out-of-cvmat-on-specific-position/)

> <https://stackoverflow.com/questions/23001512/c-and-opencv-get-and-set-pixel-color-to-mat>

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,  
Philipp

---

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [February 14, 2021, 5:35pm UTC](https://forum.opencv.org/t/unexpected-values-for-color-at-specific-pixel/1547/2 "2021-02-14T17:35:54Z")

</div>

Hi,  
You wrote CV\_8UC4 it means four channels B,G,R,X x can bewhat you want.

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

```

It’s only three channel and Ve3b too so it’s wrong  
Use CV\_8UC3 :

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

```

---

<div class="post-metadata">

**Author:** ![justphilipp](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@justphilipp](https://forum.opencv.org/u/justphilipp)\
**Post date:** [February 14, 2021, 7:53pm UTC](https://forum.opencv.org/t/unexpected-values-for-color-at-specific-pixel/1547/3 "2021-02-14T19:53:21Z")

</div>

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

Best,  
Philipp
