Page 1 of 5

Count amount of non-continuous blocks in an image

Posted: 2020-03-02T23:08:24-07:00
by Peggy
I've asked a question on Stack overflowhttps://tinyurl.com/wyn93rv, Thanks for @fmw42 suggested me that use connected-components to solve this problem. I've tred to implement the command, but the result seems incorrect.

Here is an origin image as below:
Image

Here is an image that contains block counting as below. (The result that I expect.)
Image

After the command:

Code: Select all

imagemagick convert /img/cat.png -define connected-components:area-threshold=100 -define connected-components:verbose=true -connected-components 4 -auto-level /img/cat_ok.png
I got the result as below:
Image

Code: Select all

Objects (id: bounding-box centroid area mean-color):
  0: 548x543+0+0 284.0,239.9 149464 srgb(242,233,208)
  817: 417x508+99+21 269.2,276.3 112752 srgb(59,57,43)
  11786: 140x141+182+273 249.3,349.8 13923 srgb(234,226,207)
  22526: 327x55+95+488 247.3,519.0 8992 srgb(187,180,158)
  21626: 76x45+225+467 267.3,481.6 1250 srgb(242,233,208)
  6571: 36x41+130+191 148.1,206.7 1046 srgb(59,57,41)
  25110: 142x15+213+528 277.7,535.2 1042 srgb(187,180,156)
  10349: 11x70+88+256 91.5,291.1 427 srgb(242,234,205)
  10751: 13x46+166+265 171.4,288.5 402 srgb(61,56,43)
  10326: 10x66+380+259 383.2,293.2 386 srgb(61,56,43)
  11989: 26x15+233+276 246.9,282.8 326 srgb(61,56,43)
  25680: 57x5+260+538 287.3,540.0 278 srgb(242,233,208)
  17468: 12x37+177+361 182.7,376.2 266 srgb(234,226,207)
  7911: 13x36+388+205 396.4,220.1 257 srgb(242,233,206)
  16263: 8x46+98+354 101.8,377.0 236 srgb(59,57,45)
  21741: 17x21+212+470 219.1,479.0 234 srgb(244,234,209)
  23290: 18x17+363+500 371.2,507.4 230 srgb(187,180,156)
  10543: 5x56+387+262 389.4,289.7 227 srgb(241,234,208)
  7844: 7x41+97+206 100.6,226.4 221 srgb(59,57,43)
  12553: 15x28+178+283 184.8,294.7 221 srgb(232,227,205)
  14234: 9x40+509+311 513.2,331.4 216 srgb(61,57,41)
  3213: 7x34+368+73 370.7,90.3 210 srgb(61,56,43)
  15027: 7x38+97+326 100.3,344.8 193 srgb(62,55,43)
  22409: 36x13+257+487 279.2,491.2 181 srgb(187,180,156)
  6881: 14x18+246+194 252.4,200.3 176 srgb(61,57,41)
  3426: 7x25+112+84 115.0,96.0 175 srgb(59,57,41)
  11564: 12x32+297+270 300.9,288.0 169 srgb(60,59,43)
  20852: 9x25+298+456 302.4,467.4 149 srgb(61,57,44)
  1792: 8x25+110+52 113.9,64.7 148 srgb(59,57,42)
  3857: 4x37+385+118 386.5,136.0 148 srgb(244,234,209)
  4916: 11x17+174+159 178.8,166.7 132 srgb(232,226,209)
  21742: 32x6+227+470 240.4,473.3 130 srgb(244,235,207)
  4436: 10x16+288+151 292.2,158.2 122 srgb(59,57,41)
  3492: 6x32+104+89 107.4,106.5 119 srgb(59,57,45)
  4519: 5x32+99+153 101.6,169.0 117 srgb(59,57,45)
  14782: 5x28+104+326 105.8,340.6 116 srgb(61,56,43)
  4791: 12x20+170+159 173.9,170.5 103 srgb(234,226,207)
  10749: 8x27+96+264 99.3,278.6 103 srgb(61,56,44)

How do I get the result closer to 13 blocks? I don't really need exactly block amount but at least don't be too bad. Any help would be appreciated.

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

Posted: 2020-03-03T05:00:20-07:00
by magick
Try this command:

Code: Select all

