180 likes | 219 Views
Digital Image Processing Lecture 12: Image Topology (Suppl). Matlab inline function. INLINE Construct INLINE object. INLINE(EXPR) constructs an inline function object from the MATLAB expression contained in the string EXPR. The input
E N D
Matlab inline function INLINE Construct INLINE object. INLINE(EXPR) constructs an inline function object from the MATLAB expression contained in the string EXPR. The input arguments are automatically determined by searching EXPR for variable names (see SYMVAR). If no variable exists, 'x‘ is used. g = inline('t^2') g = Inline function: g(t) = t^2
Matlab inline function Examples: g = inline('sin(2*pi*f + theta)') g Inline function: g(f,theta) = sin(2*pi*f + theta) f=inline('abs(exp(-j*x*(0:9)) *ones(10,1))'); fplot(f,[0 2*pi])
Lookup Table Operations • 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. 2-by-2 neighborhood 3-by-3 neighborhood Each neighbourhood pixel indicated by an x, and the centre pixel is the one with a circle.
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.
Matlab makelut function Syntax : lut = makelut(fun,n) • lut = makelut(fun,n) returns a lookup table for use with applylut. funis 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.
Matlab makelut function:Example lut = 0 0 0 1 0 1 1 1 0 1 1 1 1 1 1 1 f = inline('sum(x(:)) >= 2'); lut = makelut(f,2) • 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.
Matlab makelut function:Example f=inline('sum(x(:))>= 3'); lut = makelut(f,3); • freturns 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 applylut function SyntaxA = 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. iflut consists of all 1's and 0's, A will be a binary image. stop
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 applylutuses the central part of the convolution, of the same size as BW, and adds 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.
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?
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, applylutconvolves 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 lutthat the indices point to.
Example 1 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. f = inline('sum(x(:)) >= 3'); lut = makelut(f,3); 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." lut = makelut('sum(x(:)) == 4',2); BW1 = imread('text.tif'); BW2 = applylut(BW1,lut); imshow(BW1);figure, imshow(BW2)
Example 2: find the 4-boundary of an image. A pixel is a boundary pixel if it is a foreground pixel which is 4-adjacent to a background pixel. Define a f which returns 1 iff the central pixel of the neighbourhood is a boundary pixel: f = inline('x(5)&~(x(2)*x(4)*x(6)*x(8))'); lut = makelut(f,3); 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.
c=imread('circles.tif'); cw=applylut(c,lut); imshow(c),figure,imshow(cw) Change the function to find the 8-boundary pixels; foreground pixels which are 8-adjacent to background pixels: 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);
Note that this is a stronger boundary, since more pixels are classified as boundary pixels.