Page 3 of 5

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

Posted: 2020-03-09T23:31:53-07:00
by Peggy
fmw42 wrote:
2020-03-09T10:33:43-07:00
-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
Thank you for your effort and explanation.

As you metioned, I need at least 3 colors to get expected result from transparent background image. So, I won't be able to count the image correctly something like below right?

Image

If transparent can't be ignored, the counting result would very wrong for the image. The expected result might be only least than 50, if transparency is counted, the result might be more the 100.

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

Posted: 2020-03-09T23:35:11-07:00
by snibgo
What is the "right" result for that image?

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

Posted: 2020-03-09T23:51:51-07:00
by Peggy
snibgo wrote:
2020-03-09T23:35:11-07:00
What is the "right" result for that image?
Sorry, I just modified my reply. One continuous area(color) should be counted as 1 block, like the image below.

Image


For the butterfly:
Image

Red number 1 occupies most of area but it's only counted as 1.
Red number 2,3,4,5..and more are small blocks these are individually counted.

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

Posted: 2020-03-10T04:09:20-07:00
by Peggy
fmw42 wrote:
2020-03-09T10:33:43-07:00
-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
Sorry, English is not my native language. I probably missed somthing. As I've known so far, I still have some problems that need to be solved.

1. Transparent background:
I tried -connected-components:remove-color again, -connected-components:remove-color=none or -connected-components:remove-color=transparent doesn't work for transparent BG image. Even though colors more than 3.

Tested command:

Code: Select all

convert transbg_1.png -colors 2 -define connected-components:verbose=true -define -connected-components:remove-color=transparent  -define connected-components:area-threshold=100 -connected-components 4 -auto-level transbg_1_ok.png 
Tested image:
Image

Tested result(Should be 2 blocks):

Code: Select all

Objects (id: bounding-box centroid area mean-color):
  0: 320x320+0+0 159.3,176.3 61306 srgba(200,50,50,0.00285344)
  1: 207x303+57+8 159.8,136.4 38032 srgba(194,48,48,0.996231)
  2: 63x63+129+80 159.8,111.1 3062 srgba(200,50,50,0.00201419)
Another situation is, If there are only 2 colors with main color white, how do I decide background color? Could still use this command?

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'
*Could you provide me png version? I think I couldn't ask users to upload svg file, that's too hard to do for them.

Example image:
Image

How to count exactly 14 bolcks for this case(Only 2 colors, white main color with transparent background)?




2. Unique color counting.
I need Unique color amount for -colors arg. Due to the image color might be compressed, there is no good method to count unique color amount. I think I might need a form for user so they can fill color amount manually. I think this is the only solution for now.

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

Posted: 2020-03-10T09:13:42-07:00
by fmw42
Your red tear-drop image has many more colors than 3. That is because there are many more colors under the transparency. You can see that from:

Code: Select all

convert red.png -compress none -unique-colors txt:
You can also see that from removing the alpha channel by

Code: Select all

convert red.png -alpha off aoff.png
You need to be sure you image has only 3 colors including the transparent color. Then you need to use the actual transparent color as specified in the unique colors. Note that you have many alpha values and not just fully opaque and fully transparent.

Even if I threshold your alpha channel to make it either fully transparent or fully opaque and make the color under the transparent pixels black so that the transparent color is "none", I still get way more than 3 colors

Code: Select all

