Page 1 of 1
convert black-white picture to zero-one matrix
Posted: 2011-03-30T09:37:20-07:00
by waldfee
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
Re: convert black-white picture to zero-one matrix
Posted: 2011-03-30T10:28:47-07:00
by fmw42
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:
Re: convert black-white picture to zero-one matrix
Posted: 2011-04-13T12:55:08-07:00
by waldfee
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:
![Shocked :shock:](./images/smilies/icon_eek.gif)
...
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!!!
Re: convert black-white picture to zero-one matrix
Posted: 2011-04-13T13:23:54-07:00
by fmw42
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'
Re: convert black-white picture to zero-one matrix
Posted: 2011-04-13T19:19:16-07:00
by anthony
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"
Re: convert black-white picture to zero-one matrix
Posted: 2011-04-13T21:27:53-07:00
by fmw42
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
Re: convert black-white picture to zero-one matrix
Posted: 2011-04-14T00:23:26-07:00
by anthony
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
Re: convert black-white picture to zero-one matrix
Posted: 2011-04-14T09:38:12-07:00
by fmw42
I see from your example that it also works for grayscale or any given channel. Great!
Fred