digital image processing lecture 12: image topology (suppl)

18
Digital Image Processing Lecture 12: Image Topology (Suppl)

Upload: camron-earl-fields

Post on 21-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Digital Image Processing Lecture 12: Image Topology (Suppl)

Digital Image Processing Lecture 12: Image Topology (Suppl)

Page 2: Digital Image Processing Lecture 12: Image Topology (Suppl)

Matlab inline function

g = inline('t^2')

INLINE Construct INLINE object.

INLINE(EXPR) constructs an inline function object from the MATLAB expression contained in the string EXPR. The inputarguments are automatically determined by searching EXPR for variable names (see SYMVAR). If no variable exists, 'x‘ is used.

g = Inline function: g(t) = t^2

Page 3: Digital Image Processing Lecture 12: Image Topology (Suppl)

Matlab inline function

g = inline('sin(2*pi*f + theta)')

g Inline function:g(f,theta) = sin(2*pi*f + theta)

Examples:

f=inline('abs(exp(-j*x*(0:9))*ones(10,1))');

fplot(f,[0 2*pi])

Page 4: Digital Image Processing Lecture 12: Image Topology (Suppl)

Lookup Table Operations

Each neighbourhood pixel indicated by an x, and the centre pixel is the one with a circle.

A lookup table is a column vector in which each element represents the value to return for one possible combination of pixels in a neighbourhood.

The makelut function creates lookup tables for 2-by-2 and

3-by-3 neighbourhoods.

x x

x x

2-by-2 neighborhood

x x x

x x x

x x x3-by-3 neighborhood

Page 5: Digital Image Processing Lecture 12: Image Topology (Suppl)

Lookup Table Operations For a 2-by-2 neighbourhood, there are 24=16 possible

permutations of the pixels in the neighbourhood, i.e. the lookup table for this operation is a 16-element vector.

For a 3-by-3 neighbourhood, there are 29=512 permutations, so the lookup table is a 512-element vector.

Once you create a lookup table, you can use it to perform the desired operation by using the applylut function.

Order all the neighbourhoods, so that one-one correspondence between neighbourhoods and output values.

Page 6: Digital Image Processing Lecture 12: Image Topology (Suppl)

Matlab makelut function

Syntax : lut = makelut(fun,n)

lut = makelut(fun,n) returns a lookup table for use with applylut. fun is often an inline function object. The function should take a 2x2 or 3x3 matrix of 1's and 0's as input, and return a scalar. n is the size of the input to fun (2 or 3). makelut creates lut by passing all possible 2x2 or 3x3 neighbourhoods to fun, one at a time, and constructing either a 16- element vector (for 2x2 neighbourhoods), or a 512-element vector (for 3x3 neighbourhoods). The vector consists of the output from fun for each possible neighbourhood.

Page 7: Digital Image Processing Lecture 12: Image Topology (Suppl)

Matlab makelut function: Example

f returns 1 (true) if the number of 1's in the neighbourhood is 2 or greater, and returns 0 (false) otherwise.

Then makelut uses f to construct a lookup table for 2x2 neighbourhoods.

f = inline('sum(x(:)) >= 2');lut = makelut(f,2)

lut = 0 0 0 1 0 1 1 1 0 1 1 1 1 1 1 1

Page 8: Digital Image Processing Lecture 12: Image Topology (Suppl)

f=inline('sum(x(:))>= 3'); lut = makelut(f,3);

f returns 1 if three or more pixels in the 3x3 neighbourhood are 1, and 0 otherwise

lut: a 512-element vector of 1's and 0's. Each value is the output from the function ( for one of the 512 possible permutations)

Matlab makelut function: Example

Page 9: Digital Image Processing Lecture 12: Image Topology (Suppl)

Matlab applylut function

Syntax A = applylut(BW,lut)

A = applylut(BW,lut) performs a 2x2 or 3x3 neighborhood operation on binary image BW by using a lookup table (lut).

lut is either a 16-element or 512-element vector returned by makelut, which consists of output values for all possible 2x2 or 3x3 neighborhoods.

The values returned in A depend on the values in lut.

