1 year ago
#374028
Wolf
compareHist openCV with multiple dimensions
I am working on image colors comparison and I am struggling with understanding how cv2.compareHist works for multiple dimensions. Here is my issue: if I take every channels in compareHist I find a inverse result in distance than if I take each channel separetely.
I am working with HSV color space I first compute the histograms for my 3 images, my 3 channels independantly then I normalize:
channels = [0,1,2]
mask = None
bins = [180,256,256]#[10,64,3]
range_color = [0, 180, 0, 256, 0, 256]
hist_pic10 = cv2.calcHist([pic1_hsv], [channels[0]], mask, [bins[0]],range_color[0:2])
hist_pic11 = cv2.calcHist([pic1_hsv], [channels[1]], mask, [bins[1]],range_color[2:4])
hist_pic12 = cv2.calcHist([pic1_hsv], [channels[2]], mask, [bins[2]],range_color[4:6])
hist_pic20 = cv2.calcHist([pic2_hsv], [channels[0]], mask, [bins[0]],range_color[0:2])
hist_pic21 = cv2.calcHist([pic2_hsv], [channels[1]], mask, [bins[1]],range_color[2:4])
hist_pic22 = cv2.calcHist([pic2_hsv], [channels[2]], mask, [bins[2]],range_color[4:6])
hist_pic30 = cv2.calcHist([pic3_hsv], [channels[0]], mask, [bins[0]],range_color[0:2])
hist_pic31 = cv2.calcHist([pic3_hsv], [channels[1]], mask, [bins[1]],range_color[2:4])
hist_pic32 = cv2.calcHist([pic3_hsv], [channels[2]], mask, [bins[2]],range_color[4:6])
hist_pic10 = cv2.normalize(hist_pic10, hist_pic10)
hist_pic11 = cv2.normalize(hist_pic11, hist_pic11)
hist_pic12 = cv2.normalize(hist_pic12, hist_pic12)
hist_pic20 = cv2.normalize(hist_pic20, hist_pic20)
hist_pic21 = cv2.normalize(hist_pic21, hist_pic21)
hist_pic22 = cv2.normalize(hist_pic22, hist_pic22)
hist_pic30 = cv2.normalize(hist_pic30, hist_pic30)
hist_pic31 = cv2.normalize(hist_pic31, hist_pic31)
hist_pic32 = cv2.normalize(hist_pic32, hist_pic32)
hist_pic1 = cv2.calcHist([pic1_hsv], channels, mask, bins,range_color)
hist_pic2 = cv2.calcHist([pic2_hsv], channels, mask, bins,range_color)
hist_pic3 = cv2.calcHist([pic3_hsv], channels, mask, bins,range_color)
hist_pic1 = cv2.normalize(hist_pic1, hist_pic1)
hist_pic2 = cv2.normalize(hist_pic2, hist_pic2)
hist_pic3 = cv2.normalize(hist_pic3, hist_pic3)
Then I plot my histograms in function of the 3 channels (H,S,V):
Finally I compute the distances between every channels separately of pic1 and pic2/pic3 and I also compute the distance by taking the channel together:
methodName = 'Chi-Squared'
print("HUE distance between pic1 and pic2 :"+str(cv2.compareHist(hist_pic10, hist_pic20, OPENCV_METHODS[methodName])))
print("HUE distance between pic1 and pic3 :"+str(cv2.compareHist(hist_pic10, hist_pic30, OPENCV_METHODS[methodName])))
print()
print("SAT distance between pic1 and pic2 :"+str(cv2.compareHist(hist_pic11, hist_pic21, OPENCV_METHODS[methodName])))
print("SAT distance between pic1 and pic3 :"+str(cv2.compareHist(hist_pic11, hist_pic31, OPENCV_METHODS[methodName])))
print()
print("VALUE distance between pic1 and pic2 :"+str(cv2.compareHist(hist_pic12, hist_pic22, OPENCV_METHODS[methodName])))
print("VALUE distance between pic1 and pic2 :"+str(cv2.compareHist(hist_pic12, hist_pic32, OPENCV_METHODS[methodName])))
print()
print("3CHANNELS distance between pic1 and pic2 :"+str(cv2.compareHist(hist_pic1,hist_pic2,OPENCV_METHODS[methodName])))
print("3CHANNELS distance between pic1 and pic3 :"+str(cv2.compareHist(hist_pic1,hist_pic3,OPENCV_METHODS[methodName])))
And here are the results:
HUE distance between pic1 and pic2 :7.026184800359648
HUE distance between pic1 and pic3 :222.12612833683397
SAT distance between pic1 and pic2 :15.726820299555303
SAT distance between pic1 and pic3 :14.179351626709026
VALUE distance between pic1 and pic2 :4.185678982289476
VALUE distance between pic1 and pic2 :132.64926013642304
3CHANNELS distance between pic1 and pic2 :27.716834585962147
3CHANNELS distance between pic1 and pic3 :21.558113910664858
The distance for each channel is much lower between pic1 and pic2 but the overall distance with all channels is larger for pic1 than pic2.
Thanks in advance for anyone who read me and help me if possible.
python
opencv
histogram
hsv
image-comparison
0 Answers
Your Answer