convert cat.png -colors 3 -define connected-components:verbose=true -define connected-components:area-threshold=100 -connected-components 4 -auto-level x:
Objects (id: bounding-box centroid area mean-color):
  0: 548x543+0+0 286.2,245.1 147682 srgb(94.5251%,91.0933%,81.532%)
  2: 420x509+94+13 265.1,268.0 117855 srgb(23.6835%,22.7161%,17.0718%)
  537: 152x145+177+265 244.1,341.1 15142 srgb(94.5251%,91.0933%,81.532%)
  2707: 343x58+81+484 247.8,516.2 11412 srgb(73.8079%,71.0857%,62.4811%)
  2658: 123x61+204+459 257.9,477.2 2295 srgb(94.5251%,91.0933%,81.532%)
  580: 94x19+188+270 234.1,278.6 1281 srgb(23.6835%,22.7161%,17.0718%)
  237: 33x46+226+169 241.8,182.8 590 srgb(94.5251%,91.0933%,81.532%)
  164: 16x25+168+153 175.0,164.8 319 srgb(94.5251%,91.0933%,81.532%)
  157: 15x23+303+149 310.1,159.8 262 srgb(94.5251%,91.0933%,81.532%)
  341: 55x8+303+190 330.3,193.2 220 srgb(94.5251%,91.0933%,81.532%)
  380: 50x5+119+196 144.0,198.1 200 srgb(94.5251%,91.0933%,81.532%)
  411: 37x7+315+205 333.7,207.7 155 srgb(94.5251%,91.0933%,81.532%)
  426: 38x9+126+210 144.0,214.2 151 srgb(94.5251%,91.0933%,81.532%)

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

Posted: 2020-03-03T10:40:01-07:00
by fmw42
Try these. The first on colors the output with the different ids for the colors and scales that to full dynamic range so the values will not properly match the listing. The second colors the output with the mean colors of the regions, which in this case should be the same as the input after forcing to 3 colors. Note the additions of -define connected-components:mean-color=true

Input:
Image

Code: Select all

convert cat.png +dither -colors 3 \
-define connected-components:verbose=true \
-define connected-components:area-threshold=100 \
-connected-components 4 -auto-level cat_ccl_ids.png
Objects (id: bounding-box centroid area mean-color):
  0: 548x543+0+0 286.2,245.3 147781 srgb(94.5251%,91.0933%,81.532%)
  2: 420x507+94+13 265.1,267.9 117722 srgb(23.6835%,22.7161%,17.0718%)
  332: 152x145+177+265 244.0,341.0 15212 srgb(94.5251%,91.0933%,81.532%)
  687: 343x57+81+484 247.8,516.2 11295 srgb(73.8079%,71.0857%,62.4811%)
  663: 134x63+204+458 258.4,477.6 2360 srgb(94.5251%,91.0933%,81.532%)
  350: 94x18+188+270 233.9,278.6 1280 srgb(23.6835%,22.7161%,17.0718%)
  186: 33x44+226+170 241.8,182.5 604 srgb(94.5251%,91.0933%,81.532%)
  159: 16x25+168+153 174.9,164.8 326 srgb(94.5251%,91.0933%,81.532%)
  150: 15x23+303+149 310.1,159.2 268 srgb(94.5251%,91.0933%,81.532%)
  231: 55x8+303+190 330.0,193.3 236 srgb(94.5251%,91.0933%,81.532%)
  242: 51x5+119+196 142.6,197.7 180 srgb(94.5251%,91.0933%,81.532%)
  263: 38x8+126+211 143.5,214.2 150 srgb(94.5251%,91.0933%,81.532%)
  252: 36x6+315+205 332.2,207.5 150 srgb(94.5251%,91.0933%,81.532%)
Image



Code: Select all

convert cat.png +dither -colors 3 \
-define connected-components:verbose=true \
-define connected-components:mean-color=true \
-define connected-components:area-threshold=100 \
-connected-components 4 cat_ccl.png
Objects (id: bounding-box centroid area mean-color):
  0: 548x543+0+0 286.2,245.3 147781 srgb(94.5251%,91.0933%,81.532%)
  2: 420x507+94+13 265.1,267.9 117722 srgb(23.6835%,22.7161%,17.0718%)
  332: 152x145+177+265 244.0,341.0 15212 srgb(94.5251%,91.0933%,81.532%)
  687: 343x57+81+484 247.8,516.2 11295 srgb(73.8079%,71.0857%,62.4811%)
  663: 134x63+204+458 258.4,477.6 2360 srgb(94.5251%,91.0933%,81.532%)
  350: 94x18+188+270 233.9,278.6 1280 srgb(23.6835%,22.7161%,17.0718%)
  186: 33x44+226+170 241.8,182.5 604 srgb(94.5251%,91.0933%,81.532%)
  159: 16x25+168+153 174.9,164.8 326 srgb(94.5251%,91.0933%,81.532%)
  150: 15x23+303+149 310.1,159.2 268 srgb(94.5251%,91.0933%,81.532%)
  231: 55x8+303+190 330.0,193.3 236 srgb(94.5251%,91.0933%,81.532%)
  242: 51x5+119+196 142.6,197.7 180 srgb(94.5251%,91.0933%,81.532%)
  263: 38x8+126+211 143.5,214.2 150 srgb(94.5251%,91.0933%,81.532%)
  252: 36x6+315+205 332.2,207.5 150 srgb(94.5251%,91.0933%,81.532%)
Image

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

Posted: 2020-03-04T00:08:43-07:00
by Peggy
Thank you guys for the detailed reply.

I tried exactly same command as fmw42 with mean-color=true option, but I got different result:

Image output:(It's only 11 blocks)
Image

Code: Select all

Objects (id: bounding-box centroid area mean-color):
  0: 548x543+0+0 283.5,267.8 161700 srgb(237,229,205)
  1: 420x507+94+13 264.7,267.9 117063 srgb(60,58,44)
  9: 154x146+176+264 243.7,340.3 15427 srgb(237,228,204)
  10: 95x17+188+270 234.1,278.2 1226 srgb(60,58,44)
  4: 34x46+225+169 241.5,182.6 663 srgb(237,228,204)
  3: 17x26+167+152 174.5,164.4 345 srgb(237,228,204)
  2: 16x22+302+149 309.8,159.1 280 srgb(237,228,204)
  5: 56x8+302+189 329.9,192.8 260 srgb(237,228,204)
  6: 52x6+118+195 143.5,197.6 243 srgb(237,228,204)
  7: 38x8+314+204 332.4,207.2 180 srgb(237,228,204)
  8: 38x9+126+210 144.0,213.8 177 srgb(237,228,204)
IM version is:

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
Whatever, no matter how was the result, it's really close what I need.

Well, could you try the image below for me? I think the result is out of my expection. Should I set different options for different image?

Origin image:
Image

result: (something missing)
Image

Code: Select all

Objects (id: bounding-box centroid area mean-color):
  0: 512x320+0+0 256.0,157.4 136355 srgb(255,255,255)
  71: 179x170+170+85 262.0,172.7 12820 srgb(149,182,141)
  155: 125x68+165+146 228.6,179.2 5939 srgb(255,255,255)
  2: 200x196+149+58 247.1,166.2 5719 srgb(116,76,41)
  124: 117x14+170+140 234.4,144.7 683 srgb(255,255,255)
  115: 26x34+315+127 327.5,138.5 461 srgb(149,182,141)
  48: 25x33+284+79 299.1,93.0 427 srgb(149,182,141)
  5: 27x23+236+61 249.0,73.5 331 srgb(149,182,141)
  381: 29x44+303+203 319.7,231.2 305 srgb(116,76,41)
  341: 16x35+326+181 333.1,193.3 283 srgb(149,182,141)
  289: 25x15+201+166 213.0,174.5 182 srgb(116,76,41)
  422: 29x8+205+209 218.3,213.7 116 srgb(255,255,255)
  274: 11x12+233+162 238.0,167.5 112 srgb(116,76,41)
  271: 11x13+180+162 184.8,168.0 107 srgb(116,76,41)

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

Posted: 2020-03-04T02:37:07-07:00
by snibgo
The problem is that your images are not solid colours. At first glance, your cat-and-mug image has three colours, but in fact it has many more. So "-colors 3" reduces it to actually three.

How many colours, at first glance, in your new image? At least four. But there is also a darker green, and four small pink marks near the centre. So that make five. So use "-colors 5".

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

Posted: 2020-03-04T03:55:22-07:00
by Peggy
snibgo wrote:
2020-03-04T02:37:07-07:00
The problem is that your images are not solid colours. At first glance, your cat-and-mug image has three colours, but in fact it has many more. So "-colors 3" reduces it to actually three.

How many colours, at first glance, in your new image? At least four. But there is also a darker green, and four small pink marks near the centre. So that make five. So use "-colors 5".
Thank you.

Users upload the image to the server, I won't know how many colors should be set. Is there any solution for this?

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

Posted: 2020-03-04T10:32:55-07:00
by fmw42
First, JPG images are compressed. So you will have many colors even though you may have started with only a few primary colors. The compression changes values. So you need to know how many unique colors you want. I know of no good way to determine that other than looking at the histogram. Pick the number of colors from the histogram that has counts above some threshold percent of the total pixels.

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

Posted: 2020-03-05T00:47:41-07:00
by Peggy
fmw42 wrote:
2020-03-04T10:32:55-07:00
First, JPG images are compressed. So you will have many colors even though you may have started with only a few primary colors. The compression changes values. So you need to know how many unique colors you want. I know of no good way to determine that other than looking at the histogram. Pick the number of colors from the histogram that has counts above some threshold percent of the total pixels.
What do you mean histogram ?
May I get unique colors like this post https://imagemagick.org/discourse-serve ... =1&t=37649 to get color amount to be -colors arg?

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

Posted: 2020-03-05T01:27:19-07:00
by fmw42
What do you mean histogram ?
May I get unique colors like this post https://imagemagick.org/discourse-serve ... =1&t=37649 to get color amount to be -colors arg?
Histogram - see https://imagemagick.org/Usage/files/#histogram

No, that will give too many colors especially since you have a JPG file.

Snibgo has a good solution. It just needs to be converted to Unix for you.

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

Posted: 2020-03-05T01:38:30-07:00
by Peggy
fmw42 wrote:
2020-03-05T01:27:19-07:00
What do you mean histogram ?
May I get unique colors like this post https://imagemagick.org/discourse-serve ... =1&t=37649 to get color amount to be -colors arg?
Histogram - see https://imagemagick.org/Usage/files/#histogram

No, that will give too many colors especially since you have a JPG file.

Snibgo has a good solution. It just needs to be converted to Unix for you.
Alright, that would be great if Snibgo's solution works. Thank you for you help.

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

Posted: 2020-03-05T12:00:40-07:00
by snibgo
In the other thread https://www.imagemagick.org/discourse-s ... =1&t=37649 I show a method for de-anti-aliasing an image that has otherwise flat colours.

Your "dragon-hi" image doesn't have flat colours because it is jpeg-compressed, which is lossy, which means it has changed many pixels. So that method won't work for you.

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

Posted: 2020-03-05T15:48:06-07:00
by snibgo
See also 'Jump' method, which shows a different method for segmenting images. For the "dragon-hi" image, it find 8 colours but mis-colours the tears:
Image

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

Posted: 2020-03-05T22:18:45-07:00
by Peggy
snibgo wrote:
2020-03-05T15:48:06-07:00
See also 'Jump' method, which shows a different method for segmenting images. For the "dragon-hi" image, it find 8 colours but mis-colours the tears:
Image
Does that mean if it's a png file, your method would work right? Could you please help me for https://imagemagick.org/discourse-serve ... =1&t=37649 ? I think I could restrict users, they are only allowed to upload png file. I'll also read Jump' method see if it helps, thank you.

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

Posted: 2020-03-05T23:29:54-07:00
by fmw42
If the png were created from the JPG, then I believe it would still have the same issue. Once the lossy compression and JPG artifacts are there, they are not easily removed. So the PNG would have to be created initially. You might consider using vector images such as SVG.

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

Posted: 2020-03-05T23:39:57-07:00
by Peggy
fmw42 wrote:
2020-03-05T23:29:54-07:00
If the png were created from the JPG, then I believe it would still have the same issue. Once the lossy compression and JPG artifacts are there, they are not easily removed. So the PNG would have to be created initially. You might consider using vector images such as SVG.
SVG would be a good solution. If it's a SVG file, I guess I could use pure js to count unique color like https://stackoverflow.com/questions/287 ... timagedata.