Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Pillow ImageMorph Module

Pillow ImageMorph Module

ImageMorph is a specialized module in the Python Pillow image processing library, mainly used forimage morphological operations. Morphological operations are a series of operations based on image shapes, commonly used forBinary imageprocessing, with wide applications in image analysis, pattern recognition and other fields.


Core Concepts

1. Basic morphological operations

The ImageMorph module mainly implements the following two basic morphological operations:

These two basic operations can be combined into more complex morphological operations, such as opening, closing, etc.

2. Structuring Element

The core of morphological operations isthe structuring element, which determines the neighborhood shape and size of the operation. Pillow provides several predefined structuring elements:


ImageMorph main methods

The following table describes in detail the main methods of the ImageMorph module and their functions:

Method Name Parameters Return Value Description
ImageMorph.LutBuilder(patterns=None, op_name=None) patterns: List of patterns
op_name: Operation name LutBuilder object Create a lookup table builder for custom morphological operations
apply(image) image: PIL image to process Processed image Apply morphological operations to the input image
get_on_pixels(image) image: Input PIL image List of coordinates Get the coordinates of all foreground pixels (value 1) in the image
match(image) image: Input PIL image Match result Check whether the image matches the pattern of the current morphological operation
save(filename) filename: Save file name None Save the current morphological operation to a file
load(filename) filename: Load file name None Load morphological operation from file

Practical Application Examples

1. Basic morphological operations

Example

from

PIL

import

Image

,

ImageMorph

Create morphological operation object

morph_op

=

ImageMorph.

MorphOp

(

op_name

=

'dilation4'

)

Load binary image

image

=

Image.

open

(

'binary_image.png'

)

.

convert

(

'1'

)

Apply morphological operation

result

=

morph_op.

apply

(

image

)

result.

show

(

)

2. Custom structuring elements

Example

Custom structuring element

patterns

=

[

   

"1:(...)->1"

,

 

Keep when the center pixel is 1

   

"4:(010)->1"

 

Specific pattern matching

]

Create custom morphological operation

builder

=

ImageMorph.

LutBuilder

(

patterns

=

patterns

)

morph_op

=

builder.

build_op

(

)

Apply custom operation

result

=

morph_op.

apply

(

image

)


Advanced application techniques

1. Combining morphological operations

Example

Erode first, then dilate (opening operation)

erode

=

ImageMorph.

MorphOp

(

op_name

=

'erosion8'

)

dilate

=

ImageMorph.

MorphOp

(

op_name

=

'dilation8'

)

Apply operation sequence

temp

=

erode.

apply

(

image

)

result

=

dilate.

apply

(

temp

)

2. Boundary handling

The ImageMorph module uses by default'0'(black) as the boundary fill value. For special boundary handling needs, the image can be padded in advance:

Example

from

PIL

import

ImageOps

Add white border

padded_image

=

ImageOps.

expand

(

image

,

border

=

2

,

fill

=

'white'

)


Notes

  1. ImageMorph mainly targetsBinary image(mode '1'); before processing other image types, conversion is required first
  2. The size of the structuring element affects processing results and performance
  3. Complex morphological operations may require multiple applications of basic operation combinations
  4. For large images, consider block processing to improve performance

By mastering these methods and techniques, you can use Pillow's ImageMorph module to implement a variety of professional image morphological processing tasks.