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

Re: Perl Image Analysis

by lparsons42 (Novice)
on Oct 04, 2006 at 21:14 UTC ( [id://576414]=note: print w/replies, xml ) Need Help??


in reply to Perl Image Analysis

Ok, that is a good start. I set up the Imager module, and was able to get scalar values for a pixel. Now I just need to work on figuring out what to do with them.
Is there a way that any of these methods could give me just a greyscale intensity, rather than RGB values? I don't have any color data on these images, so the RGB values are kinda obfuscating the interpretation.
Not that I'm entirely opposed to obfuscation, being as I am writing in Perl, after all... :)

I submitted this thread while waiting for my new member email to come through. I suspect it may have been holed up for a while by our foolish spam filter...

Replies are listed 'Best First'.
Re^2: Perl Image Analysis
by tonyc (Pilgrim) on Oct 05, 2006 at 02:07 UTC
    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.
      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 } }
        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:
        http://en.wikipedia.org/wiki/YUV

        Anders, Norway

Re^2: Perl Image Analysis
by Koosemose (Pilgrim) on Oct 04, 2006 at 23:30 UTC

    I may be wrong, but I would expect any of the modules mentioned to return just an intensity in the case of a true grayscale image, I could be wrong, or it could be a case of the image technically being a color image that just so happens to only contain grays. Nonetheless, if all else fails, just use any one of the values, ignore the others, they should be the same.

    Well, just did some testing, and at least in regards to Image::Magick it still returns RGBA values, I can't speak for the others haven't had much experience with them, so at least in the case of Image::Magick, I would suggest just dumping the spare values.

    Just Another Perl Alchemist
Re^2: Perl Image Analysis
by zentara (Archbishop) on Oct 05, 2006 at 12:00 UTC
    <#!/usr/bin/perl use warnings; use strict; use Imager; my $file = shift; my $img = Imager->new(); $img->open(file=>$file) or die $img->errstr(); my $newimg = $img->convert(preset=>'grey'); $newimg->write(file=>'gray.jpg');
    What I have found with Imager, is that is has some very powerful, but basic tools. If you look at them closely, you probably can figure out a sequence of operations to acheive what you want. Look at "perldoc Imager" to get a list of it's sub modules, check out Imager::Filters and it's $img->difference method.

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-04-18 10:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found