DianaSimona Posted June 18, 2020 Share Posted June 18, 2020 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 Quote Link to comment https://forums.phpfreaks.com/topic/310961-converting-grayscale-image-to-pure-black-and-white/ Share on other sites More sharing options...
requinix Posted June 18, 2020 Share Posted June 18, 2020 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/310961-converting-grayscale-image-to-pure-black-and-white/#findComment-1579002 Share on other sites More sharing options...
Barand Posted June 19, 2020 Share Posted June 19, 2020 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); 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) 1 Quote Link to comment https://forums.phpfreaks.com/topic/310961-converting-grayscale-image-to-pure-black-and-white/#findComment-1579003 Share on other sites More sharing options...
DianaSimona Posted June 19, 2020 Author Share Posted June 19, 2020 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 😀 Quote Link to comment https://forums.phpfreaks.com/topic/310961-converting-grayscale-image-to-pure-black-and-white/#findComment-1579004 Share on other sites More sharing options...
Barand Posted June 19, 2020 Share Posted June 19, 2020 Try a combination approach, increasing the contrast to reduce the noise. 1 Quote Link to comment https://forums.phpfreaks.com/topic/310961-converting-grayscale-image-to-pure-black-and-white/#findComment-1579011 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.