Friday, September 19, 2025
HomeLanguagesPHP | Imagick segmentImage() Function

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:




<?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();
?>


Output:

Program 2:




<?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();
?>


Output:

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32303 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6666 POSTS0 COMMENTS
Nicole Veronica
11841 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11898 POSTS0 COMMENTS
Shaida Kate Naidoo
6781 POSTS0 COMMENTS
Ted Musemwa
7059 POSTS0 COMMENTS
Thapelo Manthata
6740 POSTS0 COMMENTS
Umr Jansen
6745 POSTS0 COMMENTS