Count amount of non-continuous blocks in an image

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?".
Peggy
Posts: 33
Joined: 2020-03-02T22:31:47-07:00
Authentication code: 1152

Re: Count amount of non-continuous blocks in an image

Post by Peggy »

Well, after some attempts I think I can't count color easily for svg image. At the begining,I need to convert svg to png, the conversion would be a probelm. No matter use IM or other methods, the result might not 100% correct. Event the png result is fine, IM still gave me a lot colors(There is only one color in SVG).

The SVG I tested was:
https://drive.google.com/file/d/1Tg2N4x ... sp=sharing

Anyway, if I set -colors 3 manually like:

Code: Select all

convert svg.png +dither -colors 3 -define connected-components:verbose=true -define connected-components:area-threshold=100 -connected-components 4 -auto-level svg.png
The result is amazing, it found 16 blocks for me. That's exactly what I want.

Origin image:
Image

After IM:

Image

Code: Select all

Objects (id: bounding-box centroid area mean-color):
  4: 289x193+27+160 168.1,253.6 41869 srgba(2,0,3,0.00808728)
  0: 342x412+0+0 170.5,157.3 39705 srgba(2,0,3,0.00810254)
  3: 337x302+3+75 169.8,202.8 39534 srgba(3,1,4,0.994903)
  2: 48x72+231+35 254.4,70.6 2958 srgba(3,1,4,0.994903)
  1: 47x72+63+35 86.0,70.6 2931 srgba(3,1,4,0.994903)
  5: 38x34+123+186 141.5,202.5 1292 srgba(3,1,4,0.994903)
  7: 38x34+241+186 259.5,202.5 1292 srgba(3,1,4,0.994903)
  6: 38x34+184+186 202.5,202.5 1291 srgba(3,1,4,0.994903)
  8: 38x33+63+242 81.5,258.0 1254 srgba(3,1,4,0.994903)
  9: 38x33+123+242 141.5,258.0 1254 srgba(3,1,4,0.994903)
  10: 38x33+184+242 202.5,258.0 1254 srgba(3,1,4,0.994903)
  11: 38x33+241+242 259.5,258.0 1254 srgba(3,1,4,0.994903)
  12: 38x33+63+299 81.5,315.0 1254 srgba(3,1,4,0.994903)
  13: 38x33+123+299 141.5,315.0 1254 srgba(3,1,4,0.994903)
  14: 38x33+184+299 202.5,315.0 1254 srgba(3,1,4,0.994903)
  15: 38x33+241+299 259.5,315.0 1254 srgba(3,1,4,0.994903)

The correct result is 14 blocks, is that possible to ignore transparent color?

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

Re: Count amount of non-continuous blocks in an image

Post by snibgo »

The script in https://stackoverflow.com/questions/287 ... timagedata counts unique colours in a raster image. I expect it gives the same result as IM's "-unique-colors".

Converting SVG to raster will generally anti-alias between flat areas. The anti-aliasing can be turned off if you use "msvg:" like this:

Code: Select all

magick +antialias msvg:in.svg out.png
As Fred says, the problem isn't the file type. The problem is that the image has been lossy-compressed with JPEG compression. If that happens and the result is converted to PNG or embedded in SVG or whatever, the lossy compression has still occurred.
snibgo's IM pages: im.snibgo.com

Peggy
Posts: 33
Joined: 2020-03-02T22:31:47-07:00
Authentication code: 1152

Re: Count amount of non-continuous blocks in an image

Post by Peggy »

snibgo wrote:
2020-03-06T09:14:39-07:00
The script in https://stackoverflow.com/questions/287 ... timagedata counts unique colours in a raster image. I expect it gives the same result as IM's "-unique-colors".

Converting SVG to raster will generally anti-alias between flat areas. The anti-aliasing can be turned off if you use "msvg:" like this:

Code: Select all

magick +antialias msvg:in.svg out.png
As Fred says, the problem isn't the file type. The problem is that the image has been lossy-compressed with JPEG compression. If that happens and the result is converted to PNG or embedded in SVG or whatever, the lossy compression has still occurred.
Got it, Thank you.

As the command below:

Code: Select all

convert svg.png +dither -colors 3 -define connected-components:verbose=true -define connected-components:area-threshold=100 -connected-components 4 -auto-level svg.png
Is that possible to ignore counting transparent color?

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Count amount of non-continuous blocks in an image

Post by fmw42 »

Can you post your svg file to some hosting service that does not require a log-in? I have not access to your google drive. What is your ImageMagick version and platform, since code and scripting may differ.

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Count amount of non-continuous blocks in an image

Post by fmw42 »

One way would be to make transparent into some other color and then add one more color to your -colors N. Then ignore the extra color with -define connected-components:remove-colors=your_extra_color. I suppose you can do that with transparent colors also. See https://imagemagick.org/script/connected-components.php

Since I do not have your SVG file, I will use your PNG. This is how I would do it on a Unix system

Code: Select all

