0

I have an image of a Jacaranda tree (jpeg) and i want to count the number of 'purple' pixels in this image using matlab.

1 Answers1

3

The hard thing about this question is -> How do you define purple?

Before answering that lets introduce HSV color space. In this color space, unlike in RGB, there is a single value that describes colors, while the others are saturation and "light" (see wikipedia or my answer here for more info about HSV). This means that just by looking at H we can know which color something is. The first thing we will do is put our image in HSV.

hsv_image = rgb2hsv(rgb_image)

Now we need to know which values of H are "purple". We can see a rough approximation of where purple is in this SO answer, where it is identified in around 300 degrees of Hue. If, instead, we look a bit deeper to the wikipedia page of Shades of purple*. There, we can see that the shades of purple listed are about 270-320 Hue values.

If we like this approximation, then we can just select the pixels of the image with that amount of H by

H=hsv_image(:,:,1);

and then just select the ones that are in the desired range

purple=H<320 && H> 270;

Or in just one go

purple=hsv_image(:,:,1)<320 && hsv_image(:,:,1)> 270;

Then we just need to count.

N_purple=sum(purple(:))

*If you were asking for gray the wikipedia page would be confusing

Ander Biguri
  • 35,140
  • 11
  • 74
  • 120