bug in autocrop_components?

Hello,

I am trying to use autocrop_components to extract into single images the already computed connected components present in an image (segmentation result), as shown below (colored with map 2,2). It seems that adjacent components are not split, like the two in the second panel - using either low or high connectivity gives the same result. Is this the expected behavior?

Thanks for any advice.

What I expected autocrop_components would do is below.

autocrop_comp_correct

As far as I know, the expected behaviour is the result. Connected segments remain connected.

This is based on memory, which can be faulty. Do not remember if there is an interactive version.

I believe connectivity is for negotiating at the pixel-level; e.g., thin diagonal line.

Thanks! I was thinking differently, as in extracting the already computed connected components into single images, a task commonly done in image analysis. Probably would be something called autocrop_labels

Solved using the code below:

gmic foo.png -repeat ‘{iM}’ +eq’[0]’ ‘{$>+1}’ -mul. ‘[0]’ -done -rm’[0]’ -autocrop -remove_empty

Maybe not efficient when ‘iM’ is very large, but it does the job.

That would only work if rounded and in greyscale, so label is better than map in this respect.

1 Like

Another (fast) possibility to consider :

$ gmic labels.png +colormap 0 +index.. . 100%,100%,100%,{iM} eval.. "i?(i(#-1,x,y,z,i-1) = i)" s. c map[3--1] [1] rm[0-2]

Hmm, this code gives me a idea to shorten all of this into a single command code or maybe even modify rep_colors_freqstat to do 5 single things (it has 4 things). You can literally make a new command using colormap code as well to do this. Lots of ideas thanks to your code.

I completely forgot that what @alosca was looking for was actually already implemented as command split_colors : G'MIC - GREYC's Magic for Image Computing: A Full-Featured Open-Source Framework for Image Processing - Reference Documentation - split_colors

So,

$ gmic labels.png split_colors 0

should do the work !

Silly us. I was going with the route you all were taking when a simpler solution is already readily available.

It is just that! It is hard to keep track of all operations already implemented :slight_smile:

For completeness, my case would be the following to preserve the original labels of all connected components and cropped:

gmic labels.png split_colors 0 rm'[0]' remove_opacity autocrop

Thanks all!

What incredible thing are you up to? Please share if it is not secret service. :stuck_out_tongue:

Thanks for the interest. I wish it was that incredible :slight_smile:

This is related to image segmentation where each connected component has a distinct label. I want to do some statistics on each labeled region, e.g. its size (width, height, diameter), area, etc. and also be able to solve a PDE on each. Separating all segmented regions just makes downstream processing easier. The regionprops function in Matlab and in python’s scikit-image are handy for that, though it doesn’t help solving the PDE.

In the solution above, I assume split_colors adds an alpha channel to result images so colors can be easily displayed within a transparent background, not something I need but I am glad remove_opacity is there! Maybe in the future we can have a flag on split_colors to disable the alpha channel (?).

Not sure about this idea. Because split_colors considers also pure black as a valid color to split, and in this case, only the alpha channel is actually useful (the rest is 0 everywhere).

I see the problem when black needs to be also considered. I was not considering pure black as of interest in my case, in a similar manner as in the other commands area_fg and label_fg. The alpha channel is only useful for the black pixels, which generates a single image out of possibly hundreds in my case.

My current hack to keep background represented by pure black is (couldn’t find yet a case it breaks)

gmic labels.png replace 0,'{iM+1}' split_colors 0 remove_opacity autocrop 0

and in case pure black is not of interest, as before,

gmic labels.png split_colors 0 remove_opacity autocrop 0 remove_empty