Skip to content
Advertisement

How to convert a grayscale image to pure black and white in php?

I’m trying to convert a grayscale image to pure black and white in PHP using the GD library.

The purpose would be to detect the cervical cells within the image.

I’ll leave the PHP code and a MatLab one (I wrote this code in MatLab and I’m trying to obtain the same result in PHP). Basically, I’m having trouble accessing each individual pixel’s color and modifying it. sample image

PHP:

JavaScript

MATLAB:

JavaScript

Advertisement

Answer

Here’s a way to do that with gd:

JavaScript

enter image description here

Keywords: PHP, image processing, greyscale, grayscale, threshold, palette, PNG, gd, GD, imagecreate, imagepng, imagecreatefromjpeg, imagecolorallocate.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement