Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

(1) you do this on the color channels, not on a grayscale version, like:

hb = cv2.calcHist([image[:,:,0]], [0], None, [256], [0,255])
hg = cv2.calcHist([image[:,:,1]], [0], None, [256], [0,255])
hr = cv2.calcHist([image[:,:,2]], [0], None, [256], [0,255])

(2) from here

Let the total mass of the histogram be M. Accumulate the mass in the bins, starting from index zero, until you pass 0.01 M. You get an index Q01. Decumulate the mass in the bins, starting from the maximum index, until you pass 0.99 M. You get an index Q99. These indexes are the so-called first and last percentiles. The contrast is estimated as Q99-Q01.

so, let's do that:

H = hb + hg + hr
M = np.sum(H)
H /= M;

# from left:
m0=0
for q0 in range(0, 255, 1):
   m0 += H[q0]
   if m0 >= 0.1:
      break
# from the right:
m1=1.0
for q1 in range(255, 0, -1):
   m1 -= H[q1]
   if m1 <= 0.99:
      break

qct = (q1-q0)

(1) you do this on the color channels, not on a grayscale version, like:

hb = cv2.calcHist([image[:,:,0]], [0], None, [256], [0,255])
hg = cv2.calcHist([image[:,:,1]], [0], None, [256], [0,255])
hr = cv2.calcHist([image[:,:,2]], [0], None, [256], [0,255])

(2) from here

Let the total mass of the histogram be M. Accumulate the mass in the bins, starting from index zero, until you pass 0.01 M. You get an index Q01. Decumulate the mass in the bins, starting from the maximum index, until you pass 0.99 M. You get an index Q99. These indexes are the so-called first and last percentiles. The contrast is estimated as Q99-Q01.

so, let's do that:

# normalized sum
H = hb + hg + hr
M = np.sum(H)
H /= M;

# from the left:
m0=0
for q0 in range(0, 255, 1):
   m0 += H[q0]
   if m0 >= 0.1:
0.01:
      break
# from the right:
m1=1.0
for q1 in range(255, 0, -1):
   m1 -= H[q1]
   if m1 <= 0.99:
      break

qct = (q1-q0)