that’s a funny typo
- your median func calculated a histogram bin index , but you need an intensity value for thresholding (the value of that bin, not the index)
- try THRESH_OTSU instead of the median
- try to visualize the intermediate results (e.g. after the threshold)
- contours need white things on black bg
- and damn, show us an image !