e.g. if lut consists of all 1's and 0's, A will be a binary image.

stop

Page 10: Digital Image Processing Lecture 12: Image Topology (Suppl)

Matlab applylut function (optional)

applylut performs a neighbourhood operation on a binary image by producing a matrix of indices into lut, and then replacing the indices with the actual values in lut.

2-by-2 Neighbourhoods

• 4 pixels in each neighbourhood, and 2 possible states for each pixel, so the total number of permutations is 24 = 16.• To produce the matrix of indices, applylut convolves the binary image BW with this mask.

The resulting convolution contains integer values in the range [0,15].

The applylut uses the central part of the convolution, of the same size as BW, andadds 1 to each value to shift the range to [1,16]. It then constructs A by replacing the values in the cells of the index matrix with the values in lut that the indices point to.

Page 11: Digital Image Processing Lecture 12: Image Topology (Suppl)

Neighborhood operations and masking

A mask is sometimes called a template, window or filter. The following is a 3x3 mask:

w1 w2 w3

w4 w5 w6

w7 w8 w9

If zi is the pixel in the position corresponding to wi, the mask is applied as:

w1 z1 + w2 z2 + w3 z3 + w4 z4 + w5 z5 + w6 z6 + w7 z7 + w8 z8 + w9 z9.

The mask is centered around pixel z5. What happens when wi = 1/9?

Page 12: Digital Image Processing Lecture 12: Image Topology (Suppl)

3-by-3 Neighborhoods

• There are nine pixels in each neighborhood, and 2 possible states for each pixel, so the total number of permutations is 29 = 512.

• To produce the matrix of indices, applylut convolves the binary image BW with this mask.

• The resulting convolution contains integer values in [0,511].

applylut uses the central part of the convolution, of the same size as BW, and adds 1 to each value to shift the range to [1,512].

It then constructs A by replacing the values in the cells of the index matrix with the values in lut that the indices point to.

Page 13: Digital Image Processing Lecture 12: Image Topology (Suppl)

Example 1

f = inline('sum(x(:)) >= 3');lut = makelut(f,3);BW1 = imread('text.tif'); BW2 = applylut(BW1,lut);imshow(BW1);figure, imshow(BW2)

Using lookup-table operations to modify an image. f function that returns 1 if three or more pixels in the 3x3 neighborhood are 1, and 0 otherwise.

Then call makelut, passing in this function as the first argument, and using the second argument to specify a 3-by-3 lookup table.

Page 14: Digital Image Processing Lecture 12: Image Topology (Suppl)
Page 15: Digital Image Processing Lecture 12: Image Topology (Suppl)

lut = makelut('sum(x(:)) == 4',2);

BW1 = imread('text.tif');

BW2 = applylut(BW1,lut);

imshow(BW1);figure, imshow(BW2)

An output pixel is "on" only if all four of the input pixel's neighborhood pixels are "on."

Page 16: Digital Image Processing Lecture 12: Image Topology (Suppl)

Example 2: find the 4-boundary of an image.

A pixel is a boundary pixel if it is a foregroundpixel which is 4-adjacent to a background pixel.

f = inline('x(5)&~(x(2)*x(4)*x(6)*x(8))');lut = makelut(f,3);

Define a f which returns 1 iff the central pixel of the neighbourhood is a boundary pixel:

X(5) is the central pixel of a 3x3 matrix x, x(2), x(4), x(6) and x(8) are the pixel’s 4-adjacent to the center.

Page 17: Digital Image Processing Lecture 12: Image Topology (Suppl)

c=imread('circles.tif');cw=applylut(c,lut);imshow(c),figure,imshow(cw)

f8=incline(‘x(5)&~(x(1)*x(2)*x(3)*x(4)*x(6)*x(7)*x(8)*x(9))’);lut = makelut(f8. 3);cw= applylut(c,lut);imshow(cw);

Change the function to find the 8-boundary pixels; foreground pixels which are 8-adjacent to background pixels:

Page 18: Digital Image Processing Lecture 12: Image Topology (Suppl)

Note that this is a stronger boundary, since more pixels are classified as boundary pixels.