# Finding minimum in the middle of a histogram

I'm building an image processing pipeline and one step in the process requires thresholding the image based on saturation.

Example code using OpenCV-Python:

H, S, V = cv2.split(img)
mask = cv2.threshold(S, low, 255, cv2.THRESH_BINARY)


I know how to set the low value manually, if I look at the histogram:

(the red arrow indicates the value I would pick)

My question is: how can I programatically find that value?

Some non-rigorous descriptions of the problem:

• find the lower boundary of the right-most "hump"
• find the minimum value that's not at the edges
• I think I understand what you need, but it's not the rightmost local minimum (that would be the local minimum still on your "hump", at least visually. – Marcus Müller Mar 15 '18 at 19:55
• I believe the world of signal processing is not ready yet for such tricky questions. I'll award a Nobel Prize to whoever can provide an algorithm to pick the second maximum of a discrete, noisy envelope – Laurent Duval Mar 15 '18 at 20:30