0

I've made a program that creates images using OpenCL and in the OpenCL code I have to access the underlaying data of the opencv-image and modify it directly but I don't know how the data is arranged internally.

I'm currently using CV_8U because the representation is really simple 0 black 255 white and everything in between but I want to add color and I don't know what format to use.

This is how I currently modify the image A[y*width + x] = 255;.

Fransebas
  • 321
  • 4
  • 10

3 Answers3

0

Since your A[y*width + x] = 255; works fine, then the underlaying image data A must be a 1D pixel array of size width * height, each element is a cv_8u (8 bit unsigned int).

Guang
  • 355
  • 1
  • 8
  • But I want to add color and I don’t know which other format to use – Fransebas Jun 30 '21 at 17:29
  • 2
    That was not clear in your original question. Since you are using CV_8U, then the pixel format is 8 bit unsigned int, just simple gray scale. There are CV_8uc3 etc, where the 'c3' specifies there are three channels, usually RGB. Then your 1D array should be 3 * width * height, ```A[y*width*3 + x * 3 + 0]``` will be the R, ```A[y*width*3 + x * 3 + 1]``` the G, ```A[y*width*3 + x * 3 + 1]``` the B. There might be ways to make a 2D or even 3D array for this, but in the end it is usually just a big chunk of linear memory. – Guang Jun 30 '21 at 17:35
  • Thanks that’s just what I needed the C3 thing is the thing I needed, very intuitive now that I see it. Just one thing If I understand correctly each color is a byte from 0-255 or it has 3 bytes per pixel? I think is the later. – Fransebas Jun 30 '21 at 17:40
  • 1
    Asking new questions or thing that were not mentioned. [How to Ask](https://stackoverflow.com/help/how-to-ask). – José D. Tascón-Vidarte Jun 30 '21 at 17:41
  • 1
    https://stackoverflow.com/questions/27183946/what-does-cv-8uc3-and-the-other-types-stand-for-in-opencv – Guang Jun 30 '21 at 17:44
0

The color values of a pixel, in the case of OpenCV, will be arranged B G R in memory. RGB order would be more common but OpenCV likes them BGR.

Your data ought to be CV_8UC3, which is the case if you use imread or VideoCapture. if it isn't that, the following information needs to be interpreted accordingly.

Your array index math needs to expand to account for the data's layout:

[(y*width + x)*3 + channel]

3 because 3 channels. channel is 0..2, x and y as you expect.

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
0

As mentioned in other answers, you'd need to convert this single-channel image to a 3-channel image to have color. The 3 channels are Blue, Green, Red (BGR).

OpenCV has a method that does just this, cv2.cvtColor(), this method takes an input image (in this case the single channel image that you have), and a conversion code (see here for more).

So the code would be like the following:

color_image = cv2.cvtColor(source_image, cv2.COLOR_GRAY2BGR)

Then you can modify the color by accessing each of the color channels, e.g.

color_image[y, x, 0] = 255 # this changes the first channel (Blue)
UdonN00dle
  • 723
  • 6
  • 28
  • OP states that they use OpenCL and imply that they write their own OpenCL kernel. OpenCL doesn't support that indexing syntax. – Christoph Rackwitz Jul 01 '21 at 16:23
  • 1
    @ChristophRackwitz you're probably right. This answer is to help point out what to do and provide a general direction. Since there's an 'opencv' tag on the question, I went ahead with OpenCV syntax. Have a nice day. – UdonN00dle Jul 02 '21 at 09:41