I am plotting a histogram in matplotlib using plt.hist, where density = True gives a normalized histogram with total area under histogram =1.
However, I moved to OpenCV in python because it can handle masks for images. I tried using cv2.normalize, but it seems to give different results than plt.hist(density=True). Is there any other function in OpenCV that is similar to plt.hist(… density=True) ?
Thanks in advance !!
there is no such thing builtin, but it’s not difficult to
normalize a histogram, so that the total area under it == 1:
s = sum(hist)
base = hist_range / num_bins
area = s * base
normalized_hist = hist / area
now, this will leave most histogram bins / values <1, so, to plot anything visible, you will need to multiply it again with an arbitrary scale factor, like 100
I would recommend multiplying with the number of bins (assuming they’re all the same size). that’ll scale the histogram to have proportional area.