Imagick::morphology

(PECL imagick 3 >= 3.3.0)

Imagick::morphologyDescription

Description

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

Applies a user supplied kernel to the image according to the given morphology method.

Parameters

morphologyMethod

Which morphology method to use one of the \Imagick::MORPHOLOGY_* constants.

iterations

The number of iteration to apply the morphology function. A value of -1 means loop until no change found. How this is applied may depend on the morphology method. Typically this is a value of 1.

ImagickKernel

channel

Return Values

Returns true on success.

Examples

Example #1 Convolve Imagick::morphology()

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

?>

Example #2 Correlate Imagick::morphology()

<?php

        
// Top-left pixel must be black
        // Bottom right pixel must be white
        // We don't care about the rest.
        

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

?>

Example #3 Erode Imagick::morphology()

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

?>

Example #4 Erode Intensity Imagick::morphology()

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

?>

Example #5 Dilate Imagick::morphology()

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

?>

Example #6 Dilate intensity Imagick::morphology()

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

?>

Example #7 Distance with Chebyshev kernel Imagick::morphology()

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

?>

Example #8 Distance with Manhattan kernel Imagick::morphology()

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

?>

Example #9 Distance with ocatagonal kernel Imagick::morphology()

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

?>

Example #10 Distance with Euclidean kernel Imagick::morphology()

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

?>

Example #11 Edge Imagick::morphology()

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

?>

Example #12 Open Imagick::morphology()

<?php
        
// As a result you will see that 'Open' smoothed the outline, by rounding off any sharp points, and remove any parts that is smaller than the shape used. It will also disconnect or 'open' any thin bridges.
        
$canvas $this->getCharacterOutline();
        
$kernel = \ImagickKernel::fromBuiltIn(\Imagick::KERNEL_DISK"6");
        
$canvas->morphology(\Imagick::MORPHOLOGY_OPEN1$kernel);
        
header("Content-Type: image/png");
        echo 
$canvas->getImageBlob();

?>

Example #13 Open intensity Imagick::morphology()

<?php
        
// As a result you will see that 'Open' smoothed the outline, by rounding off any sharp points, and remove any parts that is smaller than the shape used. It will also disconnect or 'open' any thin bridges.

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

?>

Example #14 Close Imagick::morphology()

<?php
        
//The basic use of the 'Close' method is to reduce or remove any 'holes' or 'gaps' about the size of the kernel 'Structure Element'. That is 'close' parts of the background that are about that size.
        
$canvas $this->getCharacterOutline();
        
$kernel = \ImagickKernel::fromBuiltIn(\Imagick::KERNEL_DISK"6");
        
$canvas->morphology(\Imagick::MORPHOLOGY_CLOSE1$kernel);
        
header("Content-Type: image/png");
        echo 
$canvas->getImageBlob();

?>

Example #15 Close Intensity Imagick::morphology()

<?php
        
//The basic use of the 'Close' method is to reduce or remove any 'holes' or 'gaps' about the size of the kernel 'Structure Element'. That is 'close' parts of the background that are about that size.
        
$canvas $this->getCharacter();
        
$kernel = \ImagickKernel::fromBuiltIn(\Imagick::KERNEL_DISK"6");
        
$canvas->morphology(\Imagick::MORPHOLOGY_CLOSE_INTENSITY1$kernel);
        
header("Content-Type: image/png");
        echo 
$canvas->getImageBlob();

?>

Example #16 Smooth Imagick::morphology()

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

?>

Example #17 Edge in Imagick::morphology()

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

?>

Example #18 Edge out Imagick::morphology()

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

?>

Example #19 The 'TopHat' method, or more specifically 'White Top Hat', returns the pixels that were removed by a Opening of the shape, that is the pixels that were removed to round off the points, and the connecting bridged between shapes. Imagick::morphology()

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

?>

Example #20 The 'BottomHat' method, also known as 'Black TopHat' is the pixels that a Closing of the shape adds to the image. That is the pixels that were used to fill in the 'holes', 'gaps', and 'bridges'. Imagick::morphology()

<?php

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

?>

Example #21 Hit and Miss Imagick::morphology()

<?php
        $canvas 
$this->getCharacterOutline();
        
//This finds all the pixels with 3 pixels of the right edge
        
$matrix = [[1falsefalse0]];
        
$kernel = \ImagickKernel::fromMatrix(
            
$matrix,
            [
00]
        );
        
$canvas->morphology(\Imagick::MORPHOLOGY_HIT_AND_MISS1$kernel);
        
header("Content-Type: image/png");
        echo 
$canvas->getImageBlob();

?>

Example #22 Thinning Imagick::morphology()

<?php
        $canvas 
$this->getCharacterOutline();
        
$leftEdgeKernel = \ImagickKernel::fromMatrix([[01]], [10]);
        
$rightEdgeKernel = \ImagickKernel::fromMatrix([[10]], [00]);
        
$leftEdgeKernel->addKernel($rightEdgeKernel);
        
        
$canvas->morphology(\Imagick::MORPHOLOGY_THINNING3$leftEdgeKernel);
        
header("Content-Type: image/png");
        echo 
$canvas->getImageBlob();

?>

Example #23 Thicken Imagick::morphology()

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

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

?>

Example #24 Thick to generate a convex hull Imagick::morphology()

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

        
// The thicken morphology doesn't handle small gaps. We close them
        // with the close morphology.
        
$canvas->morphology(\Imagick::MORPHOLOGY_CLOSE1$diamondKernel);
        
$canvas->morphology(\Imagick::MORPHOLOGY_THICKEN, -1$convexKernel);
        
$canvas->morphology(\Imagick::MORPHOLOGY_CLOSE1$diamondKernel);

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

?>

Example #25 Iterative morphology Imagick::morphology()

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

?>

Example #26 Helper function to get an image silhouette 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,
        
00
    
);
    
$canvas->compositeimage(
        
$character,
        \
Imagick::COMPOSITE_ATOP,
        
00
    
);
    
$canvas->setFormat('png');

    return 
$canvas;
}
?>

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial