C++ – Change single pixel color in openCV

ccolorsimageopencvpixel

I am trying to read specific pixel-colors with openCV from a cv::mat, but i get different results for the same color. So i tried to test to write pixel colors, which results also in strange colors that differs from my input.
for reading pixel colors i use:

Mat dst;
struct  {
    Vec3b colorrgb[10];
} RGBscan;
int aktivy[11] = {0, 430, 200, 170, 140, 200, 420, 670, 725, 725, 670};
int aktivx[11] = {0, 50, 200, 480, 920, 1200, 1360, 1200, 880, 540, 220};
for (int i = 1; i < 11; i++) {
    RGBscan.colorrgb[i] = dst.at<Vec3b>(aktivy[i], aktivx[i]);
    cout << "rgbcode: " << RGBscan.colorrgb[i] << "  x: " << aktivx[i] 
         << "  y: " << aktivy[i] << "\n";
};

though some of the pixels contain the same color in the cv::Mat dst i get different results in the command line.

Also, when i try so set the pixel to another color with:

dst.at<Vec3b>(436, 50) = 255, 255, 255;
dst.at<Vec3b>(437, 50) = 255, 255, 255;
dst.at<Vec3b>(438, 50) = 255, 255, 255;
dst.at<Vec3b>(439, 50) = 0, 0, 0;
dst.at<Vec3b>(440, 50) = 0, 0, 0;

i get a different color printed on the picture.
When i check the printed pixels with gimp,
(255, 255, 255) is now: (255, 110, 110)
and (0, 0, 0) is: (0, 210, 210).

The original color of these pixels is in RGB (210, 210, 210).

I just don't get what i'm doing wrong. Appreciate your help! 🙂

Best Answer

When you're assigning a new value with:

dst.at<Vec3b>(436, 50) = 255, 255, 255;

you're using C++ comma operator.

To assign a new value, you should use:

dst.at<Vec3b>(436, 50) = Vec3b(255, 255, 255);

Also note that you're reading out of bounds when i==10:

RGBscan.colorrgb[i] = ... // out of bounds when i == 10