Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
XP is just a number
 
PerlMonks  

Re^4: Perl Image Analysis

by tonyc (Pilgrim)
on Oct 06, 2006 at 01:10 UTC ( #576603=note: print w/ replies, xml ) Need Help??


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

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 } }


Comment on Re^4: Perl Image Analysis
Select or Download Code
Re^5: Perl Image Analysis
by lparsons42 (Novice) on Oct 06, 2006 at 18:58 UTC
    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.
        Yes, I see now what you mean. I didn't expect that jpg conversion would hurt a greyscale image like that.
        I then retried this using a tiff file instead. I found that the GD library can read jpgs and pretty much only jpgs. It errors out on PNG, GIF, and TIFF.
        Looks like I'm going back to using the Imager module so I can read TIFFs.
        Thanks again all!
      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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2014-04-21 00:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (489 votes), past polls