convert img.png -colors 2 -define connected-components:verbose=true -define connected-components:area-threshold=100 -connected-components 4 -auto-level x.png | grep "srgba(1.17494%,0.390631%,1.5671%,0.994903)"
3: 338x302+2+75 170.2,202.9 39846 srgba(1.17494%,0.390631%,1.5671%,0.994903)
2: 48x72+231+35 254.4,70.6 2949 srgba(1.17494%,0.390631%,1.5671%,0.994903)
1: 48x72+62+35 85.9,70.6 2939 srgba(1.17494%,0.390631%,1.5671%,0.994903)
12: 39x34+62+298 81.3,314.8 1277 srgba(1.17494%,0.390631%,1.5671%,0.994903)
7: 38x34+241+186 259.5,202.4 1274 srgba(1.17494%,0.390631%,1.5671%,0.994903)
5: 38x34+123+186 141.5,202.4 1272 srgba(1.17494%,0.390631%,1.5671%,0.994903)
15: 38x34+241+298 259.5,314.8 1271 srgba(1.17494%,0.390631%,1.5671%,0.994903)
13: 38x34+123+298 141.5,314.8 1271 srgba(1.17494%,0.390631%,1.5671%,0.994903)
8: 39x34+62+241 81.3,258.0 1264 srgba(1.17494%,0.390631%,1.5671%,0.994903)
6: 38x34+184+186 202.7,202.4 1262 srgba(1.17494%,0.390631%,1.5671%,0.994903)
11: 38x34+241+241 259.5,257.9 1258 srgba(1.17494%,0.390631%,1.5671%,0.994903)
14: 38x34+184+298 202.7,314.8 1258 srgba(1.17494%,0.390631%,1.5671%,0.994903)
9: 38x34+123+241 141.5,258.0 1256 srgba(1.17494%,0.390631%,1.5671%,0.994903)
10: 38x34+184+241 202.7,257.9 1246 srgba(1.17494%,0.390631%,1.5671%,0.994903)

There are 14 items listed. But the image will show all 16.

Peggy
Posts: 33
Joined: 2020-03-02T22:31:47-07:00
Authentication code: 1152

Re: Count amount of non-continuous blocks in an image

Post by Peggy »

fmw42 wrote:
2020-03-08T22:41:06-07:00
Can you post your svg file to some hosting service that does not require a log-in? I have not access to your google drive. What is your ImageMagick version and platform, since code and scripting may differ.
Sorry my bad, here is svg:
https://drive.google.com/file/d/1SKdVqp ... sp=sharing

IM version:

Code: Select all

Version: ImageMagick 6.9.6-8 Q16 x86_64 2016-12-26 http://www.imagemagick.org
Copyright: Copyright 1999-2017 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher Modules
Delegates (built-in): fontconfig freetype jng jpeg lcms ltdl png tiff webp zlib
fmw42 wrote:
2020-03-08T22:47:11-07:00
One way would be to make transparent into some other color and then add one more color to your -colors N. Then ignore the extra color with -define connected-components:remove-colors=your_extra_color. I suppose you can do that with transparent colors also. See https://imagemagick.org/script/connected-components.php
I'll take time for this. Thanks a lot!

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Count amount of non-continuous blocks in an image

Post by fmw42 »

You did not tell us your platform!

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Count amount of non-continuous blocks in an image

Post by fmw42 »

Your image has really only two colors black and transparent. But the information is actually in the alpha channel. The base image is totally black. So you could convert from SVG to raster, flatten the image, stretch to full dynamic range so that you only have white and black, then do CCL and filter on the black using grep. Note that for this image, you do not need to filter by area, since it only has two colors.

Code: Select all

convert 1078.svg -background white -flatten -auto-level -type bilevel +write bilevel.png -define connected-components:verbose=true -connected-components 4 -auto-level result.png | grep "gray(0)"
12: 1894x1693+13+227 957.9,945.3 1258145 gray(0)
6: 267x405+1298+0 1430.6,202.1 92736 gray(0)
1: 267x405+351+0 484.4,202.1 92723 gray(0)
15: 214x189+1034+848 1140.7,942.4 40094 gray(0)
19: 214x188+1034+1161 1140.7,1254.5 40094 gray(0)
23: 214x189+1034+1479 1140.7,1572.9 40080 gray(0)
16: 213x189+1354+848 1460.0,942.4 40050 gray(0)
14: 213x189+690+848 796.0,942.4 40044 gray(0)
20: 213x188+1354+1161 1460.0,1254.5 40044 gray(0)
17: 214x188+351+1161 457.8,1254.5 40038 gray(0)
18: 213x188+690+1161 796.0,1254.5 40034 gray(0)
24: 213x189+1354+1479 1460.0,1572.9 40034 gray(0)
21: 214x189+351+1479 457.8,1572.9 40033 gray(0)
22: 214x189+690+1479 796.0,1572.8 40013 gray(0)


14 items returned

Peggy
Posts: 33
Joined: 2020-03-02T22:31:47-07:00
Authentication code: 1152

Re: Count amount of non-continuous blocks in an image

Post by Peggy »

fmw42 wrote:
2020-03-08T23:21:33-07:00
You did not tell us your platform!
Linux Alpine thank you.

Peggy
Posts: 33
Joined: 2020-03-02T22:31:47-07:00
Authentication code: 1152

Re: Count amount of non-continuous blocks in an image

Post by Peggy »

fmw42 wrote:
2020-03-08T23:32:26-07:00
Your image has really only two colors black and transparent. But the information is actually in the alpha channel. The base image is totally black. So you could convert from SVG to raster, flatten the image, stretch to full dynamic range so that you only have white and black, then do CCL and filter on the black using grep. Note that for this image, you do not need to filter by area, since it only has two colors.

Code: Select all

convert 1078.svg -background white -flatten -auto-level -type bilevel +write bilevel.png -define connected-components:verbose=true -connected-components 4 -auto-level result.png | grep "gray(0)"
12: 1894x1693+13+227 957.9,945.3 1258145 gray(0)
6: 267x405+1298+0 1430.6,202.1 92736 gray(0)
1: 267x405+351+0 484.4,202.1 92723 gray(0)
15: 214x189+1034+848 1140.7,942.4 40094 gray(0)
19: 214x188+1034+1161 1140.7,1254.5 40094 gray(0)
23: 214x189+1034+1479 1140.7,1572.9 40080 gray(0)
16: 213x189+1354+848 1460.0,942.4 40050 gray(0)
14: 213x189+690+848 796.0,942.4 40044 gray(0)
20: 213x188+1354+1161 1460.0,1254.5 40044 gray(0)
17: 214x188+351+1161 457.8,1254.5 40038 gray(0)
18: 213x188+690+1161 796.0,1254.5 40034 gray(0)
24: 213x189+1354+1479 1460.0,1572.9 40034 gray(0)
21: 214x189+351+1479 457.8,1572.9 40033 gray(0)
22: 214x189+690+1479 796.0,1572.8 40013 gray(0)


14 items returned
Looks great!! But, what if the image contains white? The image might has many color including white that uploading from user. Is there no way for just ignoring transparent color in IM?

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Count amount of non-continuous blocks in an image

Post by fmw42 »

No, unless you filter on each transparent color as I did with grep "gray(0)" or in the first example filtering on transparency as "srgba(1.17494%,0.390631%,1.5671%,0.994903)". You can filter on colors using -connected-components:remove-color="XXX", but only if you have more than two colors. With only one color remaining, the result may not look strange. Also note that transparency may be many colors if the underlying image is not the same color at each transparent pixel. But in your case here the underlying image is totally black. So your limitation is really that you only have two colors and excluding one, will produce an image of only one color or some other color may be introduced that you did not have, since a background color is needed to merge the excluded colors into it.

Peggy
Posts: 33
Joined: 2020-03-02T22:31:47-07:00
Authentication code: 1152

Re: Count amount of non-continuous blocks in an image

Post by Peggy »

Alright, after I read your post https://imagemagick.org/script/connected-components.php I tried -connected-components:remove-color=alpha, the result seems not changed. This small project looks pretty hard.. I just need programmably count "Connected-component", if it's won't be carryed out, it still can be counted by human eyes instead.

I could restrict user to upload any type of image or file, ask them to fill form for providing me color amount in the image. After helps you provided, what do you think? Do I still have chance to carry out this with IM?

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Count amount of non-continuous blocks in an image

Post by fmw42 »

-connected-components:remove-color=alpha
There is no such color as alpha. Perhaps you mean "transparent" or "none". But with only two colors (black and transparent) using remove-color will not work as I mentioned above. You would need to have at least 3 colors in your image. Getting rid of one would still list 2 colors and the third would be merged into your other two. I do not think that is what you want.

The method I showed of keeping only the color you want so that the list only shows the number of that color is the only way that I know it would work. The code could be made to just report the number.

Code: Select all

convert 1078.svg -background white -flatten -auto-level -type bilevel +write bilevel.png -define connected-components:verbose=true -connected-components 4 -auto-level result.png | grep "gray(0)" | wc -l | sed 's/ *//g'
14

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

Re: Count amount of non-continuous blocks in an image

Post by snibgo »

fmw42 wrote:-define connected-components:remove-colors=your_extra_color
@Fred: I can't see "connected-components:remove-colors" documented at either https://www.imagemagick.org/script/comm ... php#define or https://imagemagick.org/script/connected-components.php . Can you add this documentation please?
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Count amount of non-continuous blocks in an image

Post by fmw42 »

snibgo wrote:
2020-03-09T11:31:42-07:00
fmw42 wrote:-define connected-components:remove-colors=your_extra_color
@Fred: I can't see "connected-components:remove-colors" documented at either https://www.imagemagick.org/script/comm ... php#define or https://imagemagick.org/script/connected-components.php . Can you add this documentation please?
It says:
Rather than object ids, you can instead remove or keep objects identified by there color, e.g. -define connected-components:keep-colors=red;green;blue.
The text says "remove or keep". So I assumed that was sufficient. But I will try to change the documentation to include text for "-define connected-components:remove-colors=red;green;blue"

Locked