Removing small objects combining connected components and skeletonizing (possible bug with skeleton? 7.0.8)

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
sgin
Posts: 3
Joined: 2019-07-17T01:13:30-07:00
Authentication code: 1152

Removing small objects combining connected components and skeletonizing (possible bug with skeleton? 7.0.8)

Post by sgin »

Hi all,
I am usually able to solve problems using answers on this forum, but right now I am stuck, here is why.
I have several pictures looking like the following (which I actually drew in GIMP for this example):

Image

My goal is to obtain an image that only contains the curve in the middle, removing the scale bar and other small objects. Like so:

Image

My strategy is originally to use connected components with an area threshold that will remove all the small objects, but not the line. The difficulty is that because the scale bar is very thick in some images, it is hard to define an area threshold that will remove the scale bar in all images without removing the curve in others (using mogrify). My idea was to first skeletonize images, so that thickness problems will be out of the way, and the 'area' of the line will always be larger than that of any scale bar. Here is the code I am using now :

Code: Select all

mogrify -threshold 50% *.png
mogrify -negate *.png
mogrify -morphology Thinning:-1 Skeleton *.png
mogrify -morphology Dilate Ring:2 *.png
mogrify \
	-define connected-components:verbose=true \
	-define connected-components:area-threshold=1800 \
	-define connected-components:mean-color=true \
	-connected-components 4 \
	*.png
But Thinning appears to do strictly nothing. I have also tried on single pictures with :

Code: Select all

convert example.png -morphology Thinning:-1 Skeleton result.png
magick example.png -morphology Thinning:-1 Skeleton result.png
magick convert example.png -morphology Thinning:-1 Skeleton result.png
Which also did not work. I use ImageMagick 7.0.8-53 Q16 x86_64 on Ubuntu 16.04.5 LTS Xenial (but I may also need to run the script on other computers). Is there a known bug with thinning in this version on IM? I have read bug report for previous versions, but it appeared that they should have solved since then.
On the original version of IM that was installed on this computer (which I think was 6.8 or 6.9, default for Ubuntu 16.04), Thinning did work, but I had to upgrade because connected components were not working, so I guess downgrading would not be the solution.

Any help with this is greatly appreciated, ideas of workaround are also welcome ;)
Cheers
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Removing small objects combining connected components and skeletonizing (possible bug with skeleton? 7.0.8)

Post by snibgo »

I suggest you use "magick" for v7, not "convert" or "magick convert".

"mogrify" is for cases when we want to perform the same operations on a series of images. When there is only one image, I suggest mogrify should not be used.

We can perform multiple operations in a single "magick" or "convert" command. This is always faster than multiple commands with intermediate results written to disk.


Using the OP image, v6.9.9-50:

Code: Select all

convert exampleth.png -negate -morphology Thinning:-1 Skeleton exmpth6.png
Image
The result looks good.


With v7.0.7-28:

Code: Select all

magick exampleth.png -negate -morphology Thinning:-1 Skeleton exampth7.png
Image
This is also good.
snibgo's IM pages: im.snibgo.com
sgin
Posts: 3
Joined: 2019-07-17T01:13:30-07:00
Authentication code: 1152

Re: Removing small objects combining connected components and skeletonizing (possible bug with skeleton? 7.0.8)

Post by sgin »

Thanks for your answer !

I am aware that mogrify is supposed to be used for series of images, which is ultimately what I want to achieve. In fact I used magick and convert on a single picture to check whether I messed up the mogrify command lines or if Thinning actually did not work. I expected to get the same result as you did...

I have copied and tried the command line you used on the OP picture (still using version 7.0.8 )

Code: Select all

magick example.png -negate -morphology Thinning:-1 Skeleton result.png
and I got the following result:

Image

So it seems to me that something is not right !

Cheers
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Removing small objects combining connected components and skeletonizing (possible bug with skeleton? 7.0.8)

Post by snibgo »

Yes, that looks like a bug in your version 7.0.8-53. I've reported it as a bug: https://www.imagemagick.org/discourse-s ... =3&t=36387
snibgo's IM pages: im.snibgo.com
sgin
Posts: 3
Joined: 2019-07-17T01:13:30-07:00
Authentication code: 1152

Re: Removing small objects combining connected components and skeletonizing (possible bug with skeleton? 7.0.8)

Post by sgin »

Okay, thank you for reporting this and for your advice. For now I have downgraded to version 7.0.7-39 and both connected components and Thinning Skeleton appear to be working. I was able to do what I wanted on a series of image using mogrify. Problem 'solved'.

Cheers
Post Reply