Detect coordinates of "colored-blocks" among white baground?

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
gadapchetvoi
Posts: 10
Joined: 2013-03-23T10:12:38-07:00
Authentication code: 6789

Detect coordinates of "colored-blocks" among white baground?

Post by gadapchetvoi »

My first question at imageMagick forum was honestly answered by very kind experts.
I hope my 2nd one will be the same. And once again, please correct me if I say something stupid :?

My problem was a bit complicated :

I have some colored texts & graphics on white background, like islands over the sea. (Let's suppose that each group of non-white pixels stands one by one - every pix is next to each other is an "ISLAND", and the white background is the SEA)

I need to get all the pixels' coordinates (x,y) of each ISLAND that exisiting on the SEA. Some thing like :

Code: Select all

#island 1 : position(20,30) width:100 height:120 contains 999 pixels :
20,30 : rgba(100,120,220, 15)
...
120,150 : rgba(100,120,221, 20)

 #island 2 : position(150,330) width:100 height:120 contains 1200 pixels :
20,30 : rgba(100,120,220, 15)
...
120,150 : rgba(100,120,221, 20)

.... so on
I hope I express my situation well. Please correct me or ask me anything to clarify the question.
Thank you for all suggestions !

PS: I'm working in Windows evironment, and I hope you can help me with cmd command ! Thanks !
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Detect coordinates of "colored-blocks" among white bagro

Post by snibgo »

That's complex. Fred probably has a script to do the job: http://www.fmwconcepts.com/imagemagick/index.html . But you'll need to install Cygwin to run the script, or modify it for Windows.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Detect coordinates of "colored-blocks" among white bagro

Post by fmw42 »

Do you want just the upper left corner and bounds of the region or do you want to see the coordinates of every pixel that is not white?

The latter can be done by simply using txt:- and filtering out all white pixels using something like grep -v or using sed.

The former could be done using my script, separate, to cut out each region. However, the script expects white regions on a black background. So you would have to negate your image first. Also it is a bash shell script and so you would need cygwin to run it on Windows. It may also need to be modified slightly to keep the virtual-canvas, so that you know the region size and offset. I think currently it throws that away using +repage.
gadapchetvoi
Posts: 10
Joined: 2013-03-23T10:12:38-07:00
Authentication code: 6789

Re: Detect coordinates of "colored-blocks" among white bagro

Post by gadapchetvoi »

Thank you snibgo for the instruction & link.

fmw42 wrote:Do you want just the upper left corner and bounds of the region or do you want to see the coordinates of every pixel that is not white?
Yes, I need the coordinate of every pixel.
fmw42 wrote: The latter can be done by simply using txt:- and filtering out all white pixels using something like grep -v or using sed.

The former could be done using my script, separate, to cut out each region. However, the script expects white regions on a black background. So you would have to negate your image first. Also it is a bash shell script and so you would need cygwin to run it on Windows. It may also need to be modified slightly to keep the virtual-canvas, so that you know the region size and offset. I think currently it throws that away using +repage.
I will spend times to read about Cygwin and your script for separation. I have to confess that I'm brand new to Imagemagick, and all what I've achieved so far have been made by JScript (similar to VBScript). But my code is extra slow (~1 mins to deal with a 1500x2300 png file), that's why I'm expecting a faster method from imageMagick.

I've achived the same result as your "separate-script" (by JScript) like this before :
-Replace all white pixels by transparent
-Export the colored-pixels' coordinate to a text file (as you suggested above)
- Read the text file & scan through all pixels
- Check each pixel to see if it's "next-door neighbour" of any other pixel. If true, push the new pixel to existing "ISLAND" of the neighbour pixel. Else, create a new "ISLAND".

Please let me know if your algorithm was the same as mine ( :D )
But, at all, thank you for your help very much !
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Detect coordinates of "colored-blocks" among white bagro

Post by fmw42 »

Please let me know if your algorithm was the same as mine ( )
But, at all, thank you for your help very much !
Not really the same. And it may not be the fastest way. It makes a new image for each section.

But if it is modified so that it does not remove the virtual canvas, it can be made to tell you the bounding box coordinates. Then it could be modified further to tell you how many non-white pixels are in each bounding box. But it does not do any of the latter right now. See http://www.fmwconcepts.com/imagemagick/ ... /index.php or http://www.fmwconcepts.com/imagemagick/ ... /index.php (though this latter one is not designed to find small regions)

If you want every pixel but the background (say within XX% of white), you can do the following:

convert image -fuzz XX% -transparent white sparse-color:-

That will list out x,y,color for every non-transparent pixel. But it is new to 6.8.3-10.

Otherwise, the slower method is

convert image txt:- | grep -v "white"

It will not tell you each separate region without your further processing. But my script could be modified to just find each rectangular area and tell you the bounding box coordinates, and compute how many pixels are in each section.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Detect coordinates of "colored-blocks" among white bagro

Post by anthony »

I would NOT rely on sparse-color: the coder is VERY new and it is much more likely to be rolled into txt:

In fact I would have prefered it to have been part of txt: with output defines controling exactly what output is produced.

Never heard much more from that discussion.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
gadapchetvoi
Posts: 10
Joined: 2013-03-23T10:12:38-07:00
Authentication code: 6789

Re: Detect coordinates of "colored-blocks" among white bagro

Post by gadapchetvoi »

fmw42 wrote: convert image -fuzz XX% -transparent white sparse-color:-
I've just tried this command with 2 png files. 1st one ok, the 2nd png give me this error :

Code: Select all

convert.exe : no decode delegate for this image format '-' @ error/constitute.c/ReadImage/550.
Can you please tell me somthing about this ?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Detect coordinates of "colored-blocks" among white bagro

Post by fmw42 »

the minus means send to standard out. You can leave the minus off. Try that and see if both work. If not, post a link to the file that does not work, so others can test with it.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Detect coordinates of "colored-blocks" among white bagro

Post by anthony »

version? You probably don't even have the sparse-color: codec!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
gadapchetvoi
Posts: 10
Joined: 2013-03-23T10:12:38-07:00
Authentication code: 6789

Re: Detect coordinates of "colored-blocks" among white bagro

Post by gadapchetvoi »

fmw42 wrote:the minus means send to standard out. You can leave the minus off. Try that and see if both work. If not, post a link to the file that does not work, so others can test with it.
I tried these commands, but nothing was responded :

Code: Select all

convert ZA.png -fuzz 80% -transparent white sparse-color:-
convert ZA.png -fuzz 80% -transparent white sparse-color:
So I think I need too try this :

Code: Select all

convert ZA.png -fuzz 80% -transparent white sparse-color:- abc.txt
The output was still sent to abc.txt but the error message showed like above too!
I tested with IM version 6.8.3-10 (newest as I know)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Detect coordinates of "colored-blocks" among white bagro

Post by fmw42 »

It works for me on IM 6.8.3.10 and 6.8.4.0 on Mac OSX Snow Leopard

convert rose: -fuzz 80% -transparent red sparse-color:
61,11,white 62,11,srgba(255,245,255,1) 63,11,srgba(255,248,255,1) 64,11,srgba(255,255,254,1) 65,11,srgba(255,254,251,1) 66,11,srgba(255,249,253,1) 61,12,srgba(255,255,254,1) 62,12,srgba(252,255,255,1) 63,12,srgba(253,253,254,1) 62,13,srgba(252,255,255,1) 63,13,srgba(250,255,255,1) 62,14,srgba(255,253,255,1) 63,14,srgba(253,254,255,1) 64,14,srgba(255,252,254,1) 65,14,srgba(253,255,248,1) 66,14,srgba(255,255,250,1) 62,15,white 63,15,srgba(255,254,255,1) 64,15,srgba(251,255,254,1) 66,15,srgba(255,255,249,1) 62,16,white 63,16,white 13,37,srgba(250,251,253,1) 14,37,white 12,38,white 13,38,white 14,38,srgba(255,255,254,1) 12,39,srgba(239,251,252,1) 13,39,srgba(250,255,252,1) 22,39,srgba(255,252,251,1) 23,39,white 24,39,srgba(255,255,247,1) 12,40,white 13,40,srgba(255,255,252,1) 21,40,srgba(255,254,252,1) 22,40,srgba(255,255,253,1) 23,40,srgba(253,255,254,1) 24,40,white 25,40,white 11,41,white 12,41,white 21,41,white 22,41,srgba(255,255,253,1) 23,41,srgba(254,254,252,1) 24,41,srgba(254,254,252,1) 25,41,srgba(255,253,255,1) 26,41,white 27,41,srgba(251,255,247,1) 11,42,srgba(254,255,255,1) 12,42,srgba(255,255,254,1) 19,42,srgba(255,250,255,1) 20,42,white 21,42,srgba(255,255,253,1) 22,42,srgba(254,254,252,1) 23,42,srgba(254,254,252,1) 24,42,srgba(254,254,252,1) 25,42,srgba(255,253,250,1) 26,42,srgba(249,255,255,1) 27,42,srgba(250,255,255,1) 28,42,white 11,43,srgba(251,254,255,1) 12,43,srgba(255,255,253,1) 19,43,srgba(255,253,255,1) 20,43,srgba(255,255,252,1) 21,43,srgba(254,254,252,1) 22,43,srgba(254,254,252,1) 23,43,srgba(254,254,252,1) 24,43,srgba(254,254,252,1) 25,43,srgba(250,255,253,1) 26,43,srgba(255,253,255,1) 27,43,srgba(255,248,255,1) 28,43,srgba(255,254,254,1) 29,43,white 11,44,srgba(254,255,255,1) 12,44,srgba(255,255,252,1) 18,44,white 19,44,srgba(254,255,252,1) 20,44,srgba(254,255,250,1) 21,44,srgba(254,254,252,1) 22,44,srgba(254,254,252,1) 23,44,srgba(254,254,252,1) 24,44,srgba(254,254,252,1) 25,44,srgba(254,253,255,1) 26,44,srgba(251,255,252,1) 27,44,srgba(249,255,245,1) 28,44,srgba(253,255,250,1) 29,44,srgba(255,254,255,1) 30,44,white 31,44,white 32,44,srgba(255,255,254,1) 11,45,srgba(245,250,250,1) 18,45,srgba(254,250,255,1) 19,45,srgba(253,254,254,1) 20,45,srgba(253,254,254,1) 21,45,srgba(254,254,254,1) 22,45,srgba(255,254,252,1) 23,45,srgba(255,253,251,1) 24,45,srgba(255,253,249,1) 25,45,srgba(255,252,252,1) 26,45,srgba(255,253,253,1) 27,45,srgba(254,254,252,1) 30,45,srgba(254,250,251,1) 31,45,srgba(254,255,253,1) 32,45,srgba(253,255,255,1) 33,45,srgba(255,255,252,1)

If that does not work for you be sure your IM version is at least 6.8.3.10. If your version is appropriate, then you need to report this on the bugs forum. Perhaps it did not get into the Windows build.


Otherwise, try the slower method

convert rose: -fuzz 80% -transparent red -background black -alpha background txt: | grep -v "none"

# ImageMagick pixel enumeration: 70,46,255,srgba
61,11: (255,255,255,255) #FFFFFF white
62,11: (255,245,255,255) #FFF5FF srgba(255,245,255,1)
63,11: (255,248,255,255) #FFF8FF srgba(255,248,255,1)
64,11: (255,255,254,255) #FFFFFE srgba(255,255,254,1)
65,11: (255,254,251,255) #FFFEFB srgba(255,254,251,1)
66,11: (255,249,253,255) #FFF9FD srgba(255,249,253,1)
61,12: (255,255,254,255) #FFFFFE srgba(255,255,254,1)
62,12: (252,255,255,255) #FCFFFF srgba(252,255,255,1)
63,12: (253,253,254,255) #FDFDFE srgba(253,253,254,1)
62,13: (252,255,255,255) #FCFFFF srgba(252,255,255,1)
63,13: (250,255,255,255) #FAFFFF srgba(250,255,255,1)
62,14: (255,253,255,255) #FFFDFF srgba(255,253,255,1)
63,14: (253,254,255,255) #FDFEFF srgba(253,254,255,1)
64,14: (255,252,254,255) #FFFCFE srgba(255,252,254,1)
65,14: (253,255,248,255) #FDFFF8 srgba(253,255,248,1)
66,14: (255,255,250,255) #FFFFFA srgba(255,255,250,1)
62,15: (255,255,255,255) #FFFFFF white
63,15: (255,254,255,255) #FFFEFF srgba(255,254,255,1)
64,15: (251,255,254,255) #FBFFFE srgba(251,255,254,1)
66,15: (255,255,249,255) #FFFFF9 srgba(255,255,249,1)
...
gadapchetvoi
Posts: 10
Joined: 2013-03-23T10:12:38-07:00
Authentication code: 6789

Re: Detect coordinates of "colored-blocks" among white bagro

Post by gadapchetvoi »

"sparse-color:" is ok for some picture, but respond nothing for some, like this picture :

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

Re: Detect coordinates of "colored-blocks" among white bagro

Post by fmw42 »

gadapchetvoi wrote:"sparse-color:" is ok for some picture, but respond nothing for some, like this picture :

Image
That is because every pixel has some partial transparency. Sparse-color: will only report perfectly opaque pixels.
gadapchetvoi
Posts: 10
Joined: 2013-03-23T10:12:38-07:00
Authentication code: 6789

Re: Detect coordinates of "colored-blocks" among white bagro

Post by gadapchetvoi »

fmw42 wrote:That is because every pixel has some partial transparency. Sparse-color: will only report perfectly opaque pixels.
Oh, so it's why it responded nothing for this picture. I've thought it's a bug. Thanks for figuring it out for me !
Post Reply