Page 1 of 1

Individual color values of a pixel?

Posted: Friday 16th March 2018 3:21am
by RichardB
Hi folks:

How do I extract the integer values for each color of an image pixel?

If I have an image called "test_image", and can read each pixel as "test_image[x,y]", how can I extract the integer value for each of the red, green and blue channels for that pixel? (The documentation is a bit vague on this subject.)

Thanks in advance.
-RB

Re: Individual color values of a pixel?

Posted: Friday 16th March 2018 9:48am
by stevedee
RichardB wrote:
Friday 16th March 2018 3:21am
...how can I extract the integer value for each of the red, green and blue channels for that pixel?
I think you need the gb.Image.Effect component, and basically do something like:-

hImage = Image.Load(strFilePath)  'load photo
hHisto = hImage.Histogram()
For index = 0 To 255
    lngRedPixel = hHisto[Image.Red, index]
    lngGreenPixel = hHisto[Image.Green, index]
    lngBluePixel = hHisto[Image.Blue, index]

Take a look at my blog post: https://captainbodgit.blogspot.co.uk/20 ... iewer.html

Unfortunately I've lost this project. But if I can find some time, I'll put together a simply example.

EDIT: Sorry! On reflection this is a rubbish answer. This just gives the data required to build a histogram, whereas you want the actual RGB values for a given pixel.

Re: Individual color values of a pixel?

Posted: Friday 16th March 2018 12:05pm
by stevedee
OK, Take 2.

I created a new GUI project and added a button, a text box and 3 labels to the form.
Public Sub btnGetData_Click()
Dim hImage As Image
Dim Pixels As Integer[]
Dim intPixie As Integer


  hImage = Image.Load(txtImageFile.Text)  'the test image
  Pixels = hImage.Pixels
  intPixie = Pixels[1]
  Label1.Text = intPixie
  Label1.Foreground = intPixie
  intPixie = Pixels[5]
  Label2.Text = intPixie
  Label2.Foreground = intPixie
  intPixie = Pixels[12]
  Label3.Text = intPixie
  Label3.Foreground = intPixie

End
I then used The Gimp to create a new PNG image file 100 x 100 pixels. I added three 3x3 pixel coloured blocks, red, blue & green:
test.png
test.png (339 Bytes) Viewed 1119 times
This works, but note that:-
1. The red and blue colours are reversed. This reminds me that when I was using the Gambas histogram, the red & blue were the wrong way around. Benoit fixed this bug for me, but maybe the histogram was not the real problem. Perhaps the colours are incorrectly classified or something.
2. The values returned can be entered into your calculator app (Linux Galculator) and converted to Hex for comparison with the colours in Gimp.
3. The Image.Pixels method creates a simple 1D array of the image (so index is 0-9999 for a 100 x 100 image). Why doesn't it create a 2D array like image[x,y] ?

Re: Individual color values of a pixel?

Posted: Friday 16th March 2018 12:54pm
by cogier
I had a go at this as well and came up with the attached program
Image_colour.png
Image_colour.png (21.21 KiB) Viewed 1115 times
Image_colour.tar.gz
(17.73 KiB) Downloaded 52 times
Let us know how you get on.

Re: Individual color values of a pixel?

Posted: Friday 16th March 2018 6:40pm
by stevedee
I think the problem with blue & red being switched is because Gambas thinks the format of my Gimp image is RGBA (which it reads low byte to high byte) where the hex value 0000ff is interpreted as red. But my Gimp image interprets this as blue (i.e. RRGGBB, high byte to low byte). See http://gambaswiki.org/wiki/doc/imageconv

Since green is in the middle (i.e. 00ff00) it is interpreted correctly.

I think this is a Gambas bug. Does anyone have another theory?

You can see the Gambas image format by using: Image.Format (e.g. in my code example: Label4.Text = hImage.Format).

Re: Individual color values of a pixel?

Posted: Friday 16th March 2018 8:43pm
by RichardB
Thanks for everyone's help. I'm away from my computer at the moment but I'll test out your suggestions when I'm back home.

-RB

Re: Individual color values of a pixel?

Posted: Saturday 17th March 2018 6:03pm
by vuott
...a very little and simple code:
Public Sub Button1_Click()

  Dim im As Image
  Dim px, n As Integer
  
   im = Image.Load("/path/of/my/file/image")

   For n = 0 To im.Pixels.Max
     px = im.Pixels[n]
     Print n, "Pixel: "; Hex(px, 6)
     Print Null, Hex(Shr(px, 16) And &FF, 2), Hex(Shr(px, 8) And &FF, 2), Hex(px And &FF, 2)
     Print
    Next
    
End

Re: Individual color values of a pixel?

Posted: Saturday 17th March 2018 11:36pm
by vuott
Member of Italian Gambas forum, Gianluigi, suggests this other way (look at attached file)

Re: Individual color values of a pixel?

Posted: Monday 19th March 2018 10:30am
by RichardB
Thanks again to everyone. And particular thanks to Gianluigi for his solution (exactly what I was after!) and to vuott for posting it here.

-RB