Home > Software design >  Keep bins between 5% and 95% of a histogram in OpenCV/Python
Keep bins between 5% and 95% of a histogram in OpenCV/Python

Time:05-13

I am intending to skip some histograms of an image in the extreme points of the distribution of any gray image. The extreme points to the left are represented by all the histograms lying in the region of 5%, and to the right are represented by all histograms in a region above 95% of the whole distribution Here are some codes to where I ended

image = cv2.imread('right.' str(i) '.png')
#print(image)
hist = cv2.calcHist([image], [0], None, [256], [0,256])
lower = round(0.05*len(hist))
upper = round(0.95*len(hist))
lower_hist_list = hist[0:lower]
upper_hist_list = hist[upper:len(hist)]
lower_hist, upper_hist
remaining_region =hist[index_above_lower : index_before_upper]

what I want is the histograms between the lower and upper boundaries 5%<=IMAGE<=95%?

CodePudding user response:

Based on my understanding of your question; you would like to avoid pixel intensities that are on the tail ends of your histogram. Pixels below 5% and above 95% of the distribution must be clipped.

First, obtain histogram of your grayscale image with 256 bins:

hist = cv2.calcHist([img],[0],None,[256],[0,256])

enter image description here

hist stores the number of pixels having values each between [0 - 255].

  • 5% of 256 ~ 12
  • 95% of 256 ~ 243

Based on your criteria, we need to keep pixel values in the range [12 - 243]. We can use enter image description here

Looking at the plot above:

  • pixel values below 12 have been assigned the value 12
  • pixel values above 243 have been assigned the value 243

Hence you can see the spike at both these values in the plot.

  • Related