And this is what the result becomes:
I want to fill the squares in the edges as well, how can I do that?
It can take some experimentation to find which of the many image transforms to use. In this case, you could try WatershedComponents.
For an example with smaller images, I start with a portion of your image, converted to a binary image via Binarize.
WatershedComponents[image]//Colorize
which gives a color image of the components
If you prefer black and white, you can use the ColorRules option. In this case, component 2 is the one with the most pixels, corresponding to the background. So
Colorize[WatershedComponents[img],ColorRules->{2->Black, _->White}]
gives
WatershedComponents
rather than MorphologicalComponents
. Colorize[MorphologicalComponents[ColorNegate@Binarize@img], ColorRules -> {2 -> Black, _ -> White}]
seems to give a similar result but maybe WatershedComponents
is more general especially since it seems one does not need to Binarize
the image.
$\endgroup$
Commented
Dec 6, 2022 at 21:16
img = Import["https://i.sstatic.net/dLeMW.jpg%5D"];
regs = ConnectedMeshComponents@ImageMesh@ColorNegate@img;
{regs // First, regs // Rest // RegionUnion}
On the full image, for the functions I tried MorphologicalComponents
seems to resemble the original image the most. However there are thin black lines on the sides. These may be removed with ImageCrop
:
Colorize[MorphologicalComponents[ColorNegate@Binarize@img],
ColorRules -> {2 -> Black, _ -> White}] // ImageCrop