4
$\begingroup$

FillingTransform[

And this is what the result becomes:enter image description here

I want to fill the squares in the edges as well, how can I do that?

$\endgroup$

3 Answers 3

5
$\begingroup$

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.

enter image description here

WatershedComponents[image]//Colorize

which gives a color image of the components

enter image description here

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

enter image description here

$\endgroup$
4
  • $\begingroup$ Nice. I have trouble understanding when to use 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
  • $\begingroup$ For this simple black & white image, multiple segmentation methods give similar results. For more general images, the choice of method depends on the image and what you're interested in grouping together. $\endgroup$
    – tad
    Commented Dec 6, 2022 at 21:42
  • $\begingroup$ I tried the Watershed method on the original image large image and the default behavior seems to introduce a lot of unwanted lines. $\endgroup$ Commented Dec 6, 2022 at 23:26
  • 1
    $\begingroup$ Perhaps try another Method option -- the default method includes boundaries between the components. Others, such as "Basins" do not. $\endgroup$
    – tad
    Commented Dec 7, 2022 at 21:20
5
$\begingroup$
img = Import["https://i.sstatic.net/dLeMW.jpg%5D"];
regs = ConnectedMeshComponents@ImageMesh@ColorNegate@img;
{regs // First, regs // Rest // RegionUnion}

enter image description here

$\endgroup$
3
  • $\begingroup$ Nice. Depending on what OP wants the filling for, they might have trouble reconverting that back to an image with the same scales due to the bug that was introduced since version 12 in the conversion between graphics objects and images. $\endgroup$ Commented Dec 7, 2022 at 1:17
  • $\begingroup$ @userrandrand Thanks,you are right. We have to rescaling the region according to the size of the original picture. $\endgroup$
    – cvgmt
    Commented Dec 7, 2022 at 1:20
  • $\begingroup$ I want to calculate the number of pixels in the grid, and I am not sure how to do that if I converted it into a mesh? $\endgroup$
    – nancy kang
    Commented Dec 9, 2022 at 16:13
2
$\begingroup$

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

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.