convert red.png -channel a -threshold 50% +channel -background black -alpha background -compress none -unique-colors txt:
# ImageMagick pixel enumeration: 96,1,65535,srgba
0,0: (142,35,35,255)  #8E2323FF  srgba(142,35,35,1)
1,0: (143,35,35,255)  #8F2323FF  srgba(143,35,35,1)
2,0: (144,35,35,255)  #902323FF  srgba(144,35,35,1)
3,0: (145,35,35,255)  #912323FF  srgba(145,35,35,1)
4,0: (146,35,35,255)  #922323FF  srgba(146,35,35,1)
5,0: (145,36,36,255)  #912424FF  srgba(145,36,36,1)
6,0: (146,36,36,255)  #922424FF  srgba(146,36,36,1)
7,0: (147,36,36,255)  #932424FF  srgba(147,36,36,1)
8,0: (148,36,36,255)  #942424FF  srgba(148,36,36,1)
9,0: (149,36,36,255)  #952424FF  srgba(149,36,36,1)
10,0: (149,37,37,255)  #952525FF  srgba(149,37,37,1)
11,0: (151,37,37,255)  #972525FF  srgba(151,37,37,1)
12,0: (150,37,37,255)  #962525FF  srgba(150,37,37,1)
13,0: (152,37,37,255)  #982525FF  srgba(152,37,37,1)
14,0: (153,37,37,255)  #992525FF  srgba(153,37,37,1)
15,0: (153,38,38,255)  #992626FF  srgba(153,38,38,1)
16,0: (154,38,38,255)  #9A2626FF  srgba(154,38,38,1)
17,0: (155,38,38,255)  #9B2626FF  srgba(155,38,38,1)
18,0: (156,38,38,255)  #9C2626FF  srgba(156,38,38,1)
19,0: (157,39,39,255)  #9D2727FF  srgba(157,39,39,1)
20,0: (156,39,39,255)  #9C2727FF  srgba(156,39,39,1)
21,0: (157,38,38,255)  #9D2626FF  srgba(157,38,38,1)
22,0: (158,39,39,255)  #9E2727FF  srgba(158,39,39,1)
23,0: (159,39,39,255)  #9F2727FF  srgba(159,39,39,1)
24,0: (161,39,39,255)  #A12727FF  srgba(161,39,39,1)
25,0: (160,39,39,255)  #A02727FF  srgba(160,39,39,1)
26,0: (161,40,40,255)  #A12828FF  srgba(161,40,40,1)
27,0: (160,40,40,255)  #A02828FF  srgba(160,40,40,1)
28,0: (163,40,40,255)  #A32828FF  srgba(163,40,40,1)
29,0: (162,40,40,255)  #A22828FF  srgba(162,40,40,1)
30,0: (164,41,41,255)  #A42929FF  srgba(164,41,41,1)
31,0: (164,40,40,255)  #A42828FF  srgba(164,40,40,1)
32,0: (165,41,41,255)  #A52929FF  srgba(165,41,41,1)
33,0: (166,41,41,255)  #A62929FF  srgba(166,41,41,1)
34,0: (167,41,41,255)  #A72929FF  srgba(167,41,41,1)
35,0: (168,41,41,255)  #A82929FF  srgba(168,41,41,1)
36,0: (170,41,41,255)  #AA2929FF  srgba(170,41,41,1)
37,0: (168,42,42,255)  #A82A2AFF  srgba(168,42,42,1)
38,0: (169,42,42,255)  #A92A2AFF  srgba(169,42,42,1)
39,0: (170,42,42,255)  #AA2A2AFF  srgba(170,42,42,1)
40,0: (171,42,42,255)  #AB2A2AFF  srgba(171,42,42,1)
41,0: (171,43,43,255)  #AB2B2BFF  srgba(171,43,43,1)
42,0: (172,43,43,255)  #AC2B2BFF  srgba(172,43,43,1)
43,0: (173,43,43,255)  #AD2B2BFF  srgba(173,43,43,1)
44,0: (172,42,42,255)  #AC2A2AFF  srgba(172,42,42,1)
45,0: (174,43,43,255)  #AE2B2BFF  srgba(174,43,43,1)
46,0: (175,43,43,255)  #AF2B2BFF  srgba(175,43,43,1)
47,0: (175,44,44,255)  #AF2C2CFF  srgba(175,44,44,1)
48,0: (176,43,43,255)  #B02B2BFF  srgba(176,43,43,1)
49,0: (177,44,44,255)  #B12C2CFF  srgba(177,44,44,1)
50,0: (176,44,44,255)  #B02C2CFF  srgba(176,44,44,1)
51,0: (179,44,44,255)  #B32C2CFF  srgba(179,44,44,1)
52,0: (178,44,44,255)  #B22C2CFF  srgba(178,44,44,1)
53,0: (179,45,45,255)  #B32D2DFF  srgba(179,45,45,1)
54,0: (181,45,45,255)  #B52D2DFF  srgba(181,45,45,1)
55,0: (180,45,45,255)  #B42D2DFF  srgba(180,45,45,1)
56,0: (180,44,44,255)  #B42C2CFF  srgba(180,44,44,1)
57,0: (183,45,45,255)  #B72D2DFF  srgba(183,45,45,1)
58,0: (182,45,45,255)  #B62D2DFF  srgba(182,45,45,1)
59,0: (184,45,45,255)  #B82D2DFF  srgba(184,45,45,1)
60,0: (184,46,46,255)  #B82E2EFF  srgba(184,46,46,1)
61,0: (185,46,46,255)  #B92E2EFF  srgba(185,46,46,1)
62,0: (186,46,46,255)  #BA2E2EFF  srgba(186,46,46,1)
63,0: (187,46,46,255)  #BB2E2EFF  srgba(187,46,46,1)
64,0: (187,47,47,255)  #BB2F2FFF  srgba(187,47,47,1)
65,0: (186,47,47,255)  #BA2F2FFF  srgba(186,47,47,1)
66,0: (188,47,47,255)  #BC2F2FFF  srgba(188,47,47,1)
67,0: (189,47,47,255)  #BD2F2FFF  srgba(189,47,47,1)
68,0: (191,47,47,255)  #BF2F2FFF  srgba(191,47,47,1)
69,0: (190,47,47,255)  #BE2F2FFF  srgba(190,47,47,1)
70,0: (191,48,48,255)  #BF3030FF  srgba(191,48,48,1)
71,0: (190,48,48,255)  #BE3030FF  srgba(190,48,48,1)
72,0: (193,48,48,255)  #C13030FF  srgba(193,48,48,1)
73,0: (192,48,48,255)  #C03030FF  srgba(192,48,48,1)
74,0: (194,48,48,255)  #C23030FF  srgba(194,48,48,1)
75,0: (195,49,49,255)  #C33131FF  srgba(195,49,49,1)
76,0: (194,49,49,255)  #C23131FF  srgba(194,49,49,1)
77,0: (195,48,48,255)  #C33030FF  srgba(195,48,48,1)
78,0: (196,49,49,255)  #C43131FF  srgba(196,49,49,1)
79,0: (197,49,49,255)  #C53131FF  srgba(197,49,49,1)
80,0: (198,49,49,255)  #C63131FF  srgba(198,49,49,1)
81,0: (199,50,50,255)  #C73232FF  srgba(199,50,50,1)
82,0: (198,50,50,255)  #C63232FF  srgba(198,50,50,1)
83,0: (200,50,50,255)  #C83232FF  srgba(200,50,50,1)
84,0: (201,50,50,255)  #C93232FF  srgba(201,50,50,1)
85,0: (203,51,51,255)  #CB3333FF  srgba(203,51,51,1)
86,0: (202,51,51,255)  #CA3333FF  srgba(202,51,51,1)
87,0: (202,50,50,255)  #CA3232FF  srgba(202,50,50,1)
88,0: (204,51,51,255)  #CC3333FF  srgba(204,51,51,1)
89,0: (205,51,51,255)  #CD3333FF  srgba(205,51,51,1)
90,0: (206,51,51,255)  #CE3333FF  srgba(206,51,51,1)
91,0: (206,52,52,255)  #CE3434FF  srgba(206,52,52,1)
92,0: (207,52,52,255)  #CF3434FF  srgba(207,52,52,1)
93,0: (208,52,52,255)  #D03434FF  srgba(208,52,52,1)
94,0: (209,52,52,255)  #D13434FF  srgba(209,52,52,1)
95,0: (0,0,0,0)  #00000000  none
You can see this again from turning off the alpha channel as I did above.

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

