0

I want to get color histograms of RGB images. It should be a global histogram of the hue values in HSV color space, resulting in 256 features per image.

My code in skimage is as follows (I'm not sure if this code is correct):

img_resize = transform.resize(image, (30,30))
rgb_to_hsv = color.rgb2hsv(img_resize)
histogram, edges = np.histogram(rgb_to_hsv, bins=260)

I am very unsure whether this is the right way to create color histograms of the hue values.

When I print out the histogram print(rgb_to_hsv), I get completely different results than with the following code in OpenCV:

img_resize = cv2.resize(image, (30,30))
hsv_opencv = cv2.cvtColor(img_resize, cv2.COLOR_BGR2HSV)
hist_cv = cv2.calcHist([hsv_opencv], [0], None, [256], [0,256])
hist_cv = hist_cv.flatten()

Ultimately, I would like to combine the color histogram created with skimage with hog features, which were also extracted by using skimage.

Can someone give me a feedback whether my code in skimage is correct and whether I can directly concatenate the color histogram with the hog features?

Update

from skimage import io
from skimage.feature import hog
from skimage import data, exposure
from skimage import color, transform

image = data.astronaut()
img_res = transform.resize(image, (30,30))
img_hsv = color.rgb2hsv(img_res)
hue = img_hsv[:,:,0]
hist, edges = np.histogram(hue, bins=260)
print(len(hist))
print(hist)

Output:

[114 6 7 7 10 9 10 16 26 38 73 48 31 26 19 21 18 12 24 15 14 20 15 10 15 7 7 12 18 13 5 3 8 1 4 1 0 1 2 1 1 2 0 5 0 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 2 0 0 0 0 0 0 1 1 0 0 1 1 0 4 2 1 2 2 2 4 4 4 3 5 4 3 3 3 1 2 0 0 1 4 2 0 0 0 2 0 0 0 1 0 2 0 0 0 2 4 1 1 1 1 4 0 2 2 1 0 1 3 2 1 2 2 1 1 5 4 1 2 2 3 2 1 1 2 0 3 1 2 2 6 5 7 9 5 2 4 6 11 14]

img_res2 = cv2.resize(image, (30,30))
hsv_opencv = cv2.cvtColor(img_res2, cv2.COLOR_BGR2HSV)
hist_cv = cv2.calcHist([hsv_opencv], [0], None, [256], [0,256])
hist_cv = hist_cv.flatten()
print(hist_cv)

Output:

[109. 0. 0. 0. 1. 1. 0. 0. 0. 0. 2. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 6. 0. 4. 1. 1. 3. 4. 3. 7. 9. 18. 27. 12. 13. 15. 31. 18. 26. 25. 31. 26. 39. 69. 90. 29. 24. 16. 13. 5. 4. 45. 10. 6. 6. 8. 11. 9. 0. 3. 2. 6. 2. 3. 2. 0. 7. 2. 2. 4. 2. 3. 0. 0. 3. 2. 2. 2. 2. 3. 0. 6. 0. 0. 1. 0. 2. 1. 0. 1. 0. 2. 1. 2. 1. 3. 5. 5. 4. 5. 5. 7. 4. 3. 2. 2. 5. 2. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

Update I used the skimage version 14.1 for extracting HOG features with the following code:

from skimage import feature
img = transform.resize(image, (30,30))
hog = feature.hog(img, orientations=9, pixels_per_cell=(4,4), cells_per_block=(2,2), transform_sqrt=True, visualize=False)
print(hog)

Output:

[0.1264151 0.06452642 0.01920705 ... 0.02537039 0.03174189 0.01708564]

Code for hue-histogram

from skimage import exposure
img = transform.resize(image, (30,30))
hist, bins = exposure.histogram(img, nbins=256)
print(hist)

Output:

[309 49 19 21 12 13 16 12 9 14 17 12 12 6 9 7 8 8 8 12 5 9 5 13 7 11 8 11 7 7 8 6 4 11 5 5 4 5 4 7 5 6 9 6 9 6 4 7 8 10 6 7 10 8 6 6 11 10 13 5 5 6 7 11 6 5 12 5 8 5 6 7 11 3 5 11 14 13 9 5 11 5 5 15 7 11 8 9 11 11 10 8 10 9 6 8 8 6 8 9 10 6 5 6 10 11 10 7 9 10 6 8 6 12 5 5 10 8 11 15 9 12 8 4 4 7 3 7 9 5 5 6 2 3 6 8 6 7 1 8 10 8 4 5 3 4 4 5 7 3 11 6 9 8 6 6 8 13 6 9 10 9 12 11 10 13 10 9 8 9 20 13 13 16 9 12 15 17 13 18 12 14 14 17 11 14 13 23 15 16 17 17 17 20 10 18 17 11 13 9 24 24 20 10 23 17 15 14 14 10 16 14 15 18 16 12 9 13 15 18 10 7 7 4 4 9 11 8 13 11 9 7 8 5 6 7 6 10 3 7 7 5 3 2 3 1 3 1 3 3 1 3 5 1 2 22]

May concate the two features like follows:

#normalize histogram as possible from skimage version 15.x
hist = hist/np.sum(hist)    

#concatenate 
feat_concat = np.concatenate((hog, hist), axis=1)

Would this be correct?

Code Now
  • 711
  • 2
  • 9
  • 20
  • Please give some indicative input and corresponding expected output. Your question asks specifically about a Hue histogram, yet your code doesn't appear to separate Hue from Saturation and Value. – Mark Setchell Jan 03 '20 at 19:31
  • @Mark Setchell I have added a sample output for the astronaut image above, which shows the different outputs – Code Now Jan 03 '20 at 21:21
  • Two things... 1) how did you open the image for OpenCV? If it is just a continuation of the skimage stuff, it is in RGB, so your conversion to HSV should be RGB2HSV not BGR2HSV 2) OpenCV scales Hue on the range 0..180 to fit in a uint8 whereas skimage scales on the range 0..1 or 0..360, so they are not directly comparable without scaling. – Mark Setchell Jan 03 '20 at 21:32
  • @Mark Setchell 1) oh, yes now i realize that it is just a continuation of skimage 2) ok, but the first code (skimage) should be correct for creating a hue value histogram? If the code is correct, I would have to normalize the histogram before concatenating the histograms with the hog features, because the hog features have values approx. between 0 and 1. – Code Now Jan 03 '20 at 22:30
  • Not sure at the moment. Maybe update your question with the latest and greatest code and results and we can see from there... :-) – Mark Setchell Jan 03 '20 at 22:33
  • @Mark Setchell Have attached an update. Would this approch be correct? – Code Now Jan 03 '20 at 23:49
  • Why are you using a different number of bins for the two libraries, 260 vs 256? The usefulness of concatenating different feature vectors is not a programming question and is separate (and thus less appropriate for SO) from your original question of how to get two libraries to provide the same output. There is a paper you should check out that discusses & evaluates many different ways to encode color information with image features: [Evaluation of Color Descriptors for Object and Scene Recognition, by van de Sande, Gevers, & Snoek](https://www.koen.me/research/pub/vandesande-cvpr2008.pdf) – alkasm Jan 04 '20 at 00:26
  • That was a typo, I meant 256. Thank you for the article. – Code Now Jan 04 '20 at 00:43
  • As long as you scale the values from OpenCV and scikit image hue channels the same, you should get the same results. Try using floating point images; the hue in OpenCV will be in [0, 360] as stated [in the docs](https://docs.opencv.org/trunk/de/d25/imgproc_color_conversions.html#color_convert_rgb_hsv) so you can divide by 360 to get hue in both as [0, 1]. – alkasm Jan 04 '20 at 02:25

0 Answers0