How to segment high contrast object from the background using python?

Viewed 149

I am facing an exciting task, and I am stuck. So I have images that contain high and low-intensity objects that have to be separated from the background. I provided an example image below so you can have some idea.

Example image

Most of the segmentation methods I have seen in OpenCV start with a thresholding step like Otsu's Thresholding. However, this is not applicable in my case because it would filter out all the low-intensity objects. Is there any obvious way to tackle this kind of problem?

We can assume that an object never touches the image boundary, and it will always have high contrast with the background and have colours close to black and white.

Pixels in an object have very similar colour but they are not identical.

1 Answers

If the object is black and you know it's value won't be greater than, say, 5(on 0..255 scale), you may use this value as a threshold

black_obj_mask = image < 5

Same to the white object. If white objects pixel values are always greater then 250, you may use it

white_obj_mask = image > 250 

Edited If all the pixels of an object are the same color, you can use something like this. The code sample is from here

import numpy as np
from scipy import stats

a = np.array([[1, 3, 4, 2, 2, 7],
              [5, 2, 2, 1, 4, 1],
              [3, 3, 2, 2, 1, 1]])

m = stats.mode(a.flatten())[0]
mask = a==m

Related