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:

<?php
 $im = imagecreatefromjpeg("celule.jpg");

function imagetograyscale($im)
{
    if (imageistruecolor($im)) {
        imagetruecolortopalette($im, false, 256);
    }

    for ($c = 0; $c < imagecolorstotal($im); $c++) {
        $col = imagecolorsforindex($im, $c);
        $gray = round(0.299 * $col['red'] + 0.587 * $col['green'] + 0.114 * $col['blue']);
        imagecolorset($im, $c, $gray, $gray, $gray);
    }
}

imagetograyscale($im);

//imagefilter($im, IMG_FILTER_CONTRAST, -255);  //i'm not looking for this effect 

header('Content-type: image/jpeg');
imagejpeg($im);

$C = imagesx($im); //width
 $L = imagesy($im); //height

 echo "Dimensiuni imagine: latime $C, inaltime $L <br>";

 //scanning through the image
 for($x = 0; $x < $L; $x++) {  //each line
    for($y = 0; $y < $C; $y++) {  //each column
        // pixel color at (x, y)
        $color = imagecolorat($im, $y, $x);
        $color = imagecolorsforindex($im, $color); //getting rgb values
        $RED[$x][$y] = $color["red"];  //each rgb component
        $GREEN[$x][$y] = $color["green"];
        $BLUE[$x][$y] = $color["blue"];

    }
 } 

?>

MATLAB:

clear all, clc, close all;


I = imread('celule.jpg');
imshow(I)
title('original');

a=rgb2gray(I);

figure;
imshow(a)
title('grayscale');

s=size(a);


for i=1:s(1)
    for j=1:s(2)

        if a(i,j)>190
            a(i,j)=0;
        else a(i,j)=255;
            end
        end
end

 figure;
 imshow(a)
 title('pure black and white');

Advertisement

Answer

Here’s a way to do that with gd:

#!/usr/bin/php -f
<?php

   // Open image and get dimensions
   $im = imagecreatefromjpeg("cellule.jpg");
   $w = imagesx($im);
   $h = imagesy($im);

   // Convert to greyscale
   imagefilter($im,IMG_FILTER_GRAYSCALE);
   imagepng($im, "grey.png");              // DEBUG only

   // Allocate a new palette image to hold the b&w output
   $out = imagecreate($w,$h);
   // Allocate b&w palette entries
   $black = imagecolorallocate($out,0,0,0);
   $white = imagecolorallocate($out,255,255,255);

   // Iterate over all pixels, thresholding to pure b&w
   for ($x = 0; $x < $w; $x++) {
      for ($y = 0; $y < $h; $y++) {
         // Get current color
         $index  = imagecolorat($im, $x, $y);
         $grey   = imagecolorsforindex($im, $index)['red'];
         // Set pixel white if below threshold - don't bother settting black as image is initially black anyway
         if ($grey <= 190) {
            imagesetpixel($out,$x,$y,$white);
         }
      }
   }
   imagepng($out, "result.png");
?>

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