Binarizing images of similar background and object color?

As shown in the example picture below, if the background and the color of the object to be obtained are similar, What would be good to use as a preprocessing before proceeding with binarization?

i = Blur[img, 3];
res = ImageMultiply[i, 
   SelectComponents[FillingTransform@Binarize[i], "Area", -1]];

I used the code below, but it’s hard to separate.

What is the pre-processing method for neat binarization?

GraphicsRow[{i, res}]

Hi Arim

img // With[{r = 3}, (Closing[#, 10 r] &)@*(Opening[#1, 2 r] &)@*(GradientFilter[#, 6 r] &)] // Binarize

Robert

I think the object and background are completely binarized. Thank you for your help!

Hi, if you want the original image without background then just multiply with the binary mask.

Do you mean that if you want to show only binarized objects without background, you just need to multiply the above equation by binarized masks?

and.. How could you find the optimal numbers for r=3, 10r, 2r, 6r, etc. in the equation presented?
I want to apply it to other similar pictures.

Thanks,

img // With[{r = 3}, (Closing[#, 10 r] &)@(Opening[#1, 2 r] &)@(GradientFilter[#, 6 r] &)] // Binarize // img # &

I am afraid that it will be very difficult to process similar images with the same set of filter parameters. So far the parameters are just a result of trial & error.

Robert

If you use it in a similar image, let’s adjust the parameters! Thank you for your advice.

Hello, Is it possible for an additional binarized image to reverse the current object with a white background? (The object is black background of white)

Hi Arim

Not sure about your question, but you can use

ColorNegate[ ]

in case you just want to reverse black/white. Otherwise you need to be more specific, example, …

Robert

Oh, thank you so much! :slight_smile: