Sei sulla pagina 1di 3

Image morphology

Image Morphology
Li Yicheng
LIACS
l.y.c.liyicheng@gmail.com
Abstract
Understand the image morphology, and pay attention to the following definitions: open, close, erode,
dilate, structuring element. Understand how to use opencv image processing builtin fucntions to do the
morphology operation. Understand how to use the track bar in openCV, because such tool is important in
future testing.

I.

Image Morphology

First please click here to check this website to


understand image morphology. What I did is
just summarise the important part I think in
this tutorial.
Figure 1: Examples

I.

Structuring element

I Morphological techniques probe an image


with a small shape or template called a structuring element.
I structuring element is a small matrix of pixels, each with a value of zero or one.
I The matrix dimensions specify the size of
the structuring element.
I The pattern of ones and zeros specifies the
shape of the structuring element.
I An origin of the structuring element is usually one of its pixels, although generall the
origin can be outside the structuring element.
I A common practise is to have odd dimensions of a struturing matrix and the origin is
defined as the center of the matrix.
tutorial

Figure 2: Examples

When a structuring element is placed in a


binary image, each of its pixels is associated
with the corresponding pixel of the neighbourhood under the structuring element. The
structuring element is said to fit the image if,
for each of its pixels set to 1, the corresponding
image pixel is also 1. Similarly, a structuring
element is said to hit, or intersect, an image
if, at least for one of its pixels set to 1 the
corresponding image pixel is also 1.

referred to sebastians work check his work in PACKT.com

Image morphology

Figure 3: illustration of hit and fit

II.

Operation

I Erosion
we denote that g = f s, where f is the original matrix of the image and s is the matrix of
structuring element.
g( x, y) = 1 if s fits the f else g( x, y) = 0 something is not clear enough, and now I am going
to further show how this is done, the operation
is just take every pixel in the matrix and set
it as a center of a structuring element if the
chosen part fit the structuring element then the
center part marked as 1, else marked as 0.
I Dilation
we denote that g = f s, where f is the original matrix of the image and s is the matrix of
structuring element.
g( x, y) = 1 if s fits the f else g( x, y) = 0 something is not clear enough, and now I am going
to further show how this is done, the operation
is just take every pixel in the matrix and set
it as a center of a structuring element if the
chosen part hit the structuring element then
the center part marked as 1, else marked as 0.

II.
I.

Code and API

Open and Close operation

1 void OpenClose ( i n t , void )


2 {
3
i n t n = open_close_pos m a x _ i t e r s ;
4
i n t an = n > 0 ? n : n ;
5
Mat element = g e t S t r u c t u r i n g E l e m e n t (
element_shape , S i z e ( an 2+1 , an
2+1) , P o i n t ( an , an ) ) ;
6
if ( n < 0 )
7
morphologyEx ( s r c , dst , CV_MOP_OPEN
, element ) ;
8
else
9
morphologyEx ( s r c , dst ,
CV_MOP_CLOSE, element ) ;
10
imshow ( " Open/Close " , d s t ) ;

11 }
12
13
14 API
15 void morphologyEx ( InputArray s r c ,
OutputArray dst , i n t op , InputArray
k e r n e l , P o i n t anchor= P o i n t ( 1 , 1) , i n t
i t e r a t i o n s =1 , i n t borderType=
BORDER_CONSTANT, c o n s t S c a l a r&
borderValue=
morphologyDefaultBorderValue ( ) )

Parameters:
src - Source image. The number of channels
can be arbitrary. The depth should be one of
CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
dst - Destination image of the same size and
type as src .
element - Structuring element.
op - Type of a morphological operation that
can be one of the following:
MORPH_OPEN - an opening operation
MORPH_CLOSE - a closing operation
MORPH_GRADIENT - a morphological gradient
MORPH_TOPHAT - top hat

MORPH_BLACKHAT - black
hat

MORPH_HITMISS - hit and miss


iterations - Number of times erosion and dilation are applied.
borderType - Pixel extrapolation method. See
borderInterpolate() for details.
borderValue - Border value in case of a constant border. The default value has a special
meaning. See createMorphologyFilter() for
details.

II.

Erode and dilate operation

1 void E r o d e D i l a t e ( i n t , void )
2 {
3
int n = erode_dilate_pos max_iters ;
4
i n t an = n > 0 ? n : n ;
5
Mat element = g e t S t r u c t u r i n g E l e m e n t (
element_shape , S i z e ( an 2+1 , an
2+1) , P o i n t ( an , an ) ) ;
6
if ( n < 0 )
7
erode ( s r c , dst , element ) ;
8
else
9
d i l a t e ( s r c , dst , element ) ;
10
imshow ( " Erode/ D i l a t e " , d s t ) ;
11 }

Image morphology

According to the document in OpenCV:


opening:
dst = opening(src, element)

= dilate(erode(src, element))

T r a c k b a r C a l l b a c k onChange =0 , void
u s e r d a t a =0)

(1)

Useful to removing small objects(assuming


these objects are bright on a dark background).
closing:
dst = closing(src, element)

= erode(dilate(src, element))

(2)

Useful to removing small holes(dark regions).

III.
1

3
4

Trackbar

c r e a t e T r a c k b a r ( " i t e r a t i o n s " , " Open/


Close " ,& open_close_pos , m a x _ i t e r s
2+1 , OpenClose ) ;
c r e a t e T r a c k b a r ( " i t e r a t i o n s " , " Erode/
D i l a t e " ,& e r o d e _ d i l a t e _ p o s ,
m a x _ i t e r s 2+1 , E r o d e D i l a t e ) ;
API :
i n t c r e a t e T r a c k b a r ( c o n s t s t r i n g&
trackbarname , c o n s t s t r i n g& winname ,
i n t value , i n t count ,

Parameters:
trackbarname - Name of the created trackbar.
winname - Name of the window that will be
used as a parent of the created trackbar.
value - Optional pointer to an integer variable
whose value reflects the position of the slider.
Upon creation, the slider position is defined by
this variable.
count - Maximal position of the slider. The
minimal position is always 0.
onChange - Pointer to the function to be
called every time the slider changes position.
This function should be prototyped as void
Foo(int,void*); , where the first parameter is
the trackbar position and the second parameter
is the user data (see the next parameter). If the
callback is the NULL pointer, no callbacks are
called, but only value is updated.
userdata - User data that is passed as is to
the callback. It can be used to handle trackbar
events without using global variables.

Potrebbero piacerti anche