Individual color values of a pixel?

Post

Posted
Rating:
#1 (In Topic #161)
Trainee
 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
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Regular
stevedee is in the usergroup ‘Regular’

RichardB said

…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:-

Code (gambas)

  1.  
  2. hImage = Image.Load(strFilePath)  'load photo
  3. hHisto = hImage.Histogram()
  4. For index = 0 To 255
  5.     lngRedPixel = hHisto[Image.Red, index]
  6.     lngGreenPixel = hHisto[Image.Green, index]
  7.     lngBluePixel = hHisto[Image.Blue, index]
  8.  
  9.  

Take a look at my blog post: Captain Bodgit: Gambas: Improving the PhotoViewer

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

<COLOR color="#FF0000">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.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Regular
stevedee is in the usergroup ‘Regular’
OK, Take 2.

I created a new GUI project and added a button, a text box and 3 labels to the form.

Code (gambas)

  1. Public Sub btnGetData_Click()
  2. Dim hImage As Image
  3. Dim Pixels As Integer[]
  4. Dim intPixie As Integer
  5.  
  6.  
  7.   hImage = Image.Load(txtImageFile.Text)  'the test image
  8.   Pixels = hImage.Pixels
  9.   intPixie = Pixels[1]
  10.   Label1.Text = intPixie
  11.   Label1.Foreground = intPixie
  12.   intPixie = Pixels[5]
  13.   Label2.Text = intPixie
  14.   Label2.Foreground = intPixie
  15.   intPixie = Pixels[12]
  16.   Label3.Text = intPixie
  17.   Label3.Foreground = intPixie
  18.  
  19.  

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:

Image

(Click to enlarge)


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] ?
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Guru
cogier is in the usergroup ‘Guru’
I had a go at this as well and came up with the attached program

Attachment

Image

(Click to enlarge)


Let us know how you get on.
Online now: Yes Back to the top

Post

Posted
Rating:
#5
Avatar
Regular
stevedee is in the usergroup ‘Regular’
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 /doc/imageconv - Gambas Documentation

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).
Online now: No Back to the top

Post

Posted
Rating:
#6
Trainee
 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
Online now: No Back to the top

Post

Posted
Rating:
#7
Regular
vuott is in the usergroup ‘Regular’
…a very little and simple code:

Code (gambas)

  1. Public Sub Button1_Click()
  2.  
  3.   Dim im As Image
  4.   Dim px, n As Integer
  5.  
  6.    im = Image.Load("/path/of/my/file/image")
  7.  
  8.    For n = 0 To im.Pixels.Max
  9.      px = im.Pixels[n]
  10.      Print n, "Pixel: "; Hex(px, 6)
  11.      Print Null, Hex(Shr(px, 16) And &FF, 2), Hex(Shr(px, 8) And &FF, 2), Hex(px And &FF, 2)
  12.      Print
  13.     Next
  14.    

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#8
Regular
vuott is in the usergroup ‘Regular’
Member of Italian Gambas forum, Gianluigi, suggests this other way (look at attached file)

Attachment

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#9
Trainee
 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
Online now: No Back to the top
1 guest and 0 members have just viewed this.