Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中将灰度图像转换为纯黑白?_Php_Image_Matlab_Gd_Grayscale - Fatal编程技术网

如何在php中将灰度图像转换为纯黑白?

如何在php中将灰度图像转换为纯黑白?,php,image,matlab,gd,grayscale,Php,Image,Matlab,Gd,Grayscale,我正在尝试使用GD库在PHP中将灰度图像转换为纯黑白图像 目的是检测图像中的宫颈细胞 我将留下PHP代码和MatLab代码(我在MatLab中编写了这段代码,并试图在PHP中获得相同的结果)。基本上,我很难访问每个像素的颜色并修改它 PHP: 你试过Imagick charcoalImage吗 也许PHPImagick库更适合您的工作。这里有一种使用gd的方法: #!/usr/bin/php -f <?php // Open image and get dimensions

我正在尝试使用GD库在PHP中将灰度图像转换为纯黑白图像

目的是检测图像中的宫颈细胞

我将留下PHP代码和MatLab代码(我在MatLab中编写了这段代码,并试图在PHP中获得相同的结果)。基本上,我很难访问每个像素的颜色并修改它

PHP:


你试过Imagick charcoalImage吗


也许PHPImagick库更适合您的工作。

这里有一种使用
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");
?>
#/usr/bin/php-f

嘿我在做最后一年的项目,我的教授说我应该使用GD图书馆。我读过一些关于它的教程,但我必须承认我有点卡住了。无论如何,谢谢你的推荐!为了确保您的图像只包含纯黑白(即0或255),您希望将其保存为JPEG(通常为3通道)似乎有些奇怪,因为它是有损的,完全可以自由更改0/255值以节省空间。你能不能把你的纯黑白图像制作成一个无损的、完美的PNG格式?我没有想到要把图像转换成PNG格式。我使用的数据库包含jpeg单元格图像,所以我假设我将使用该格式。我对用php处理图像也很陌生,我还有很多东西要学,但谢谢你的见解!嘿非常感谢,这正是我想要的,而且解释得很好。再次感谢
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');
#!/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");
?>