PHP | ImagickPixel getColorCount() function

The ImagickPixel::getColorCount() function is an inbuilt function in PHP which is used to get the color count associated with the pixel color. A color count is the number of pixels in the image that have the same color as this ImagickPixel. getColorCount() appears to only work for ImagickPixel objects created through getImageHistogram().

Syntax:

int ImagickPixel::getColorCount( void ) : int

Parameters: This function doesn’t accepts any parameter.

Return Value: This function returns an integer containing the color count.

Exceptions: This function throws ImagickException on error.



Below given programs illustrate the ImagickPixel::getColorCount() function in PHP:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Create a new imagick object
$imagick = new Imagick(
  
// Get the image histogram
$histogramElements = $imagick->getImageHistogram();
  
// Get the last index
$lastIndex = count($histogramElements) - 1;
  
// Get the element from array which is 
// a ImagickPixel object
$lastColor = $histogramElements[$lastIndex];
  
// Get the Color count
echo $lastColor->getColorCount();
?>

chevron_right


Output:

18

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Create a new imagick object
$imagick = new Imagick(
  
// Get the image histogram
$histogramElements = $imagick->getImageHistogram();
  
// Get the element from array which is 
// a ImagickPixel object
$lastColor = $histogramElements[0];
  
// Get the Color count
echo $lastColor->getColorCount();
?>

chevron_right


Output:

1

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Create a new imagick object
$imagick = new Imagick(
  
// Get the image histogram
$histogramElements = $imagick->getImageHistogram();
  
// Get the element from array which is 
// a ImagickPixel object
$firstColor = $histogramElements[0];
  
// Set the Color count
$firstColor->setColorCount(20);
  
// Get the Color count
echo $firstColor->getColorCount();
?>

chevron_right


Output:

20

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Create a new imagick object
$imagick = new Imagick(
  
// Get the image histogram
$histogramElements = $imagick->getImageHistogram();
  
// Get the whole color stats
echo "R G B Hue :Count<br>";
foreach ($histogramElements as $pixel) {
    $colors = $pixel->getColor();
    foreach ($colors as $color) {
        print($color . " ");
    }
    print(":" . $pixel->getColorCount() . "<br>");
}
?>

chevron_right


Output:

R G B Hue :Count
0 22 35 1 :1
0 25 37 1 :1
0 24 37 1 :1
0 31 43 1 :1
0 32 44 1 :1
0 33 45 1 :1
0 37 49 1 :3
.
.
.

Reference: https://www.php.net/manual/en/imagickpixel.getcolorcount.php




My Personal Notes arrow_drop_up

I love Open Source Culture have contributed to phpMyAdmin, LibreOffice, Ant-Design and Web is my passion

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.