I am trying to edit the pixels of an image with opencv.
I used this code:
#include<iostream>
#include<opencv2/opencv.hpp>
#include<stdint.h>
using namespace std;
uchar* array123()
{
uchar arr[3] = { 123, 123, 123 };
return arr;
};
int main()
{
cv::Mat img = cv::imread("img.jpg");
uchar* n;
for (int x = 0; x < img.cols; x++)
{
for (int y = 0; y < img.rows; y++)
{
n = array123();
cout << "n " << (int)*n << " " << (int)*(n + 1) << " " << (int)*(n + 2) << endl;
cv::Vec3b& color = img.at<cv::Vec3b>(y, x);
cout << "Before change " << (int)color[0] << " " << (int)color[1] << " " << (int)color[2] << endl;
color[0] = *n;
color[1] = *(n + 1);
color[2] = *(n + 2);
img.at<cv::Vec3b>(y, x) = color;
cv::Vec3b color2 = img.at<cv::Vec3b>(y, x);
cout << "After change " << (int)color2[0] << " " << (int)color2[1] << " " << (int)color2[2] << endl;
}
}
}
The console will log something like this:
n 123 123 123
Before change 153 123 26
After change 204 204 204
... repeating with After change always being 204 204 204 ...
And if I save the image is just a gray image with 204, 204, 204 RGB values
Does anyone know why this happens?