Posted: 2020-03-10T22:29:35-07:00
by Peggy
fmw42 wrote:
2020-03-10T09:13:42-07:00
Your red tear-drop image has many more colors than 3. That is because there are many more colors under the transparency. You can see that from:

Code: Select all

convert red.png -compress none -unique-colors txt:
You can also see that from removing the alpha channel by

Code: Select all

convert red.png -alpha off aoff.png
You need to be sure you image has only 3 colors including the transparent color. Then you need to use the actual transparent color as specified in the unique colors. Note that you have many alpha values and not just fully opaque and fully transparent.

Even if I threshold your alpha channel to make it either fully transparent or fully opaque and make the color under the transparent pixels black so that the transparent color is "none", I still get way more than 3 colors

Code: Select all

convert red.png -channel a -threshold 50% +channel -background black -alpha background -compress none -unique-colors txt:
# ImageMagick pixel enumeration: 96,1,65535,srgba
0,0: (142,35,35,255)  #8E2323FF  srgba(142,35,35,1)
1,0: (143,35,35,255)  #8F2323FF  srgba(143,35,35,1)
2,0: (144,35,35,255)  #902323FF  srgba(144,35,35,1)
3,0: (145,35,35,255)  #912323FF  srgba(145,35,35,1)
4,0: (146,35,35,255)  #922323FF  srgba(146,35,35,1)
5,0: (145,36,36,255)  #912424FF  srgba(145,36,36,1)
6,0: (146,36,36,255)  #922424FF  srgba(146,36,36,1)
7,0: (147,36,36,255)  #932424FF  srgba(147,36,36,1)
8,0: (148,36,36,255)  #942424FF  srgba(148,36,36,1)
9,0: (149,36,36,255)  #952424FF  srgba(149,36,36,1)
10,0: (149,37,37,255)  #952525FF  srgba(149,37,37,1)
11,0: (151,37,37,255)  #972525FF  srgba(151,37,37,1)
12,0: (150,37,37,255)  #962525FF  srgba(150,37,37,1)
13,0: (152,37,37,255)  #982525FF  srgba(152,37,37,1)
14,0: (153,37,37,255)  #992525FF  srgba(153,37,37,1)
15,0: (153,38,38,255)  #992626FF  srgba(153,38,38,1)
16,0: (154,38,38,255)  #9A2626FF  srgba(154,38,38,1)
17,0: (155,38,38,255)  #9B2626FF  srgba(155,38,38,1)
18,0: (156,38,38,255)  #9C2626FF  srgba(156,38,38,1)
19,0: (157,39,39,255)  #9D2727FF  srgba(157,39,39,1)
20,0: (156,39,39,255)  #9C2727FF  srgba(156,39,39,1)
21,0: (157,38,38,255)  #9D2626FF  srgba(157,38,38,1)
22,0: (158,39,39,255)  #9E2727FF  srgba(158,39,39,1)
23,0: (159,39,39,255)  #9F2727FF  srgba(159,39,39,1)
24,0: (161,39,39,255)  #A12727FF  srgba(161,39,39,1)
25,0: (160,39,39,255)  #A02727FF  srgba(160,39,39,1)
26,0: (161,40,40,255)  #A12828FF  srgba(161,40,40,1)
27,0: (160,40,40,255)  #A02828FF  srgba(160,40,40,1)
28,0: (163,40,40,255)  #A32828FF  srgba(163,40,40,1)
29,0: (162,40,40,255)  #A22828FF  srgba(162,40,40,1)
30,0: (164,41,41,255)  #A42929FF  srgba(164,41,41,1)
31,0: (164,40,40,255)  #A42828FF  srgba(164,40,40,1)
32,0: (165,41,41,255)  #A52929FF  srgba(165,41,41,1)
33,0: (166,41,41,255)  #A62929FF  srgba(166,41,41,1)
34,0: (167,41,41,255)  #A72929FF  srgba(167,41,41,1)
35,0: (168,41,41,255)  #A82929FF  srgba(168,41,41,1)
36,0: (170,41,41,255)  #AA2929FF  srgba(170,41,41,1)
37,0: (168,42,42,255)  #A82A2AFF  srgba(168,42,42,1)
38,0: (169,42,42,255)  #A92A2AFF  srgba(169,42,42,1)
39,0: (170,42,42,255)  #AA2A2AFF  srgba(170,42,42,1)
40,0: (171,42,42,255)  #AB2A2AFF  srgba(171,42,42,1)
41,0: (171,43,43,255)  #AB2B2BFF  srgba(171,43,43,1)
42,0: (172,43,43,255)  #AC2B2BFF  srgba(172,43,43,1)
43,0: (173,43,43,255)  #AD2B2BFF  srgba(173,43,43,1)
44,0: (172,42,42,255)  #AC2A2AFF  srgba(172,42,42,1)
45,0: (174,43,43,255)  #AE2B2BFF  srgba(174,43,43,1)
46,0: (175,43,43,255)  #AF2B2BFF  srgba(175,43,43,1)
47,0: (175,44,44,255)  #AF2C2CFF  srgba(175,44,44,1)
48,0: (176,43,43,255)  #B02B2BFF  srgba(176,43,43,1)
49,0: (177,44,44,255)  #B12C2CFF  srgba(177,44,44,1)
50,0: (176,44,44,255)  #B02C2CFF  srgba(176,44,44,1)
51,0: (179,44,44,255)  #B32C2CFF  srgba(179,44,44,1)
52,0: (178,44,44,255)  #B22C2CFF  srgba(178,44,44,1)
53,0: (179,45,45,255)  #B32D2DFF  srgba(179,45,45,1)
54,0: (181,45,45,255)  #B52D2DFF  srgba(181,45,45,1)
55,0: (180,45,45,255)  #B42D2DFF  srgba(180,45,45,1)
56,0: (180,44,44,255)  #B42C2CFF  srgba(180,44,44,1)
57,0: (183,45,45,255)  #B72D2DFF  srgba(183,45,45,1)
58,0: (182,45,45,255)  #B62D2DFF  srgba(182,45,45,1)
59,0: (184,45,45,255)  #B82D2DFF  srgba(184,45,45,1)
60,0: (184,46,46,255)  #B82E2EFF  srgba(184,46,46,1)
61,0: (185,46,46,255)  #B92E2EFF  srgba(185,46,46,1)
62,0: (186,46,46,255)  #BA2E2EFF  srgba(186,46,46,1)
63,0: (187,46,46,255)  #BB2E2EFF  srgba(187,46,46,1)
64,0: (187,47,47,255)  #BB2F2FFF  srgba(187,47,47,1)
65,0: (186,47,47,255)  #BA2F2FFF  srgba(186,47,47,1)
66,0: (188,47,47,255)  #BC2F2FFF  srgba(188,47,47,1)
67,0: (189,47,47,255)  #BD2F2FFF  srgba(189,47,47,1)
68,0: (191,47,47,255)  #BF2F2FFF  srgba(191,47,47,1)
69,0: (190,47,47,255)  #BE2F2FFF  srgba(190,47,47,1)
70,0: (191,48,48,255)  #BF3030FF  srgba(191,48,48,1)
71,0: (190,48,48,255)  #BE3030FF  srgba(190,48,48,1)
72,0: (193,48,48,255)  #C13030FF  srgba(193,48,48,1)
73,0: (192,48,48,255)  #C03030FF  srgba(192,48,48,1)
74,0: (194,48,48,255)  #C23030FF  srgba(194,48,48,1)
75,0: (195,49,49,255)  #C33131FF  srgba(195,49,49,1)
76,0: (194,49,49,255)  #C23131FF  srgba(194,49,49,1)
77,0: (195,48,48,255)  #C33030FF  srgba(195,48,48,1)
78,0: (196,49,49,255)  #C43131FF  srgba(196,49,49,1)
79,0: (197,49,49,255)  #C53131FF  srgba(197,49,49,1)
80,0: (198,49,49,255)  #C63131FF  srgba(198,49,49,1)
81,0: (199,50,50,255)  #C73232FF  srgba(199,50,50,1)
82,0: (198,50,50,255)  #C63232FF  srgba(198,50,50,1)
83,0: (200,50,50,255)  #C83232FF  srgba(200,50,50,1)
84,0: (201,50,50,255)  #C93232FF  srgba(201,50,50,1)
85,0: (203,51,51,255)  #CB3333FF  srgba(203,51,51,1)
86,0: (202,51,51,255)  #CA3333FF  srgba(202,51,51,1)
87,0: (202,50,50,255)  #CA3232FF  srgba(202,50,50,1)
88,0: (204,51,51,255)  #CC3333FF  srgba(204,51,51,1)
89,0: (205,51,51,255)  #CD3333FF  srgba(205,51,51,1)
90,0: (206,51,51,255)  #CE3333FF  srgba(206,51,51,1)
91,0: (206,52,52,255)  #CE3434FF  srgba(206,52,52,1)
92,0: (207,52,52,255)  #CF3434FF  srgba(207,52,52,1)
93,0: (208,52,52,255)  #D03434FF  srgba(208,52,52,1)
94,0: (209,52,52,255)  #D13434FF  srgba(209,52,52,1)
95,0: (0,0,0,0)  #00000000  none
You can see this again from turning off the alpha channel as I did above.
You're right, I guess this won't work for my project, there are some problems can't be solved. Thank you guys for your great effort! /hug

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

