PHP | Imagick segmentImage() Function

The Imagick::segmentImage() function is an inbuilt function in PHP which is used to segment an image.

Syntax:

bool Imagick::segmentImage(int $COLORSPACE, 
float $cluster_threshold, float $smooth_threshold, 
bool $verbose = FALSE )

Parameters: This function accepts four parameters as mentioned above and described below:

  • $COLORSPACE: It specifies the integer value corresponding to one of COLORSPACE constants. You can also pass direct constants like imagick::COLORSPACE_RGB.
  • $cluster_threshold: It specifies a percentage describing minimum number of pixels contained in hexedra before it is considered valid.
  • $smooth_threshold: It specifies whether to eliminate noise from the histogram.
  • $verbose (Optional): It specifies whether to output detailed information about recognized classes. Default value is false

List of all COLORSPACE constants are given below:

  • imagick::COLORSPACE_UNDEFINED (0)
  • imagick::COLORSPACE_RGB (1)
  • imagick::COLORSPACE_GRAY (2)
  • imagick::COLORSPACE_TRANSPARENT (3)
  • imagick::COLORSPACE_OHTA (4)
  • imagick::COLORSPACE_LAB (5)
  • imagick::COLORSPACE_XYZ (6)
  • imagick::COLORSPACE_YCBCR (7)
  • imagick::COLORSPACE_YCC (8)
  • imagick::COLORSPACE_YIQ (9)
  • imagick::COLORSPACE_YPBPR (10)
  • imagick::COLORSPACE_YUV (11)
  • imagick::COLORSPACE_CMYK (12)
  • imagick::COLORSPACE_SRGB (13)
  • imagick::COLORSPACE_HSB (14)
  • imagick::COLORSPACE_HSL (15)
  • imagick::COLORSPACE_HWB (16)
  • imagick::COLORSPACE_REC601LUMA (17)
  • imagick::COLORSPACE_REC709LUMA (19)
  • imagick::COLORSPACE_LOG (21)
  • imagick::COLORSPACE_CMY (22)

Return Value: This function returns TRUE on success.



Exceptions: This function throws ImagickException on error.

Below given programs illustrate the Imagick::segmentImage() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new imagick object
$imagick = new Imagick(
  
// Segment the image with colorspace as imagick::COLORSPACE_RGB
$imagick->segmentImage(1, 0, 12);
  
// Display the image
header("Content-Type: image/png");
echo $imagick->getImageBlob();
?>

chevron_right


Output:

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new imagick object
$imagick = new Imagick(
  
// Segment the image with colorspace as imagick::COLORSPACE_GRAY
$imagick->segmentImage(2, 5, 30);
  
// Display the image
header("Content-Type: image/png");
echo $imagick->getImageBlob();
?>

chevron_right


Output:

Reference: https://www.php.net/manual/en/imagick.segmentimage.php




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.