Jump to content

Recommended Posts

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, im having troubles accessing each individual pixel's color and modifying it.

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');

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 collumn
        // 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"];

    }
 } 

?>

image "celule.jpg": https://i.ibb.co/6Ffj6sc/celule.jpg

And what's the problem? I see some PHP code there which looks like it's on the right path. In fact it already has something to grayscale an image, so I would think that what you need to do is simply modify that to use black and white instead.

  • Like 1

Have you tried 

$im = imagecreatefromjpeg('../images/celule.jpg');
imagefilter($im, IMG_FILTER_GRAYSCALE);
imagefilter($im, IMG_FILTER_CONTRAST, -1000); 

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

image.png.6d0570aa59e4ccf73de5a1861072e99f.png

Emulating you matlab example 

$im = imagecreatefromjpeg('../images/celule.jpg');
imagefilter($im, IMG_FILTER_GRAYSCALE);

$white = imagecolorallocate($im, 255,255,255);
$black = imagecolorallocate($im, 0,0,0);

$w = imagesx($im);
$h = imagesy($im);

for ($x=0; $x<$w; $x++) {
    for ($y=0; $y<$h; $y++) {
        $c = imagecolorat($im, $x, $y);
        if ($c & 0xFF > 190)                         // blue component
            imagesetpixel($im, $x, $y, $white);
        else 
            imagesetpixel($im, $x, $y, $black);
    }
}
header("Content-type: image/jpeg");
imagejpeg($im);
imagedestroy($im); 

Gives an effect like this (top-left corner of image)

image.png.32ac6f033f744575064f3a15604986f4.png

  • Thanks 1

Thank you for your replies!

What i was trying to do is obtain a pure black and white image as i did in matlab (using a threshold value), but i was quite unsure on how to implement that in php.

I will try using the tips provided!

Thanks again 😀

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.