Posted: 2020-03-10T23:26:51-07:00
by fmw42
The technique works. It is your images that are not what you think they are in terms of number of colors.

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

Posted: 2020-03-10T23:56:05-07:00
by Peggy
fmw42 wrote:
2020-03-10T23:26:51-07:00
The technique works. It is your images that are not what you think they are in terms of number of colors.
Yes, IM is amazing! I would never adoubt it. my question for now whould be:

How to count non-continuous blocks from a compressed image with transparent background?

A image might contains a lot of colors that might be counted differently with human eyes. How do I count the very similar color as 1 block for a transparent image? And also the transparent color need to be ignored, it can't be counted as block.

This image has only 2 colors and 2 blocks with human eyes:
Image

But acually it contains more than 2 colors and would be counted as more than 2 blocks, Is that possible to be solved?

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

Posted: 2020-03-11T01:13:06-07:00
by snibgo
I suggest you flatten against a colour that isn't already used, eg black in this case. Then there is no alpha, and the problem is simpler. "Connected-components" then finds the four areas.

Code: Select all

magick f.png -background Black -layers Flatten -alpha off -define connected-components:verbose=true -define connected-components:area-threshold=100 -connected-components 4 -auto-level c.png

Objects (id: bounding-box centroid area mean-color):
  0: 320x320+0+0 158.7,175.0 61539 srgb(0,0,0)
  88: 207x301+58+10 160.8,143.7 30933 srgb(204,51,51)
  787: 113x112+104+57 160.4,112.2 6938 srgb(147,36,36)
  1380: 62x61+130+83 160.2,113.0 2958 srgb(0,0,0)
