SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Sep 2003
    Location
    Northern California
    Posts
    605
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using PHP or non-programmatic tool to count pixels of select color in image

    Is there a way using PHP's color-related functions or imageMagik to count up the number pixels of a certain color (hex or rgb) in a jpg?

    If there is a non-programming means to achieve this, that's ok too...meaning a tool or software app, plugin, etc.

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Well, unless there's a predefined method for doing this you would loop through all possible co-ordinates, and increment a variable for every pixel that is that color.

    PHP Code:
    function getPixelCountByColor($image$color){
        
    $currentCount 0;
        for(
    $x 1$x <= imagesx($image); $x++){
            for(
    $y 1$y <= imagesy($image); $y++){
                if(
    imagecolorat($image$x$y) == $color){
                    
    $currentCount++;
                }
            }
        }

    Edit:

    By the way, I haven't tested it - that's just pseudo
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    Well, unless there's a predefined method for doing this you would loop through all possible co-ordinates, and increment a variable for every pixel that is that color.

    PHP Code:
    function getPixelCountByColor($image$color){
        
    $currentCount 0;
        for(
    $x 1$x <= imagesx($image); $x++){
            for(
    $y 1$y <= imagesy($image); $y++){
                if(
    imagecolorat($image$x$y) == $color){
                    
    $currentCount++;
                }
            }
        }

    Edit:

    By the way, I haven't tested it - that's just pseudo
    er... that's really slow... Just find the totaly x then the total y then multiply them since the image is a rectangle.

    PHP Code:
    $x 1;
    for(;$ <= 
    imagesx($image); $x++);

    $y 1;
    for(;
    $y <= imagesy($image); $y++);

    return 
    $x $y
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  4. #4
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The question isn't about getting the amount of pixels.

    It's about getting the amount of pixels with a certain colour.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  5. #5
    SitePoint Guru
    Join Date
    Sep 2003
    Location
    Northern California
    Posts
    605
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great thanks....this helps.

  6. #6
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    The question isn't about getting the amount of pixels.

    It's about getting the amount of pixels with a certain colour.
    oh lol. sorry. I should read more ^_^
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •