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
convert black-white picture to zero-one matrix
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: convert black-white picture to zero-one matrix
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:
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
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: ...
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!!!
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: ...
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!!!
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: convert black-white picture to zero-one matrix
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'
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'
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: convert black-white picture to zero-one matrix
NOTE IM does not output ascii PbmPlus images row-by-row, but pnmnoraw command does!fmw42 wrote:try one of these or one of the NetPBM ascii formats.
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
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
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: convert black-white picture to zero-one matrix
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.NOTE IM does not output ascii PbmPlus images row-by-row, but pnmnoraw command does!
Thanks for pointing this out.
Fred
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: convert black-white picture to zero-one matrix
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 noted this in IM Examples, Formats, NetPbm/PbmPlus With examples!
http://www.imagemagick.org/Usage/formats/#netpbm
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: convert black-white picture to zero-one matrix
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