# Neighborhood Processing

## Presentation on theme: "Neighborhood Processing"— Presentation transcript:

Neighborhood Processing
Digital Image Processing

A rectangle (usually with sides of odd length) or other shape over the given image

a 3X5 mask corresponding pixel values

Spatial Filtering Position mask over current pixel
Form all products of filter elements with the corresponding elements of the neighborhood Add all the product Repeat for every pixel in image

Spatial Convolution Similar to spatial filtering, except ..
The filter must be rotated by 180° before multiplying and adding Or, equivalently, The rationale of “convolution” will become apparent in view of Fourier Transform

Mean Filter Example Given a 3×3 mask, take the average of all nine values within the mask The result of filtering x with 3×3 averaging filter

Mask Notation It is convenient to describe a linear filter simply in terms of the coefficients of all the gray values of pixels within the mask The averaging filter The other filter example Ch5-p.91-92

Mask on the Edge What happens at the edge of the image, where the mask partly falls outside the image? There are a number of different approaches to dealing with this problem Ignore the edges (like the example shown previously) Pad with zeros Mirroring

Filtering in MATLAB imfilter(image,filter,options..)
filter2(filter, image, shape) The result is a matrix of data type “double” shape is optional; it describes the method for dealing with the edges ‘same’ – pad with zeros ‘valid’ – ignore the edges ‘full’ – pad with zeros and applying the filter at all places on and around the image where the mask intersects the image matrix imfilter(image,filter,options..) More advanced image filtering function

Filtering Examples in MATLAB (1/2)
>> x = uint8(10*magic(5)) >> a = ones(3,3)/9 >> filter2(a,x,'same') >> filter2(a,x,'valid')

Filtering Examples in MATLAB (2/2)
>> filter2(a,x,'full') filter2 provides no mirroring option Mirroring can be realized by >> D = floor(size(a)/2) >> wr=D(1);wc=D(2); % get half of the mask size >> m_x=[x(wr:-1:1,:);x;x(end:-1:end-(wr-1),:)] % additional mirrored-rows >> m_x=[m_x(:,wc:-1:1),m_x,m_x(:,end:-1:end-(wc-1))]; % additional mirrored columns >> filter2(a,m_x,'valid')

Pre-cooked Filtering in MATLAB: fspecial()
I = imread('cameraman.tif'); subplot(2,2,1);imshow(I);title('Original Image'); H = fspecial('motion',20,45); MotionBlur = imfilter(I,H,'replicate'); subplot(2,2,2);imshow(MotionBlur);title('Motion Blurred Image'); H = fspecial('disk',10); blurred = imfilter(I,H,'replicate'); subplot(2,2,3);imshow(blurred);title('Blurred Image'); H = fspecial('unsharp'); sharpened = imfilter(I,H,'replicate'); subplot(2,2,4);imshow(sharpened);title('Sharpened Image');

Low- and High-Pass Filters (1/2)
Frequencies of an image are a measure of the amount by which gray values change with distance High-pass filter (left) Low-pass filter (right) >> f = fspecial('laplacian') >> cf = imfilter(c,f); >> f1 = fspecial('log') % Laplacian of Gaussian >> cf1 = imfilter(c,f1); Ch5-p.98

Low- and High-Pass Filters (2/2)
Values outside [0,255] range Make negative value positive Clip values 0-255 scaling transformation >> f2 = [1 -2 1; ; ]; >> cf2 = filter2(f2,c); >> figure, imshow(mat2gray(cf2)); >> % scaling transformation >> maxcf2 = max(cf2(:)); >> mincf2 = min(cf2(:)); >> cf2g = (cf2-mincf2)/(maxcf2-mincf2); >> figure, imshow(cf2/60);

Gaussian Filters (1/2) Ch5-p.103 >> a=50;s=3;
>> g=fspecial('gaussian',[a a],s); >> surf(1:a,1:a,g) >> s = 9; >> g2 = fspecial('gaussian',[a a],s); >> figure, surf(1:a,1:a,g2) Ch5-p.103

Gaussian Filters (2/2) g3 g4 g1 g2
>> c = imread('cameraman.tif'); >> g1 = fspecial('gaussian',[5 5]); >> g2 = fspecial('gaussian',[5 5],2); >> g3 = fspecial('gaussian',[11 11],1); >> g4 = fspecial('gaussian',[11 11],5); >> imshow(filter2(g1,c)/256); >> figure, imshow(filter2(g2,c)/256); >> figure, imshow(filter2(g3,c)/256); >> figure, imshow(filter2(g4,c)/256); g3 g4 g1 g2

>> c = imread(‘cameraman.tif’); >> f = fspecial('average'); >> xf = filter2(f,c); >> xu = double(c)-xf/1.5; >> imshow(xu/70); Ch5-p.106

Unsharp Masking The unsharp option of fspecial produces such filters
>> p = imread('pelicans.tif'); >> u = fspecial('unsharp',0.5); >> pu = filter2(u,p); >> imshow(p), figure, imshow(pu/255); α = 0.5 Ch5-p.108

High-Boost Filtering Allied to unsharp masking filters are the high-boost filters where A is an amplification factor If A = 1, then the high-boost filter becomes an ordinary high-pass filter >> id = [0 0 0;0 1 0; 0 0 0]; >> f = fspecial('average'); >> hb1 = 3*id-2*f; >> hb2 = 1.25*id-0.25*f; >> x1 = filter2(hb1,p); >> x2 = filter2(hb2,p); >> figure, imshow(x1/255); >> figure, imshow(x2/255);

Nonlinear Filters Maximum filter Minimum filter
>> cmax = nlfilter(c,[3 3],'max(x(:))'); >> cmin = nlfilter(c,[3 3],'min(x(:))'); >> figure, imshow(cmax); >> figure, imshow(cmin); Ch5-p.112

Region of Interest Processing
>> ig = imread('iguana.tif'); >> roi = roipoly(ig,[ ],[ ]); >> figure, imshow(ig); >> figure, imshow(roi); >> roi_2 = roipoly(ig); % interactively select ROI Ch5-p.115

Region of Interest Filtering
>> a = fspecial('average',[15 15]); >> iga = roifilt2(a,ig,roi); >> imshow(iga); >> u = fspecial('unsharp'); >> igu = roifilt2(u,ig,roi); >> figure, imshow(igu); >> L = fspecial('log'); >> igl = roifilt2(L,ig,roi); >> figure, imshow(igl); iga: average filtering igu: unsharp masking igl: Laplacian of Gaussian Ch5-p.116