convert black-white picture to zero-one matrix

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?".

convert black-white picture to zero-one matrix

Postby waldfee » 2011-03-30T09:37:20+00:00

Hello everbody.
Today I started using imagemagick. I want to convert a black-white picture to a zero-one matrix.
I have been searching for an answer for hours. I saw lots of interesting thinks, but not the solution for my problem.

Can anyone tell me how I can do that or, when I have been blind, where I can find it?

Thanks a lot and kind regards
Waldfee
waldfee
 
Posts: 2
Joined: 2011-03-30T09:28:13+00:00

Re: convert black-white picture to zero-one matrix

Postby fmw42 » 2011-03-30T10:28:47+00:00

try one of these or one of the NetPBM ascii formats.


convert rose: -threshold 50% -depth 1 -fx "debug(u)" null:

convert rose: -threshold 50% -depth 1 txt:
User avatar
fmw42
 
Posts: 15004
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: convert black-white picture to zero-one matrix

Postby waldfee » 2011-04-13T12:55:08+00:00

Sorry for answering that late! I am still a student and there were other things to do :(

The last time, I checked both variants but did not know how to handle 'rose:'. Well, today I tryed it again, after some hours (and some other ways to try to solve the problem) I found out how to handle rose: :shock: ...

Anyway, the second way helps a looot. It gives me the coordinates (x,y), the mix of the colour and the name of the colour. If there were a possibility to convert the output in a way, that there are only the coordinates and the colours, or even better, black=1 and white=0, it would be the dot of the 'i' ;)

Again, thanks a lot!!!
waldfee
 
Posts: 2
Joined: 2011-03-30T09:28:13+00:00

Re: convert black-white picture to zero-one matrix

Postby fmw42 » 2011-04-13T13:23:54+00:00

this works to print the coords and black or white


convert rose: -threshold 50% -depth 1 txt: | sed -n 's/^\(.*\):.*[ ]\(.*\)$/\1 \2/p'


and this works to print the coords and 0 or 1


convert rose: -threshold 50% -depth 1 txt: | sed -n 's/^\(.*\):.*[ ]\(.*\)$/\1 \2/p' | sed 's/black/0/g' | sed 's/white/1/g'

or this


convert rose: -threshold 50% -depth 1 txt: | sed 's/^\(.*\):.*[ ]\(.*\)$/\1 \2/g; s/black/0/g; s/white/1/g'
User avatar
fmw42
 
Posts: 15004
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: convert black-white picture to zero-one matrix

Postby anthony » 2011-04-13T19:19:16+00:00

fmw42 wrote:try one of these or one of the NetPBM ascii formats.


NOTE IM does not output ascii PbmPlus images row-by-row, but pnmnoraw command does!

Code: Select all
convert lib/dragon_face.xbm pbm: | pnmnoraw
P1
32 32
00000000000000000000000000000000
00000000000000000010000011100000
00000000010000000110001111100000
00000000001110001110111111100000
00000000111111101111111111000000
00000000111111100111111111100000
00000000111111111011111011000000
00000001111110111111111111000000
00000001110000111111001011000000
00000000100001101100110011000000
00000000000011011110010110000000
00000000000111111100100111000000
00000000001111110010011100000000
00000000001111111001111110000000
00000000001101111111100110000000
00000000011110011101001001000000
00000001111111111010010001000000
00000000001101010111100101100000
00000000011111110111111001100000
00000000111111111110110010100000
00000000000111110101111000110000
00000000100001111011111000110000
00000000100000011111111100110000
00000000000000001111111101100000
00000001000000001100011100000000
00000000000000001101100100000000
00001010000000001101000000000000
00000101000000001101011000000000
00000001000000000011100000000000
00000000111000000000000000000000
00000000001011101010000000000000
00000000000010010100000000000000


As you can see PBM file format has no need of spaces, so you only need to strip the first two lines to get your array ;-)

ASIDE: Image follows the XBM and PBM bitmap convention of black=1 which you wanted!
Internally of course it has black=0 as you have seen. So it is negating the images as it reads and writes bitmap formats ;-)

Think of it as "Magick"
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
http://www.imagemagick.org/Usage/
User avatar
anthony
 
Posts: 8732
Joined: 2004-05-31T19:27:03+00:00
Location: Brisbane, Australia

Re: convert black-white picture to zero-one matrix

Postby fmw42 » 2011-04-13T21:27:53+00:00

NOTE IM does not output ascii PbmPlus images row-by-row, but pnmnoraw command does!


This is very useful comment about pnmoraw, Anthony. Something I never knew you could do and have had to work around it in the past.

Thanks for pointing this out.

Fred
User avatar
fmw42
 
Posts: 15004
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: convert black-white picture to zero-one matrix

Postby anthony » 2011-04-14T00:23:26+00:00

I know about it because I was the one to release the PbmPlus intermediate release in 1995.

I noted this in IM Examples, Formats, NetPbm/PbmPlus With examples!
http://www.imagemagick.org/Usage/formats/#netpbm
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
http://www.imagemagick.org/Usage/
User avatar
anthony
 
Posts: 8732
Joined: 2004-05-31T19:27:03+00:00
Location: Brisbane, Australia

Re: convert black-white picture to zero-one matrix

Postby fmw42 » 2011-04-14T09:38:12+00:00

anthony wrote:I know about it because I was the one to release the PbmPlus intermediate release in 1995.

I noted this in IM Examples, Formats, NetPbm/PbmPlus With examples!
http://www.imagemagick.org/Usage/formats/#netpbm



I see from your example that it also works for grayscale or any given channel. Great!

Fred
User avatar
fmw42
 
Posts: 15004
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA


Return to Users

Who is online

Users browsing this forum: Bing [Bot], dlemstra, Google Adsense [Bot] and 26 guests