-1

I am trying to build a machine learning algorithm where I need to convert pictures to their binaries. I am using Pillow library to get the data from images. Since the performance of the algorithm is not great, I need extra parameters to thoroughly train the network and one of the extra parameters might be hue.

So is there a method in Python that gives me hue value of an image?

Ugur
  • 44
  • 7
  • 1
    have a look at https://stackoverflow.com/questions/55405639/extract-hue-channel-from-hsv-image-in-pillow – Jan Wilamowski Jan 06 '22 at 06:52
  • 1
    Your question is not very clear. What is an image's *"binary"*, please? You say you want the *"hue value"*, of an image - but every pixel will have its own hue value, so do you mean you want the average hue across all pixels, or the individual hues of all pixels, or something else? – Mark Setchell Jan 06 '22 at 07:50
  • Thank you for the information you provided. Sorry for giving little detail about gathering hue values, this is because my knowledge on the subject is limited. A binary image (in this case) is an image that all pixels can have the value of either 0 or 1. I want the average hue across all pixels. – Ugur Jan 06 '22 at 07:56

1 Answers1

0

I am not sure what you are really trying to do, as Christoph says in the comments, but you can find the mean hue of all the pixels in an image with PIL like this:

from PIL import Image, ImageStat

# Load image, convert to HSV and split the channels, retain H, discard S and V
H, _, _ = Image.open('image.png').convert('RGB').convert('HSV').split()

# Print the mean Hue across all pixels
print(ImageStat.Stat(H).mean)

Note that I converted to RGB first to avoid problems that may arise if you try this with palette images, CMYK images, images with transparency and greyscale images. See here.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • if a picture was entirely red with some noise, hues of half the pixels would be near 0... and the other half near 360 (hues on a circle, thanks to human eyes). the mean of that would be 180, which is cyan. -- I find the whole question questionable. the image ought to be averaged in a sensible color space like RGB/BGR/YUV, and a hue could be calculated from that average. – Christoph Rackwitz Jan 06 '22 at 16:25
  • @ChristophRackwitz Yes, I agree. It depends to an extent on what the OP is trying to do... there are conceivable applications for this approach... but it would be better if OP explained the specific use case. – Mark Setchell Jan 06 '22 at 16:35
  • The main reason I asked the question was to figure out if mean hue across all pixels of an image is a unique identifier of that image. If that is the case then I would be able to define my input data further and hopefully get better results in terms of binarizing the image. Images I try to binarize are satellite images that contain clouds. The model output should be a binary image where pixel values ​​of 0 in the places where the clouds are and 1 in the other places. – Ugur Jan 07 '22 at 06:13
  • Without seeing your data, I would imagine clouds are more likely to show up as unsaturated (i.e. uncoloured) and high valued (i.e. bright) in your images. So you could convert to HSV and look for pixels with low S and high V - as per the first 5 lines of code here... https://stackoverflow.com/a/70534060/2836621 – Mark Setchell Jan 07 '22 at 08:18
  • Yes clouds appear high valued (bright) in images I use. Thank you for your suggestion, I will look into it. – Ugur Jan 07 '22 at 08:33