ret, mask = cv2.threshold(mask, 127, 255, 0)

def hist_lines(im, mask):
    h = np.zeros((300, 256, 3))
if len(im.shape) != 2:
    print('hist_lines applicable only for grayscale images')
im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
hist_item = cv2.calcHist([im], [0], mask, [256], [0, 255])
cv2.normalize(hist_item, hist_item, 0, 255, cv2.NORM_MINMAX)
hist = np.int32(np.around(hist_item))
for x, y in enumerate(hist):
    cv2.line(h, (x, 0), (x, y), (255, 255, 255))
y = np.flipud(h)
return y
histogram = hist_lines(img, None)
How to recognize histograms with a specific shape in opencv / python