5

I am using pi with raspbian wheezy and python 2.7. I have an image in jpg format which I can read and convert to array. But I want to convert the image from RGB color model to HSI color model and then plot a histogram for the hue saturation and intensity parameters of the HSI image. I have tried to plot but I'm not sure about the accuracy of my results. I have included the code. Can anyone tell me how I can verify the output of my program.

import scipy
from scipy import ndimage
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import numpy as np
from scipy import misc
import scipy.misc

img = scipy.misc.imread("/home/subhradeep/Desktop/ref.jpg")
array=np.asarray(img)
arr=(array.astype(float))/255.0
img_hsv = colors.rgb_to_hsv(arr[...,:3])

lu1=img_hsv[...,0].flatten()
plt.subplot(1,3,1)
plt.hist(lu1*360,bins=360,range=(0.0,360.0),histtype='stepfilled', color='r', label='Hue')
plt.title("Hue")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.legend()

lu2=img_hsv[...,1].flatten()
plt.subplot(1,3,2)                  
plt.hist(lu2,bins=100,range=(0.0,1.0),histtype='stepfilled', color='g', label='Saturation')
plt.title("Saturation")   
plt.xlabel("Value")    
plt.ylabel("Frequency")
plt.legend()

lu3=img_hsv[...,2].flatten()
plt.subplot(1,3,3)                  
plt.hist(lu3*255,bins=256,range=(0.0,255.0),histtype='stepfilled', color='b', label='Intesity')
plt.title("Intensity")   
plt.xlabel("Value")    
plt.ylabel("Frequency")
plt.legend()
plt.show()

Here is a sample of my output histogram

4
  • Photoshop does histograms. Take the image, do the same in Photoshop and compare. Possibly GIMP also does it and or other free software.
    – Piotr Kula
    Nov 7, 2013 at 13:15
  • @ppumkin But as far as I know Photoshop does histograms but in the RGB colorspace not in HSI colorspace Nov 7, 2013 at 13:37
  • Ohh right there is a Luminosity histogram but it does not show Hue, Saturation and Intensity. It just seems to show it all combined. This is new to me though and quite interesting.Although creating a new image with CMYK or LAB show a different histogram altogether. I wonder if this will help at all? adobepress.com/articles/article.asp?p=1930490
    – Piotr Kula
    Nov 7, 2013 at 14:53
  • This question appears to be off-topic because it is about signal processing
    – finnw
    Dec 18, 2013 at 15:47

1 Answer 1

6

Here is the complete edited code

import scipy
from scipy import ndimage
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import numpy as np
from scipy import misc
import scipy.misc

img = scipy.misc.imread("/home/subhradeep/Desktop/testc.jpg")
array=np.asarray(img)
arr=(array.astype(float))/255.0
img_hsv = colors.rgb_to_hsv(arr[...,:3])

lu1=img_hsv[...,0].flatten()
plt.subplot(1,3,1)
plt.hist(lu1*360,bins=360,range=(0.0,360.0),histtype='stepfilled', color='r', label='Hue')
plt.title("Hue")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.legend()

lu2=img_hsv[...,1].flatten()
plt.subplot(1,3,2)                  
plt.hist(lu2,bins=100,range=(0.0,1.0),histtype='stepfilled', color='g', label='Saturation')
plt.title("Saturation")   
plt.xlabel("Value")    
plt.ylabel("Frequency")
plt.legend()

lu3=img_hsv[...,2].flatten()
plt.subplot(1,3,3)                  
plt.hist(lu3*255,bins=256,range=(0.0,255.0),histtype='stepfilled', color='b', label='Intesity')
plt.title("Intensity")   
plt.xlabel("Value")    
plt.ylabel("Frequency")
plt.legend()
plt.show()

Verified results using Matlab

 Hue

 Saturation

Intensity

1
  • Awesome- You should make a GIT repo and put your code there for other people to use. This looks like some good work!
    – Piotr Kula
    Nov 9, 2013 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.