PHP Conference Japan 2024

Imagick::morphology

(PECL imagick 3 >= 3.3.0)

Imagick::morphology根据给定的形态学方法将用户提供的内核应用于图像。

描述

public Imagick::morphology(
    int $morphologyMethod,
    int $iterations,
    ImagickKernel $ImagickKernel,
    int $channel = Imagick::CHANNEL_DEFAULT
): bool

根据给定的形态学方法将用户提供的内核应用于图像。

参数

morphologyMethod

使用哪种形态学方法,\Imagick::MORPHOLOGY_* 常量之一。

iterations

应用形态学函数的迭代次数。值为 -1 表示循环直到找不到更改。这如何应用可能取决于形态学方法。通常,此值为 1。

ImagickKernel

channel

返回值

成功时返回 true

示例

示例 #1 卷积 Imagick::morphology()

<?php
$imagick
= $this->getCharacter();
$kernel = \ImagickKernel::fromBuiltIn(\Imagick::KERNEL_GAUSSIAN, "5,1");
$imagick->morphology(\Imagick::MORPHOLOGY_CONVOLVE, 2, $kernel);
header("Content-Type: image/png");
echo
$imagick->getImageBlob();

?>

示例 #2 相关 Imagick::morphology()

<?php

// 左上角像素必须为黑色
// 右下角像素必须为白色
// 我们不关心其余部分。


$imagick = $this->getCharacterOutline();
$kernel = \ImagickKernel::fromMatrix(self::$correlateMatrix, [2, 2]);
$imagick->morphology(\Imagick::MORPHOLOGY_CORRELATE, 1, $kernel);
header("Content-Type: image/png");
echo
$imagick->getImageBlob();

?>

示例 #3 腐蚀 Imagick::morphology()

<?php
$canvas
= $this->getCharacterOutline();
$kernel = \ImagickKernel::fromBuiltIn(\Imagick::KERNEL_OCTAGON, "3");
$canvas->morphology(\Imagick::MORPHOLOGY_ERODE, 2, $kernel);
header("Content-Type: image/png");
echo
$canvas->getImageBlob();

?>

示例 #4 腐蚀强度 Imagick::morphology()

<?php
$canvas
= $this->getCharacter();
$kernel = \ImagickKernel::fromBuiltIn(\Imagick::KERNEL_OCTAGON, "1");
$canvas->morphology(\Imagick::MORPHOLOGY_ERODE_INTENSITY, 2, $kernel);
header("Content-Type: image/png");
echo
$canvas->getImageBlob();

?>

示例 #5 膨胀 Imagick::morphology()

<?php
$canvas
= $this->getCharacterOutline();
$kernel = \ImagickKernel::fromBuiltIn(\Imagick::KERNEL_OCTAGON, "3");
$canvas->morphology(\Imagick::MORPHOLOGY_DILATE, 4, $kernel);
header("Content-Type: image/png");
echo
$canvas->getImageBlob();

?>

示例 #6 膨胀强度 Imagick::morphology()

<?php
$canvas
= $this->getCharacter();
$kernel = \ImagickKernel::fromBuiltIn(\Imagick::KERNEL_OCTAGON, "1");
$canvas->morphology(\Imagick::MORPHOLOGY_DILATE_INTENSITY, 4, $kernel);
header("Content-Type: image/png");
echo
$canvas->getImageBlob();

?>

示例 #7 使用 Chebyshev 内核的距离 Imagick::morphology()

<?php
$canvas
= $this->getCharacterOutline();
$kernel = \ImagickKernel::fromBuiltIn(\Imagick::KERNEL_CHEBYSHEV, "3");
$canvas->morphology(\Imagick::MORPHOLOGY_DISTANCE, 3, $kernel);
$canvas->autoLevelImage();
header("Content-Type: image/png");
echo
$canvas->getImageBlob();

?>

示例 #8 使用曼哈顿核的距离 Imagick::morphology()

<?php
$canvas
= $this->getCharacterOutline();
$kernel = \ImagickKernel::fromBuiltIn(\Imagick::KERNEL_MANHATTAN, "5");
$canvas->morphology(\Imagick::MORPHOLOGY_DISTANCE, 3, $kernel);
$canvas->autoLevelImage();
header("Content-Type: image/png");
echo
$canvas->getImageBlob();

?>

示例 #9 使用八角形核的距离 Imagick::morphology()

<?php
$canvas
= $this->getCharacterOutline();
$kernel = \ImagickKernel::fromBuiltIn(\Imagick::KERNEL_OCTAGONAL, "5");
$canvas->morphology(\Imagick::MORPHOLOGY_DISTANCE, 3, $kernel);
$canvas->autoLevelImage();
header("Content-Type: image/png");
echo
$canvas->getImageBlob();

