Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re^2: Perl Image Analysis

by tonyc (Friar)
on Oct 05, 2006 at 02:07 UTC ( [id://576457]=note: print w/replies, xml ) Need Help??

in reply to Re: Perl Image Analysis
in thread Perl Image Analysis

You can get just the first channel (your grey value) with the getsamples() method:
# get all the samples for row $y my @samples = $image->getsamples(y => $y, channels => [ 0 ]);
If your image does have only one channel (check $image->getchannels) the channels argument is unneeded so you have:
my @samples = $image->getsamples(y => $y);
You can also get the samples as a string of bytes:
my $samples = $image->getsamples(y => $y);
You don't mention the precision of your data, but Imager currently always reads TIFFs at 8-bits/sample, even if the TIFF has higher precision.

Replies are listed 'Best First'.
Re^3: Perl Image Analysis
by lparsons42 (Novice) on Oct 05, 2006 at 13:49 UTC
    Yes, this is looking more promising. The triplet values were not as useful as hoped for. I tried to use the getsamples() method to sample just one pixel, however, and the results were like:
    ǡyʾk~rw U=9dyТI*\ZGp
    I'm not sure how to compare those values to each other... My code to do this was as follows:
    ### #it appears there may actually be a way to read the grey channel #using the "getsamples" routine my $ycoord = 5; my $xcoord = 5; my $sample = $img->getsamples(y => $ycoord, x => $xcoord); print "we sampled 5,5 to have a value of $sample\n";
    I tried sampling two different points that I knew to be quite different by this method, which is how I got those two odd strings above. I'll have to look further into the Perldocs for Imager, it appears :)
      You're running into 2 problems here:
      • getsamples() uses the context you call it in to decide whether to produce samples packed into a string or a list of samples
      • if you don't supply a width getsamples() returns samples from the given x position all the way to the right side of the image.
      So to get just the sample for one pixel as an integer you'd do:
      my ($sample) = $image->getsamples(x => $x, y => $y, width => 1);
      But if you're looking for speed you'll probably find that working a row at a time is faster:
      for my $y ( 0 .. $image->getheight() - 1 ) { for my $sample ($image->getsamples(y => $y)) { # do something with the sample } }
        Ok, I used that to determine the intensity of a single pixel in greyscale. However, when I tried a simple test of it, I got unexpected results.
        I made a test image, that was 24 pixels wide by 12 pixels tall. This is a jpeg that I made in kolourpaint and set to greyscale. It has a white background and I used only black color on it. I made two boxes on this image, which are each 8x with 2 white pixels all the way around and 4 white pixels in the center between the two boxes.
        The truly curious can see this da vinci - quality 834 byte jpeg at this site
        However, when I analyze this file, I get varied results for what should not be varied intensities. Specifically, the spots (5,5) and (5,17) should both be intensity 0 (since they are 100% black). However, this is not the case. Instead, (5,5) registers 15 and (5,17) registers 0.
        Any insight as to why this is would be great!
      If you have the RGB-values you can easily convert them to grayscale through the formula on Wikipedia where Y (luminance) is the grayscale intensity:

      Y = 0.299 * R + 0.587 * G + 0.114 * B

      Here is the link:

      Anders, Norway

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://576457]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2024-05-22 01:55 GMT
Find Nodes?
    Voting Booth?

    No recent polls found