PHP ImageMagick - Image Threshold



In this chapter, you will be learning to create different types of threshold images using a few inbuilt functions of the ImageMagick library.

PHP Imagemagick offers a wide range of features, including image thresholding. Image thresholding allows you to adjust the contrast of an image by setting a specific limit or 'threshold' that determines how light or dark pixels in the image will appear.

This makes it possible to create high-contrast images with sharp edges between objects and backgrounds, as well as smooth gradients without harsh transitions. By using PHP Imagemagick's image threshold feature, users can easily improve their digital photos and graphics.

Creating a Black Threshold Image

The 'blackThresholdImage()' function is part of the Imagemagick library. It can be used to quickly and easily modify an image by applying a threshold value that will be compared against all pixels in the input image.

Any pixel with a value below the specified threshold will be turned to black, while any pixel above the threshold will remain unchanged.

Syntax

public Imagick::blackThresholdImage(mixed $threshold): bool

This function has only one parameter which is ‘threshold’. This specifies the threshold value with which all the pixels of the image are compared.

Example

This example shows the use of the 'blackThresholdImage()' function. An Imagick object is created and an image is taken as input. The 'blackThresholdImage()' function is applied with a threshold value as its parameter, resulting in an output image saved as 'blackThresholdImage.png'.

<?php
   $image=new Imagick($_SERVER['DOCUMENT_ROOT']."/test/image.png");
   $image->blackThresholdImage('rgb(1, 255, 141)');
   $image->writeImage($_SERVER['DOCUMENT_ROOT']."/test/blackThresholdImage.png");
?>

Assume that the following is the input image (image.png) in the program −

Creating Black Threshold Image

Output

Creating Black Threshold Image

Creating a White Threshold Image

The 'whiteThresholdImage()' function enables you to set a specified threshold value, and then apply it against all pixels of an input image. Those values that are below the threshold will be converted to white, while any value that is above the threshold remains unchanged.

This can be highly beneficial for tasks such as image segmentation or noise removal, allowing you to quickly identify portions of an image that need further attention.

Syntax

public Imagick::whiteThresholdImage(mixed $threshold): bool

It has only one parameter - ‘threshold’. This specifies the threshold value with which all the pixels of the image are compared.

Example

The following example demonstrates how to use the 'whiteThresholdImage' function. In here, an Imagick object is created and an image is taken as input. The 'whiteThresholdImage' function is then applied with a threshold value as its parameter. The resultant image is saved as 'whiteThresholdImage.png'.

<?php
   $image=new Imagick($_SERVER['DOCUMENT_ROOT']."/test/imagee.png");
   $image->whiteThresholdImage('rgb(1, 255, 141)');
   $image->writeImage($_SERVER['DOCUMENT_ROOT']."/test/whiteThresholdImage.png");
?>

Assume that the following is the input image (image.png) in the program −

Creating White Threshold Image

Output

Creating White Threshold Image
Advertisements