Then your script can ignore areas that are black. This leaves two areas, the result you want.

Automatically finding a colour that isn't already used is a bit tricky.

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

Posted: 2020-03-11T01:28:53-07:00
by Peggy
snibgo wrote:
2020-03-11T01:13:06-07:00
I suggest you flatten against a colour that isn't already used, eg black in this case. Then there is no alpha, and the problem is simpler. "Connected-components" then finds the four areas.

Code: Select all

magick f.png -background Black -layers Flatten -alpha off -define connected-components:verbose=true -define connected-components:area-threshold=100 -connected-components 4 -auto-level c.png

Objects (id: bounding-box centroid area mean-color):
  0: 320x320+0+0 158.7,175.0 61539 srgb(0,0,0)
  88: 207x301+58+10 160.8,143.7 30933 srgb(204,51,51)
  787: 113x112+104+57 160.4,112.2 6938 srgb(147,36,36)
  1380: 62x61+130+83 160.2,113.0 2958 srgb(0,0,0)
Then your script can ignore areas that are black. This leaves two areas, the result you want.

Automatically finding a colour that isn't already used is a bit tricky.
That sounds working to me. Could I detect the image whether contains a specific color? So I could use the color to be background of the image. I'll keep working for this and report the result. Thank you.

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

Posted: 2020-03-11T02:31:28-07:00
by snibgo
Peggy wrote:Could I detect the image whether contains a specific color?
Your "red-drop" image has 8 bits/channel/pixel. So the colour #000100000000 which is very nearly black is guaranteed not to be in your image. For this to work, your IM can't be Q8. It must be Q16 or larger.

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

