如何使用 imageconvolution() 在 PHP 中應用 3×3 卷積矩陣?


imageconvolution() 是 PHP 中的一個內建函式,用於應用 3×3 卷積矩陣,使用影像中的係數和偏移量。

語法

bool imageconvolution ( $image, $matrix, $div, $offset)

引數

imageconvolution() 接受四個引數:$image、$matrix、$div 和 $offset。

  • $image − 此引數用於使用影像建立函式(例如 imagecreatetruecolor())建立影像大小。

  • $matrix − 此引數包含一個 3×3 浮點陣列矩陣。

  • $div − 用於歸一化。

  • $offset − 此引數用於設定顏色偏移量。

返回值

如果成功,imageconvolution() 返回 True;如果失敗,則返回 False。

示例 1

<?php
   // load the PNG image by using imagecreatefrompng function.
   $image = imagecreatefrompng('C:\xampp\htdocs\Images\img59.png');
   
   // Applied the 3X3 array matrix
   $matrix = array(
      array(2, 0, 0),
      array(0, -1, 0),
      array(0, 0, -1)
   );
   // imageconvolution function to modify image elements
   imageconvolution($image, $matrix, 1, 127);

   // show the output image in the browser
   header('Content-Type: image/png');
   imagepng($image, null, 9);
?>

輸出

在使用 imageconvolution() 函式之前輸入的 PNG 影像

使用 imageconvolution() 函式後輸出的 PNG 影像

示例 2

<?php
   $image = imagecreatetruecolor(700, 300);
   
   // Writes the text and apply a gaussian blur on the image
   imagestring($image, 50, 25, 8, 'Gaussian Blur Text image', 0x00ff00);
   $gaussian = array(
      array(1.0, 2.0, 1.0),
      array(2.0, 4.0, 2.0),
      array(1.0, 2.0, 1.0)
   );
   imageconvolution($image, $gaussian, 16, 0);

   // Rewrites the text for comparison
   imagestring($image, 15, 20, 18, 'Gaussian Blur Text image', 0x00ff00);
   header('Content-Type: image/png');
   imagepng($image, null, 9);
?>

輸出

更新於: 2021 年 8 月 9 日

226 檢視數

開啟你的事業

完成課程獲得認證

現在開始
廣告
© . All rights reserved.