Re: Finding Color of Specific Pixel in Loaded Image



<IWP506@xxxxxxxxx> wrote in message news:1136085577.996835.231780@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

OK -- I have the return value.  Say, for instance, it is "16711422".
Some documentation I read says this is an RGB value...  Are they
saying red is 167 green is 114 and blue is 22?

The individual colours (red, green and blue) are contained in the lower three bytes of the returned value (which is itself a Long containing four bytes). You can "see" the individual byte data by viewing the Long in hex format. For example, the value 16711422 is FEFEFE when viewed in hex format, indicating that in this specific case all three colours (RGB) have the value &HFE (which is of course 254 in decimal). The valid range for each byte is 0 to 255, with 0,0,0, being black and 255,255,255 being white.


So, you can split the returned value into the three RGB values by converting it to a Hex string and then manipulating that string. However, that is a fairly slow way of doing it and a much faster way is to dump the returned data from the memory space of the Long directly into the memory space of a small Byte array. There are a number of ways of doing this. You could use the CopyMem API or alternatively the much safer VB Lset method. Here is an example of the latter:

Option Explicit
Private Type longcolor
   Value As Long
End Type
Private Type RGBcolors
   Red As Byte
   Green As Byte
   Blue As Byte
   Dummy As Byte
End Type
Private myLong As longcolor
Private myBytes As RGBcolors

Private Sub Command1_Click()
myLong.Value = Picture1.Point(10, 10)
LSet myBytes = myLong
' the following is just to show that it works
Print myBytes.Red
Print myBytes.Green
Print myBytes.Blue
End Sub

The above code performs the conversion very quickly. If you want to do it faster then you need to speed up the slow part of the whole process (which is reading the Long value from the pixel in the first place). You are currently using the VB Point method (which is the slowest way of doing it). A faster way would be to use the alternative API GetPixel routine instead. An even faster way (very much faster!) is to get the entire block of pixels for the complete image into a suitable VB array in one go and then you can read and manipilate the individual pixel colours very quickly. In fact, there is an even faster way than that but it needs quite a lot more code to do it and you need to take account of the different possible pixel data formats. For the time being I would suggest that you use the above method until you are happy that you understand the process and then post again if you want help with the much faster "get the whole lot into a suitable VB array in one go" method that I have mentioned.

Mike


.



Relevant Pages

  • Re: Open RGB Explicit Little endian Dicom Image
    ... I am trying to open RGB Explicit Little endian Dicom Image.Which Tag ... somehow think the transfer syntax conveys some parsing of multiple ... - samples per pixel to set up the correct JPEG header ... One, three, and four image planes are defined. ...
    (comp.protocols.dicom)
  • Re: interpolation for a color image?
    ... produce a result that is the same as box interpolation of RGB. ... interpolation errors on reparate RGB channels may produce a new pixel value slightly ... store the RGB ratios for each pixel, i.e., keep the original image, convert ...
    (sci.image.processing)
  • Re: Contious optical receiver
    ... What differ a prism from a RGB mask? ... So you say the blending a CCD record is not physical? ... The sensor array has a color filter mask over it so each "pixel" is ...
    (sci.physics)
  • Re: Get RGB settings
    ... RGB as well, so that will be my next effort. ... Private Type RGB_Wrapper ... Sub RGB_Settings ... LSet Colors = Color ...
    (microsoft.public.excel.programming)
  • Re: image processing
    ... 4.Calculate the distance in RGB space for each pixel from each of the ... Calculate the various area fractions for the 10 different minerals ... HSV color space and calculating delta E color difference isn't much ...
    (comp.soft-sys.matlab)