Posted: 2020-03-11T02:45:25-07:00
by Peggy
snibgo wrote:
2020-03-11T02:31:28-07:00
Peggy wrote:Could I detect the image whether contains a specific color?
Your "red-drop" image has 8 bits/channel/pixel. So the colour #000100000000 which is very nearly black is guaranteed not to be in your image. For this to work, your IM can't be Q8. It must be Q16 or larger.
Alright, thanks for the information. I'll have further study for this.

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

Posted: 2020-03-11T03:18:15-07:00
by Peggy
I've done testing for some images, the results are shown as blow:


Image 1:

Image
Image

Code: Select all

convert pen.png -background Black -layers Flatten -alpha off -define connected-components:verbose=true -define connected-components:area-threshold=100 -connected-components 4 -auto-level pen_ok.png

Objects (id: bounding-box centroid area mean-color):
  0: 512x512+0+0 248.5,258.3 209495 srgb(0,0,0)
  7: 303x303+209+4 358.7,157.2 18554 srgb(12,213,152)
  2417: 319x239+0+269 142.5,406.5 12687 srgb(12,213,152)
  263: 190x189+251+47 345.8,141.2 6054 srgb(0,0,0)
  513: 189x190+280+75 374.0,169.5 5882 srgb(0,0,0)
  2806: 70x126+49+325 85.3,387.7 2498 srgb(12,213,152)
  2811: 70x126+201+325 233.4,388.4 2444 srgb(12,213,152)
  85: 60x60+436+20 467.1,48.0 1694 srgb(0,0,0)
  2121: 53x53+228+235 250.2,265.1 1343 srgb(0,0,0)
  3848: 241x1+39+492 159.0,492.0 241 srgb(0,8,5)
  2794: 1x161+304+308 304.0,388.0 161 srgb(4,94,67)
  3322: 14x14+185+405 191.5,411.8 161 srgb(12,213,152)
  2793: 1x161+15+308 15.0,388.0 161 srgb(0,8,5)
  2795: 1x160+319+308 319.0,387.5 160 srgb(7,137,97)
  3308: 14x14+121+405 127.4,411.8 160 srgb(12,213,152)
  3315: 14x14+153+405 159.4,411.9 159 srgb(12,213,152)
  2403: 145x1+39+268 111.0,268.0 145 srgb(4,94,67)
  2563: 145x1+39+283 111.0,283.0 145 srgb(6,119,85)
13 objects are found. 7 objects expected.




Image 2:

Image
Image

Code: Select all

convert flower.png -background Black -layers Flatten -alpha off -define connected-components:verbose=true -define connected-components:area-threshold=100 -connected-components 4 -auto-level flower_ok.png
Objects (id: bounding-box centroid area mean-color):
  1541: 400x200+0+200 199.6,323.8 47460 srgb(0,0,0)
  0: 400x201+0+0 199.4,74.9 46894 srgb(0,0,0)
  12: 110x176+95+27 146.8,107.4 10569 srgb(6,158,219)
  813: 196x82+203+158 305.9,198.8 10541 srgb(209,176,42)
  788: 196x82+1+158 93.9,198.9 10521 srgb(77,140,64)
  21: 105x172+200+27 252.3,107.5 10519 srgb(0,106,154)
  1554: 104x170+200+201 252.3,290.0 10484 srgb(165,142,49)
  1551: 105x170+95+201 146.3,290.6 10468 srgb(150,201,61)
  1548: 57x34+139+201 167.3,217.8 466 srgb(0,0,0)
  953: 57x33+204+164 232.0,180.3 435 srgb(0,0,0)
  1605: 8x64+196+204 199.5,236.8 415 srgb(0,0,0)
  594: 8x64+196+130 199.5,162.7 414 srgb(0,0,0)
  1575: 55x32+205+202 231.2,216.9 413 srgb(0,0,0)
  964: 54x31+141+165 166.6,179.7 401 srgb(0,0,0)