?>

示例 #10 使用欧几里得核的距离 Imagick::morphology()

<?php
$canvas
= $this->getCharacterOutline();
$kernel = \ImagickKernel::fromBuiltIn(\Imagick::KERNEL_EUCLIDEAN, "4");
$canvas->morphology(\Imagick::MORPHOLOGY_DISTANCE, 3, $kernel);
$canvas->autoLevelImage();
header("Content-Type: image/png");
echo
$canvas->getImageBlob();

?>

示例 #11 边缘 Imagick::morphology()

<?php
$canvas
= $this->getCharacterOutline();
$kernel = \ImagickKernel::fromBuiltIn(\Imagick::KERNEL_OCTAGON, "3");
$canvas->morphology(\Imagick::MORPHOLOGY_EDGE, 1, $kernel);
header("Content-Type: image/png");
echo
$canvas->getImageBlob();

?>

示例 #12 开运算 Imagick::morphology()

<?php
// 结果显示“开运算”通过圆化任何尖点和平滑轮廓,并去除小于所用形状的任何部分。它还会断开或“打开”任何细小的桥接部分。
$canvas = $this->getCharacterOutline();
$kernel = \ImagickKernel::fromBuiltIn(\Imagick::KERNEL_DISK, "6");
$canvas->morphology(\Imagick::MORPHOLOGY_OPEN, 1, $kernel);
header("Content-Type: image/png");
echo
$canvas->getImageBlob();

?>

示例 #13 开运算强度 Imagick::morphology()

<?php
// 结果显示“开运算”通过圆化任何尖点和平滑轮廓,并去除小于所用形状的任何部分。它还会断开或“打开”任何细小的桥接部分。

$canvas = $this->getCharacter();
$kernel = \ImagickKernel::fromBuiltIn(\Imagick::KERNEL_DISK, "6");
$canvas->morphology(\Imagick::MORPHOLOGY_OPEN_INTENSITY, 1, $kernel);
header("Content-Type: image/png");
echo
$canvas->getImageBlob();

?>

示例 #14 闭运算 Imagick::morphology()

<?php
// “闭运算”的基本用法是减少或去除任何大小约为内核“结构元素”的“孔”或“间隙”。也就是说,闭合背景中大约那个大小的部分。
$canvas = $this->getCharacterOutline();
$kernel = \ImagickKernel::fromBuiltIn(\Imagick::KERNEL_DISK, "6");
$canvas->morphology(\Imagick::MORPHOLOGY_CLOSE, 1, $kernel);
header("Content-Type: image/png");
echo
$canvas->getImageBlob();

?>

示例 #15 闭运算强度 Imagick::morphology()

<?php
// 'Close' 方法的基本用法是减少或去除任何大小约等于内核“结构元素”的“孔”或“间隙”。也就是说,'闭合'背景中大约那个大小的部分。
$canvas = $this->getCharacter();
$kernel = \ImagickKernel::fromBuiltIn(\Imagick::KERNEL_DISK, "6");
$canvas->morphology(\Imagick::MORPHOLOGY_CLOSE_INTENSITY, 1, $kernel);
header("Content-Type: image/png");
echo
$canvas->getImageBlob();

?>

示例 #16 平滑 Imagick::morphology()

<?php
$canvas
= $this->getCharacterOutline();
$kernel = \ImagickKernel::fromBuiltIn(\Imagick::KERNEL_OCTAGON, "3");
$canvas->morphology(\Imagick::MORPHOLOGY_SMOOTH, 1, $kernel);
header("Content-Type: image/png");
echo
$canvas->getImageBlob();

?>

示例 #17 内边缘 Imagick::morphology()

<?php
$canvas
= $this->getCharacterOutline();
$kernel = \ImagickKernel::fromBuiltIn(\Imagick::KERNEL_OCTAGON, "3");
$canvas->morphology(\Imagick::MORPHOLOGY_EDGE_IN, 1, $kernel);
header("Content-Type: image/png");
echo
$canvas->getImageBlob();

?>

示例 #18 外边缘 Imagick::morphology()

<?php
$canvas
= $this->getCharacterOutline();
$kernel = \ImagickKernel::fromBuiltIn(\Imagick::KERNEL_OCTAGON, "3");
$canvas->morphology(\Imagick::MORPHOLOGY_EDGE_OUT, 1, $kernel);
header("Content-Type: image/png");
echo
$canvas->getImageBlob();

?>

示例 #19 'TopHat' 方法,更准确地说是 '白顶帽',返回的是通过形状的'开运算'移除的像素,也就是为了圆角和连接形状之间的桥梁而移除的像素。 Imagick::morphology()

<?php
$canvas
= $this->getCharacterOutline();
$kernel = \ImagickKernel::fromBuiltIn(\Imagick::KERNEL_DISK, "5");
$canvas->morphology(\Imagick::MORPHOLOGY_TOP_HAT, 1, $kernel);
header("Content-Type: image/png");
echo
$canvas->getImageBlob();

?>

示例 #20 'BottomHat' 方法,也称为 '黑顶帽',指的是形状的'闭运算'添加到图像中的像素。也就是用于填充“孔”、“间隙”和“桥梁”的像素。 Imagick::morphology()

<?php

$canvas
= $this->getCharacterOutline();
$kernel = \ImagickKernel::fromBuiltIn(\Imagick::KERNEL_DISK, "5");
$canvas->morphology(\Imagick::MORPHOLOGY_BOTTOM_HAT, 1, $kernel);
header("Content-Type: image/png");
echo
$canvas->getImageBlob();

?>

示例 #21 命题与否定 Imagick::morphology()

<?php
$canvas
= $this->getCharacterOutline();
// 这将查找所有具有 3 个像素右边缘的像素
$matrix = [[1, false, false, 0]];
$kernel = \ImagickKernel::fromMatrix(
$matrix,
[
0, 0]
);
$canvas->morphology(\Imagick::MORPHOLOGY_HIT_AND_MISS, 1, $kernel);
header("Content-Type: image/png");
echo
$canvas->getImageBlob();

?>

示例 #22 细化 Imagick::morphology()

<?php
$canvas
= $this->getCharacterOutline();
$leftEdgeKernel = \ImagickKernel::fromMatrix([[0, 1]], [1, 0]);
$rightEdgeKernel = \ImagickKernel::fromMatrix([[1, 0]], [0, 0]);
$leftEdgeKernel->addKernel($rightEdgeKernel);

$canvas->morphology(\Imagick::MORPHOLOGY_THINNING, 3, $leftEdgeKernel);
header("Content-Type: image/png");
echo
$canvas->getImageBlob();

?>

示例 #23 增厚 Imagick::morphology()

<?php
$canvas
= $this->getCharacterOutline();
$leftEdgeKernel = \ImagickKernel::fromMatrix([[0, 1]], [1, 0]);
$rightEdgeKernel = \ImagickKernel::fromMatrix([[1, 0]], [0, 0]);
$leftEdgeKernel->addKernel($rightEdgeKernel);

$canvas->morphology(\Imagick::MORPHOLOGY_THICKEN, 3, $leftEdgeKernel);
header("Content-Type: image/png");
echo
$canvas->getImageBlob();

?>

示例 #24 使用加粗生成凸包 Imagick::morphology()

<?php
$canvas
= $this->getCharacterOutline();
$diamondKernel = \ImagickKernel::fromBuiltIn(\Imagick::KERNEL_DIAMOND, "1");
$convexKernel = \ImagickKernel::fromBuiltIn(\Imagick::KERNEL_CONVEX_HULL, "");

// 加粗形态学不处理小的间隙。我们用闭合形态学来闭合它们。
// with the close morphology.
$canvas->morphology(\Imagick::MORPHOLOGY_CLOSE, 1, $diamondKernel);
$canvas->morphology(\Imagick::MORPHOLOGY_THICKEN, -1, $convexKernel);
$canvas->morphology(\Imagick::MORPHOLOGY_CLOSE, 1, $diamondKernel);

header("Content-Type: image/png");
echo
$canvas->getImageBlob();

?>

示例 #25 迭代形态学 Imagick::morphology()

<?php
$canvas
= $this->getCharacterOutline();
$kernel = \ImagickKernel::fromBuiltIn(\Imagick::KERNEL_DISK, "2");
$canvas->morphology(\Imagick::MORPHOLOGY_ITERATIVE, 3, $kernel);
$canvas->autoLevelImage();
header("Content-Type: image/png");
echo
$canvas->getImageBlob();

?>

示例 #26 获取图像轮廓的辅助函数 Imagick::morphology()

<?php
function getCharacterOutline() {
$imagick = new \Imagick(realpath("./images/character.png"));
$character = new \Imagick();
$character->newPseudoImage(
$imagick->getImageWidth(),
$imagick->getImageHeight(),
"canvas:white"
);
$canvas = new \Imagick();
$canvas->newPseudoImage(
$imagick->getImageWidth(),
$imagick->getImageHeight(),
"canvas:black"
);

$character->compositeimage(
$imagick,
\Imagick::COMPOSITE_COPYOPACITY,
0, 0
);
$canvas->compositeimage(
$character,
\Imagick::COMPOSITE_ATOP,
0, 0
);
$canvas->setFormat('png');

return
$canvas;
}
?>

添加注释

用户贡献的注释

此页面没有用户贡献的注释。
To Top