Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: Perl Image Analysis

by lparsons42 (Novice)
on Oct 05, 2006 at 13:49 UTC ( [id://576534]=note: print w/replies, xml ) Need Help??


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

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 :)

Replies are listed 'Best First'.
Re^4: Perl Image Analysis
by tonyc (Friar) on Oct 06, 2006 at 01:10 UTC
    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!

        I loaded the image using Opera and used it's zoom function to blow the image up 1000%. It is then fairly obvious that the "white" area immediately surrounding the black squares is full of slightly off-white pixels, including #f2f2f2, #f1f1f1, #ededed, #f6f6f6, #f5f5f5, etc. This is probably an artifact of the JPEG 'lossy' compression system, though it could also be an artifact of the drawing program you used to constuct it.

        Update: Actually, looking at it again (looking at my lcd from an oblique angle), the black isn't entirely black either. bThere are several pixels in both squares that are variously #0f0f0f, #030303, #0c0c0c etc.

        Redo your image using the non-lossy .png or .tiff formats. If the artifacts go away, it was the JPEG compression biting you. If they don't, it is your drawing program that may be attempting to antialias something. Either look for a configuration option to turn that off or use a 'dumb' graphics editor (like mspaint.exe) that doesn't attempt to do anything clever :) (The had to be some advantage to paint didn't there :)

        Also, be wary of what program you use to zoom the image. For example, Ifranview will resample the image as you zoom it unless you explicitly turn that option (view->properties->view->Use resample) off. This results in the edges of the squares getting antialiased, and the corners getting rounded as the image is zoomed.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
        Don't forget about (lossy) jpeg compression. You'd be better off using GIF or TIFF for this experiment.
        identify -verbose  identify -verbose artificial_black_n_white.jpg


        grep
        One dead unjugged rabbit fish later
Re^4: Perl Image Analysis
by rustyjuggler (Initiate) on Oct 06, 2006 at 06:41 UTC
    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

Log In?
Username:
Password:

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

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

    No recent polls found