Works like a charm! All 6 objects have found.




Image 3:

Image
Image

Code: Select all

convert tw.png -background Black -layers Flatten -alpha off -define connected-components:verbose=true -define connected-components:area-threshold=100 -connected-components 4 -auto-level tw_ok.png
Objects (id: bounding-box centroid area mean-color):
  0: 896x474+0+0 444.4,238.1 331700 srgb(255,255,255)
  125: 393x189+89+143 295.2,225.0 48204 srgb(254,108,0)
  35: 350x191+457+141 635.8,237.5 44651 srgb(254,108,0)
  56: 149x1+119+142 193.0,142.0 149 srgb(255,138,52)
4 objects found, 3 boject expected.




Image 4:

Image
Image

Code: Select all

convert birds.png -background Red -layers Flatten -alpha off -define connected-components:verbose=true -define connected-components:area-threshold=100 -define connected-components:mean-color=true -connected-components 4 -auto-level birds_ok.png
Objects (id: bounding-box centroid area mean-color):
  0: 800x800+0+0 399.6,398.8 629081 srgb(255,0,0)
  1801: 52x68+337+395 358.4,425.9 1367 srgb(0,0,0)
  4500: 51x68+710+699 739.8,735.0 1119 srgb(0,0,0)
  1856: 51x60+179+401 205.4,430.2 1006 srgb(0,0,0)
  11: 51x60+81+48 107.4,77.2 1006 srgb(0,0,0)
  2599: 37x61+600+499 614.9,526.4 906 srgb(0,0,0)
  2570: 76x45+495+498 531.3,520.7 842 srgb(0,0,0)
  375: 76x45+112+169 148.3,191.7 842 srgb(0,0,0)
  4326: 37x49+567+675 581.8,696.7 694 srgb(0,0,0)
  3574: 83x22+622+615 663.6,622.0 671 srgb(0,0,0)
  1361: 83x22+131+361 172.6,368.0 671 srgb(0,0,0)
  3213: 77x19+310+545 347.5,551.4 575 srgb(0,0,0)
  466: 70x37+202+177 233.7,190.9 543 srgb(0,0,0)
  1051: 48x31+242+213 263.5,228.2 457 srgb(0,0,0)
  3640: 25x25+563+617 572.6,629.5 219 srgb(0,0,0)
All 14 birds are found!




Image 5:

Image
Image

Code: Select all

convert mountain.png -background Red -layers Flatten -alpha off -define connected-components:verbose=true -define connected-components:area-threshold=100 -define connected-components:mean-color=true -connected-components 4 -auto-level mountain_ok.png
Objects (id: bounding-box centroid area mean-color):
  0: 512x512+0+0 255.1,246.5 210130 srgb(255,0,0)
  6: 466x208+23+152 262.8,297.5 33966 srgb(48,54,68)
  90: 88x160+216+172 252.9,248.0 8974 srgb(255,0,0)
  710: 93x84+301+262 347.0,309.6 3465 srgb(255,0,0)
  873: 88x73+53+273 102.3,316.4 3011 srgb(255,0,0)
  434: 31x81+166+235 181.9,282.3 1180 srgb(255,0,0)
  1430: 33x30+413+308 431.7,325.9 481 srgb(255,0,0)
  2110: 469x2+22+360 256.5,361.0 470 srgb(224,8,10)
  2106: 467x1+22+360 255.0,360.0 467 srgb(56,52,65)
4 Objects are found, 1 object expected.




Image 6:

Image
Image

Code: Select all

