Separating, then conversion to 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?".
Post Reply
hughbar
Posts: 2
Joined: 2015-04-06T03:33:10-07:00
Authentication code: 6789

Separating, then conversion to matrix

Post by hughbar »

Hi folks

I want to make a matrix of ones and zeros out of some red bits of an image, so:

Code: Select all

convert 5446.png -separate -channel Red out.txt
Which give me a file containing lines like this:

Code: Select all

146,255: (152,152,152)  #989898  rgb(152,152,152)
147,255: (210,210,210)  #D2D2D2  rgb(210,210,210)
148,255: (175,175,175)  #AFAFAF  rgb(175,175,175)
149,255: (175,175,175)  #AFAFAF  rgb(175,175,175)
150,255: (175,175,175)  #AFAFAF  rgb(175,175,175)
151,255: (175,175,175)  #AFAFAF  rgb(175,175,175)
152,255: (175,175,175)  #AFAFAF  rgb(175,175,175)
This looks promising and I'm ready to do some scripting, but:
a. I'm unsure whether this is a useful format
b. How to interpret the line detail

I'm not used to graphics formats, so this may be a fairly silly question?

Best regards Hugh
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Separating, then conversion to matrix

Post by snibgo »

IM processes in the order you specify, so ...

Code: Select all

convert 5446.png -separate -channel Red out.txt
... will first separate the three channels. This creates three images. Then you select just the red channel from each of the three images for further processing. But you don't do any further processing, so "-channel Red" does nothing.

Instead, you could:

Code: Select all

convert 5446.png -channel Red -separate out.txt
The will select just the red channel, so "-separate" will give one image, which is the red channel.
snibgo's IM pages: im.snibgo.com
hughbar
Posts: 2
Joined: 2015-04-06T03:33:10-07:00
Authentication code: 6789

Re: Separating, then conversion to matrix

Post by hughbar »

Thanks, there was some extra confusion, because I didn't realise that the image was 'small' [256x 256], so now I can read the first column as x,y [? I hope!] coordinates and the rest as 'colour'.

My ugly way of doing this is currently:

Code: Select all

#!/usr/bin/perl -w
# convert separated channel into array
# convert 5446.png -channel Red -separate out.txt

open my $txt, '<', 'out.txt';
my @lines;
while (<$txt>) {
    next if (/^#/);    #don't process comments

    # 234,3: (220,220,220)  #DCDCDC  gainsboro
    m/^(\d+)\,(\d+)\:/;
    my $x = $1;
    my $y = $2;
    if (/grey/) {
        $lines[$x] .= "1,";
    }
    else {
        $lines[$x] .= "0,";
    }
}

# now print the lines
foreach my $line (@lines) {
    $lines =~ s/,$//;
    print $line . "\n";

}
exit 0;
This could be a lot better, command line parameter for choosing the colour I want to 'decide' on etc. but it's a good start. Thanks!
Post Reply