php how to measure area of irregular shapes from a picture

Discussion in 'PHP' started by coleymr, Jun 21, 2010.

  1. #1
    Hi,
    For a forthcomming project, I require a piece of software that can measure the area of an irregular shape from a picture.

    This needs to work in a LAMP environment.

    I would be most grateful for any help.

    Thanks
    Mark
     
    coleymr, Jun 21, 2010 IP
  2. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #2
    What measurements do you want the area to be in ? pixels?

    Also have you got an example image?
     
    stephan2307, Jun 21, 2010 IP
  3. coleymr

    coleymr Guest

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Here are some sample pictures with shapes that I need find the area for

    Thanks
     

    Attached Files:

    coleymr, Jun 22, 2010 IP
  4. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #4
    thanks. In what measurements do you need the area in? the number of pixels or what?
     
    stephan2307, Jun 22, 2010 IP
  5. flexdex

    flexdex Peon

    Messages:
    104
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Do you need to match shapes or just getting the bounding box?
     
    flexdex, Jun 22, 2010 IP
  6. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #6
    This question has been bothering me so I have been playing around. I picked the second attached image and produced the following code.
    
    <?php
    
        $im = new imagick( 'http://forums.digitalpoint.com/attachment.php?attachmentid=41098&d=1277195237' );
        $im->setImageFormat( "png" );
        
        $x = 1;
        $y = 1;
    
        // this will be white (background)
        $target = $im->getImagePixelColor($x, $y);
        
        // fill the background color red
        $im->floodfillPaintImage ('#ff0000',20000,$target,$x,$y,false); 
    
        // get width and height of image
        $width = $im->getImageWidth();
        $height = $im->getImageHeight();
          
        // counter to count white pixels
        $counter = 0;
        
        // loop horizontally through the image
        for ($w =10; $w < $width; $w++) {
            // loop vertically through the image
            for ($h =10; $h < $height; $h++) {
                // get color at for the current position  and compare it with white
                if ($target == $im->getImagePixelColor($w,$h)) {
                    // if the pixel is white increment the counter
                    $counter++;
                }
    
            }
        }
    echo ($width*$height)-$counter;  
    ?>
    
    PHP:

    Now I have no idea how exact it is and even if it is correct is (really didn't want to count the pixels by hand). And this one will certainly not work for the first attached image either as there is a circle in the middle.

    But I guess it is a start to work with.
     
    stephan2307, Jun 22, 2010 IP
  7. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #7
    Just tried it with the other image and either by coincidence we have exactly the same number of white pixels (that would be really big coincidence) or script doesn't work properly (yes that is the option that makes the most sense).

    the thought was there so now you just need to fix it good luck
     
    stephan2307, Jun 22, 2010 IP
  8. strgraphics

    strgraphics Active Member

    Messages:
    710
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #8
    i think..., this will useful to you...

    list($width, $height, $type, $attr) = getimagesize("image url");
    PHP:
    you will get each and every thing.. about that image..and finally echo the width, hight, type normally..

    first give a try for it, and let me..

    Thank you.
     
    strgraphics, Jun 22, 2010 IP
  9. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #9
    I don't think that he wants to do that. He wants to get the area of a shape that is in the image not just getting height and width. There was never the talk about that.
     
    stephan2307, Jun 22, 2010 IP
  10. strgraphics

    strgraphics Active Member

    Messages:
    710
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #10
    Oops, area that is present in the image..., how yar..!

    i am very exciting to see the comments then..,
     
    strgraphics, Jun 22, 2010 IP
  11. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #11
    stephan2307, Jun 22, 2010 IP
  12. Michellu

    Michellu Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    The basic idea would be this:
    -you read all the pixels in the image in a matrix (a 2 dimensional array)
    -than you go through each line of the matrix and check every pixel:
    if the current pixel is black it means you found a border of the image and start counting the white pixels until you find black again
    (for odd number of border encounters you start counting, for even number of border encounters you stop counting; this resolves the holes also)

    But the problem is really a lot more complicated than this. Your image is a jpg. Jpg images have a lot of artifacts from compression, not all pixels are black or white, a lot are gray. You'll first need to created a method to approximate pixel's colors to either black or white.

    Also this method only works on closed shapes with no gap. Gaps have to be treated explicitly.
     
    Michellu, Jun 22, 2010 IP
  13. christiandersen

    christiandersen Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #13
    I am not sure what a Lamp environment is but we have some software that can do this measurement for you. We have af free, and payable versions, depending on your needs.
    The software is called KLONK Image Measurement, you can visit www.imagemeasurement.com to download you own version, i hope you can use this information.
     
    christiandersen, Sep 8, 2013 IP