0

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?

  • 2
    204 is hex CC which is used by some compilers to mark uninitialized or freed memory. I guess you have undefined behavior somewhere. Please extract a [mcve], you might find the problem yourself doing so. If not, at least it improves your question here. As a new user, also take the [tour] and read [ask]. – Ulrich Eckhardt Aug 14 '20 at 19:01
  • Are the uchar n pointers being pointed to uchars which have been assigned your desired values, are are they point to integer types? "n" might not be point to the exact byte values you want -you could step through a memory debugger to check – FShrike Aug 14 '20 at 19:06
  • @UlrichEckhardt Thank you for the sugestion, is this what you mean by minimal reproductible example? – Simon Lovec Aug 14 '20 at 19:12
  • 1
    Is the call to `cv:imwrite` necessary to reproduce the unexpected behavior? If not, it is not minimal. Apply this question to every single line. Also, two things you should do a bit of research on: Firstly, compilers issue warnings, use those. Secondly, C-style casts are frowned upon, because they can enforce conversions that are invalid and which would rightfully be rejected by the compiler. – Ulrich Eckhardt Aug 14 '20 at 19:37
  • 2
    On Visual Studio `0xcc` or 204 means uninitialized stack memory. [https://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations](https://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations) – drescherjm Aug 14 '20 at 19:42
  • 1
    I see `n = array123();` is undefined behavior and the cause of the `0xcc` you can not return a pointer to a local variable like this. – drescherjm Aug 14 '20 at 19:49
  • @drescherjm Thank you, just added static in front of the uchar arr[3] and it works now! – Simon Lovec Aug 14 '20 at 19:54
  • @UlrichEckhardt Thank you for the sugestions, fixed all the compiler warnings and now I know what minimal to reproduce exapmle means! – Simon Lovec Aug 14 '20 at 19:57
  • 1
    Does this answer your question? [C++ function to return array](https://stackoverflow.com/questions/10808825/c-function-to-return-array) – drescherjm Aug 14 '20 at 20:02

0 Answers0