Page 1 of 1

Separating, then conversion to matrix

Posted: 2015-04-06T03:39:47-07:00
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

Re: Separating, then conversion to matrix

Posted: 2015-04-06T05:14:57-07:00
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.

Re: Separating, then conversion to matrix

Posted: 2015-04-07T01:55:49-07:00
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!