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?