-1

I'm trying to compute the histogram of an HSV image with openCV, with the following code:

def istogrammaHSV(image,histSize):
    hsv_planes= cv2.split(image)
    histSize= histSize  
    histRange= (0,256)
    accumulate=False
    
    h_hist = np.array(cv2.calcHist(hsv_planes, [0], None, [histSize], histRange, accumulate=accumulate),dtype='uint8' )
    s_hist = np.array(cv2.calcHist(hsv_planes, [1], None, [histSize], histRange, accumulate=accumulate),dtype='uint8' )
    v_hist = np.array(cv2.calcHist(hsv_planes, [2], None, [histSize], histRange, accumulate=accumulate),dtype='uint8' )
    
    #normalization
    hist = np.append(h_hist, s_hist, axis=0)
    hist = np.append(hist, v_hist, axis=0)
    hist = hist / np.sqrt(np.sum(hist**2))
    
    return hist

The problem is that, since the images are png, when i convert them to HSV i have a lot of black pixels (what once was the transparent background). But i don't want the black pixels to be computed in the histogram, what can i do?

Sample HSV image:

Sample HSV image

  • Use a mask. See the mask argument at https://docs.opencv.org/4.1.1/d6/dc7/group__imgproc__hist.html#ga4b2b5fd75503ff9e6844cc4dcdaed35d – fmw42 Aug 26 '22 at 15:39
  • Yes but what can i use as a mask? How do i mask the black pixels? – Mario Turco Aug 26 '22 at 15:48
  • 1
    You create a binary mask image that is white where the HSV image is not black and black where it is black. You can use cv2.threshold or Numpy to create the mask from the image – fmw42 Aug 26 '22 at 16:34

1 Answers1

-1

It looks like original image in RGBA format. Use alpha channel of your image as a mask.

Dipet
  • 1
  • 2
  • I tried this solution but i do have another problem with my code, the resulting histograms seems to have very very small value that are not even integers, is that normal? Shouldn't an histogram count the number of pixels with a certain value? Example output for a single image: array([ 9.16731338e-312, 6.87442735e+015, 1.69922164e+248, 2.23338035e+117, 2.93964545e+112, 1.30606576e+050, 1.07510469e+079, 1.00476193e+094, 2.90722966e+073,....]) – Mario Turco Aug 26 '22 at 16:25
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 31 '22 at 02:48