convert text.png -background Red -layers Flatten -alpha off -define connected-components:verbose=true -define connected-components:area-threshold=100 -define connected-components:mean-color=true -connected-components 4 -auto-level text_ok.png
Objects (id: bounding-box centroid area mean-color):
  0: 1027x327+0+0 517.1,163.9 286345 srgb(255,0,0)
  75: 113x157+395+102 457.7,178.7 7133 srgb(51,51,51)
  400: 84x90+168+115 209.7,159.7 5929 srgb(255,0,0)
  421: 83x89+410+115 451.0,159.2 5875 srgb(255,0,0)
  52: 113x149+154+102 199.6,166.0 5848 srgb(51,51,51)
  42: 115x117+758+101 813.4,158.0 5562 srgb(136,176,75)
  27: 115x117+633+101 687.7,158.0 5463 srgb(136,176,75)
  64: 92x113+285+102 328.1,148.7 3786 srgb(51,51,51)
  2: 56x147+519+68 548.3,138.7 2944 srgb(136,176,75)
  435: 83x36+648+115 689.2,135.2 2302 srgb(255,0,0)
  447: 83x36+774+115 814.8,135.2 2301 srgb(255,0,0)
  125: 39x112+587+103 597.5,149.8 1972 srgb(136,176,75)
  139: 1x147+153+104 153.0,177.0 147 srgb(153,26,26)
  147: 1x111+284+104 284.0,159.0 111 srgb(153,26,26)
  163: 1x111+586+104 586.0,159.0 111 srgb(165,133,56)
10 objects are found, 7 objects expected.





Image 7:

Image
Image
Image

Code: Select all

convert sun.png -background Red -layers Flatten -alpha off -define connected-components:verbose=true -define connected-components:area-threshold=100 -define connected-components:mean-color=true -connected-components 4 -auto-level sun_ok.png
Objects (id: bounding-box centroid area mean-color):
  0: 800x800+0+0 402.4,399.4 498481 srgb(255,0,0)
  1271: 374x357+204+222 389.9,400.1 93649 srgb(245,191,31)
  5954: 57x56+288+428 316.2,455.9 2492 srgb(243,152,30)
  7677: 102x121+506+557 557.4,616.4 2487 srgb(245,191,31)
  3345: 59x59+266+361 294.5,390.0 2483 srgb(255,255,255)
  2370: 58x59+402+309 429.7,338.5 2462 srgb(255,255,255)
  1616: 127x79+63+247 124.6,288.0 2390 srgb(245,191,31)
  3646: 59x50+443+368 471.9,392.5 2364 srgb(243,152,30)
  190: 91x117+522+116 567.2,174.4 2341 srgb(245,191,31)
  6381: 133x60+602+460 668.1,488.8 2339 srgb(245,191,31)
  9: 18x141+394+56 402.6,126.4 2327 srgb(245,191,31)
  137: 99x107+182+111 231.9,163.6 2288 srgb(245,191,31)
  8360: 26x134+375+608 387.3,674.7 2229 srgb(245,191,31)
  2102: 128x50+603+291 666.9,316.5 2191 srgb(245,191,31)
  7992: 89x99+156+576 201.2,625.5 2077 srgb(245,191,31)
  6174: 113x50+66+445 122.1,470.7 1935 srgb(245,191,31)
  8250: 55x104+260+598 287.0,649.8 1839 srgb(245,191,31)
  3809: 92x27+85+375 130.8,386.8 1512 srgb(245,191,31)
  7364: 77x58+574+527 612.3,555.5 1460 srgb(245,191,31)
  159: 34x82+305+113 319.8,153.8 1377 srgb(245,191,31)
  7303: 72x55+138+520 174.3,548.0 1367 srgb(245,191,31)
  8190: 43x79+445+591 466.8,629.7 1367 srgb(245,191,31)
  1135: 67x58+161+209 193.7,238.5 1317 srgb(245,191,31)
  4512: 82x16+615+394 655.5,401.5 1276 srgb(245,191,31)
  364: 40x73+460+135 479.4,171.3 1257 srgb(245,191,31)
  4176: 70x39+347+385 385.9,410.1 1053 srgb(44,35,13)
  1516: 54x43+577+238 603.4,259.2 998 srgb(245,191,31)
  2819: 20x20+423+328 432.6,337.6 320 srgb(44,35,13)
  4030: 20x21+286+382 295.8,392.4 314 srgb(44,35,13)
All 28 objects are found!!





The result for some imags are really awesome. There are some unexpected result , I think it beacuse of some color are difficult to identify with human eyes, but it do exist. Any idea for improving this? I feel so close to the answer, just need one more step. Would that be better to change my IM from Q8 to Q16?

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

Posted: 2020-03-11T03:59:56-07:00
by snibgo
Your images have anti-aliasing between solid colours. I have already showed you a command to remove anti-aliasing.

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

Posted: 2020-03-11T04:17:07-07:00
by Peggy
snibgo wrote:
2020-03-11T03:59:56-07:00
Your images have anti-aliasing between solid colours. I have already showed you a command to remove anti-aliasing.
Do you mean this command?

Code: Select all

magick +antialias msvg:in.svg out.png
The images I provided are png not svg, does it still work? I added +antialias to my command, the result is still incorrect. According to https://www.imagemagick.org/Usage/antialiasing/ I probably need different args for different image, right?