Beruflich Dokumente
Kultur Dokumente
Introduction
• Spatial domain – image plane
Intensity Transformations and • Spatial domain processing (operations directly
on image pixels):
Spatial Filtering
p g – intensity (gray‐level) transformations
i t it ( l l) t f ti
– spatial filtering (neighborhood processing, spatial
Lecture_IP2 correlation)
• Concentrate on image enhancement methods
2009‐07‐07 1 2009‐07‐07 2
Simple Example
figure,imshow(f,[ ]);
Spatial Processing
Generic spatial domain processing:
g ( x, y ) = T [ f ( x, y )]
where f ( x, y ) is the input image, g ( x, y ) is the output image, and T
is an operator on f , defined over a specified neighborhood about point ( x, y).
Consider a rectangular neighborhood:
Center of region moved from pixel to
pixel starting at top left corner
(origin), encompassing different
neighborhoods. Operator T is
applied at each location (x,y) to yield
f=imread(‘lena.tif’); f1=mat2gray(f); output g at that location. Only local
figure,imshow(f); f2=im2uint8(f1); pixels are used in computing value of
max(f(:))=245; figure,imshow(f2); g at (x,y).
min(f(:))=24; max(f2(:))=255;
2009‐07‐07 3 2009‐07‐07 4
min(f2(:))=0;
Intensity Transformations Intensity Transformations
• g=imadjust(f, [low_in high_in],[low_out
• Neighborhood is 1x1 pixel (single pixel)
high_out], gamma);
• Value of g only depends on intensity of f at
the single pixel => intensity or gray‐level
transformation
• Intensity transformations s=T(r) where r is
intensity of f (at point (x,y)) and s is intensity • Intensity values below low_in and above high_in are clipped
• image can be of class uint8, uint16 or double
of g (at point (x,y)) • output image of same class as input image
• all parameters are between 0 and 1
• [ ] empty results in default [0 1]
• if high_out < low_out, output intensity is reversed
• parameter gamma specifies shape of the curve
2009‐07‐07 5 2009‐07‐07 6
1
2009-07-07
Example of Image Transformation Example of Image Transformation
g2=imadjust(f,[0.5 0.75], [0 1]);
f=imread(‘breast.tif’); function stretchlim as an aid to imadjust
imshow(f); g1=imadjust(f,[0 1],[1 0]); g3=imadjust(f,[ ],[ ],2); Low_High = stretchlim(f);
Low_High = [low high] where low is level
of bottom 1% and high is level of top 1%
Logarithmic and Contrast Stretching
Example of Logarithmic Transform
Transformations
• Logarithmic transformation:
g=c*log(1+double(f)) (c=constant)
• Similar to gamma curve but with fixed shape
• Used to compress dynamic range (of Fourier spectrum; peak
magnitude of 10**6 reduced to peak log magnitude of
i d f 10**6 d d kl i d f
loge(10**6)=13.8; can then see low level effects present in
Fourier spectrum of images)
• Use full range of display of compressed values [0 255] using Linear Scale Log Scale
code:
f=Fourier Spectrum (range [0 ‐‐ 1.5e6])
gs=im2uint8(mat2gray(g)); % linear scale
‐ mat2gray brings values to the range [0 1] g=im2uint8(mat2gray(log(1+double(f))));
imshow(g);
2009‐07‐07
‐ im2uint8 bring values to the range [0 255] 9 2009‐07‐07 10
Contrast Stretching Transforms Intensity Transformations
• function g = intrans(f, varargin); % m‐file to perform
E=20 range of intensity (gray‐level) transformations
• g = intrans(f, ‘neg’); % computes negative of input
image f
• g
g = intrans(f,
intrans(f, ‘log’,
log , C, class); % computes C
C, class); % computes C*log(1+f)
log(1 f)
Thresholding function – • g = intrans(f, ‘gamma’, GAM); % performs a gamma
Contrast stretching transformation limiting case of contrast
– compresses input levels lower stretching – used for image transformation on the input image using parameter
than m into a narrow range of dark segmentation. 1 GAM
s = T (r ) =
levels; compresses input values 1 + (m / r ) E • g = intrans(f, ‘stretch’, M, E); % computes a contrast‐
above m into a narrow band of
light levels;
r = input intensity stretching transformation using expression
• results in image of higher s = output intensity 1./(1+M./(F+eps)).^E; M in range [0 1]
contrast E controls slope
g=1./(1+(m./(double(f)+eps)).^E);
2009‐07‐07 eps prevents overflow if f=0 11 2009‐07‐07 12
2
2009-07-07
function g=intrans(f,varargin
g=intrans(f,varargin)) function g=intrans(f,varargin
g=intrans(f,varargin))
function g = intrans(f, varargin) % Verify the correct number of inputs.
%INTRANS Performs intensity (gray-level) transformations. error(nargchk(2, 4, nargin))
% G = INTRANS(F, 'neg') computes the negative of input image F. case 'gamma'
% % Store the class of the input for use later. if length(varargin) < 2
% G = INTRANS(F, 'log', C, CLASS) computes C*log(1 + F) and classin = class(f);
% multiplies the result by (positive) constant C. If the last two error('Not enough inputs for the gamma option.')
% parameters are omitted, C defaults to 1. Because the log is used % If the input is of class double, and it is outside the range
end
% frequently to display Fourier spectra, parameter CLASS offers the % [0, 1], and the specified transformation is not 'log', convert the gam = varargin{2};
% option to specify the class of the output as 'uint8' or % input to the range [0, 1]. g = imadjust(f, [ ], [ ], gam);
% 'uint16'. If parameter CLASS is omitted, the output is of the if strcmp(class(f), 'double') & max(f(:)) > 1 & ...
% same class as the input. ~strcmp(varargin{1}, 'log')
% f = mat2gray(f); case 'stretch'
% G = INTRANS(F, 'gamma', GAM) performs a gamma else % Convert to double, regardless of class(f). if length(varargin) == 1
transformation on
% the input image using parameter GAM (a required input).
f = im2double(f); % Use defaults.
end
% m = mean2(f);
% G = INTRANS(F,
INTRANS(F 'stretch'
stretch , M,
M E) computes a contrast-stretching
contrast stretching % Determine the type of transformation specified. E=4 4.0;
0;
% transformation using the expression 1./(1 + (M./(F + method = varargin{1}; elseif length(varargin) == 3
% eps)).^E). Parameter M must be in the range [0, 1]. The default
% value for M is mean2(im2double(F)), and the default value for E m = varargin{2};
% Perform the intensity transformation specified.
% is 4. switch method
E = varargin{3};
% case 'neg' else error('Incorrect number of inputs for the stretch
% For the 'neg', 'gamma', and 'stretch' transformations, double g = imcomplement(f); option.')
% input images whose maximum value is greater than 1 are scaled
% first using MAT2GRAY. Other images are converted to double first end
case 'log'
% using IM2DOUBLE. For the 'log' transformation, double images if length(varargin) == 1 g = 1./(1 + (m./(f + eps)).^E);
are c = 1; otherwise
% transformed without being scaled; other images are converted to elseif length(varargin) == 2
% double first using IM2DOUBLE.
error('Unknown enhancement method.')
c = varargin{2};
% end
elseif length(varargin) == 3
% The output is of the same class as the input, except if a c = varargin{2};
% different class is specified for the 'log' option. classin = varargin{3}; % Convert to the class of the input image.
else g = changeclass(classin, g);
% Copyright 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins error('Incorrect number of inputs for the log option.')
% Digital Image Processing Using MATLAB, Prentice-Hall, 2004 end
2009‐07‐07
% $Revision: 1.7 $ $Date: 2003/10/13 00:45:53 $
13 2009‐07‐07 14
g = c*(log(1 + double(f)));
Example of Contrast Stretching Arbitrary Intensity Transformations
i Let T denote a column vector containing
the values of the arbitrary transformation
function
i for an 8 - bit image (uint8), T(1) is the value
to which intensity 0 is mapped; T(2) is the value • for each pixel value in f,
to which intensity 1 is mapped; ...; T(256) is the interp1 finds that value in the
value to which intensity 255 is mapped abscissa (z)
i the utility of the arbitrary transformation is • interp1 the interpolates the
improved if both input and output images are corresponding value in T and
represented as floating point values in range [0 1] outputs the interpolated value
i we can readily implement such intensity mappings to g in the corresponding pixel
using the function interp1 as : location
g = interp1(z, T, f);
g=intrans(f, ‘stretch’, mean2(im2double(f)), 0.9); f is the input image
figure, imshow(g); g is the output (transformed) image
• mean2 computes mean value of f and used as value for m T is the column vector of transformation values
• image f was converted to double using im2double to scale its values to z is a column vector of length of T but scaled to [0 1] range
range [0,1] so that mean would also be in this range using formula z = linspace(0, 1, numel(T))'; (this is equivalent
• value of E, contrast stretching parameter, determined experimentally to z = 0 :1/ numel(T) :1)'
2009‐07‐07 15 2009‐07‐07 16
Histogram Processing Histogram Processing
i Histogram of digital image with L intensity levels in range [0, G ]
is defined as:
h(rk ) = nk
where rk is k -th intensity level in interval [0, G ] and nk is the
number of pixels in the image whose intensity level is rk .
i G is 255 for images of class uint8; 65535 for images of class uint16,
and 1.0 for images of class double.
i Normalized
o ed histograms
s og s (n = total
o number
u be oof ppixels
e s in image):
ge):
h( r ) n
p (rk ) = k = k , k = 0,1, 2,..., L − 1
n n
i p (rk ) is an estimate of the probability of occurrence of intensity level rk
MATLAB: h = imhist(f, b) f=imread(‘lena.tif’); f=imread(‘lena.tif’);
f is input image z=imhist(f,256); h2=imhist(f,26);
h is histogram horz1=1:256; horz2=1:10:256;
b is number of bins (256 default) bar(horz1,z); bar(horz2,h2);
Normalized Histogram: bar(horz, z, width);
p = imhist(f, b) / numel(f) z is row vector with histogram values
numel(f) = total number of pixels in image horz is vector of increments of horizontal scale
2009‐07‐07 17 2009‐07‐07 18
width is bar width in range [0 1]
3
2009-07-07
Histogram Examples Additional Graphics Commands
imhist(f)
h=imhist(f); • axis ([horzmin horzmax vertmin vertmax]);
h1=h(1:10:256);
horz=1:10:256;
• axis tight; % determine horizontal and vertical min
bar(horz,h1); and max automatically from data being plotted
axis([0 255 0 15000]);
set(gca,’xtick’,0:50:255); • gca: get current axis; used for setting x/y‐axis tick
set(gca,’ytick’,0:2000:15000);
( )
locations
• grid on; % turns on grid plotting
• xlabel(‘stringx’, ‘fontsize’, size);
• ylabel(‘stringy’, ‘fontsize’, size);
• text(xloc, yloc, ‘string’, ‘fontsize’, size);
• title (‘titlestring’);
stem(horz, v, ’color_linestyle_marker’, ’fill’)
2009‐07‐07
plot(h) 19 2009‐07‐07 20
Histogram Examples
Graphical Line Markers
h1n=h1/numel(f); h2n=h2/numel(f);
stem(horz1, h1n, 'r-o', 'fill'); stem(horz2, h2n, ‘b-o', 'fill');
Handle Plot Example Histogram Equalization
fhandle = @tanh; % plot hyperbolic tangent
fplot(fhandle, [‐2 2], ‘r:x’, 200); plot 201 points in pr (r ) denotes the probability density function (PDF) of intensity levels in a
region between ‐2 and 2 given image. Consider the transform:
r
s = T (r ) = ∫ pr ( w)dw
0
2009‐07‐07 23 2009‐07‐07 24
4
2009-07-07
Histogram Equalization Example Histogram Processing
MATLAB Code:
For digital images histogram equalization becomes :
imshow(f); % upper image
k k nj
sk = T (rk ) = ∑ pr (rj ) = ∑ k = 0,1, 2,..., L − 1 figure, imhist(f); % upper histogram
j =0 j =0 n ylim(‘auto’); % y‐axis limits
g = histeq(f, nlev) g=histeq(f, 256); % hist equalization
f = input image figure imshow(g); % bottom image
figure, imshow(g); % bottom image
figure, imhist(g); % lower histogram
nlev is the number of intensity levels for output image (default = 64) ylim(‘auto’); % y‐axis limits
i where sk is the intensity in the output image corresponding to
value rk in the input image Improvements in average
intensity and contrast easily seen
in bottom image;
Improved histogram uniformity
also clearly seen in bottom
histogram;
Bottom histogram is not exactly
2009‐07‐07 25 flat.
2009‐07‐07 26
Example Histogram Processing Histogram Matching
Cumulative sum of normalized histogram values:
• Histogram equalization achieves enhancement
MATLAB Code:
by spreading the levels of the input image
hnorm = imhist(f) ./ numel(f);
cdf = cumsum(hnorm);
over a wider range of the intensity scale.
x = linspace(0, 1, 256);
x linspace(0, 1, 256);
plot(x, cdf);
• In some applications it is desirable to specify
In some applications it is desirable to specify
axis([0 1 0 1]);
set(gca, ’xtick’, 0:.2:1);
the shape of the histogram that we wish the
set(gca, ’ytick’, 0:.2:1); processed image to have.
xlabel(‘Input intensity values’, ’fontsize’, 9);
See that narrow range of input
ylabel(‘Output intensity values’, ’fontsize’, 9);
text(0.18, 0.5, ’Transformation function’, ’fontsize’, 9);
• This process is called histogram matching or
intensity levels transformed into
full intensity scale in output
histogram specification.
image.
2009‐07‐07 27 2009‐07‐07 28
Histogram Matching Example of Histogram Equalization
Let r and z denote intensity levels of input and output images with f
probability density functions pr (r ) and pz ( z ). Then the transformation:
r
imhist(f);
s = T (r ) = ∫ pr ( w)dw
0
MATLAB Code:
gives intensity levels s that have a uniform PDF, ps ( s). Define a variable
z with the p
property
p y g=histeq(f, hspec);
z f is input image
H ( z ) = ∫ pz ( w)dw = s hspec is specified histogram (row vector)
0 g is output image
Goal is to define an image with intensity levels z which have the specified
density pz ( z ). From the equations above it follows that Upper left: original image
Upper right: original histogram
z = H −1 ( s ) = H −1[T (r )] Lower left: image from equalized histogram
Using histogram equalization we can find T ( r ) from input image. Hence Lower right: equalized histogram
we can use above equation to find the transformed levels z whose PDF is
pz ( z ) as long as we can find H −1 (always the case with discrete variables). Result: washed out image using
g histeq(f, 256);
2009‐07‐07 29 2009‐07‐07
histogram equalization!! 30
5
2009-07-07
Example of Histogram Matching Spatial Filtering
• Neighborhood processing:
– defining a center point, (x,y)
Upper Left: Bimodal Gaussian – performing an operation involving only pixels in
form for specified histogram neighborhood about center point
Lower Left: Histogram resulting – letting result of that operation be the response of the
letting result of that operation be the response of the
from histogram matching to process at that point
bimodal Gaussian (but with a – repeating the process for every point in the image.
smoother transition of levels in
the dark region of the intensity • Every image point is the center of a new
scale (near 0 image intensity) neighborhood
Upper Right: Image obtained by • Alternative filtering approach in the frequency
histogram matching
domain
2009‐07‐07 31 2009‐07‐07 32
Linear Spatial Filtering Linear Spatial Filtering
• Linear operations consist of multiplying each • Move center of filter mask w from
pixel in the neighborhood by a corresponding point‐to‐point in image, f.
• At each point (x,y) the response of the
coefficient and summing the results to obtain filter is the sum of products of the filter
the response at each point (x,y) coefficients and the corresponding
neighborhood pixels in the area
neighborhood pixels in the area
• For neighborhoods of size m x n, mn spanned by the filter mask.
• For mask of size m x n, assume
coefficients are required. m=2a+1, n=2b+1, where a and b are
non‐negative integers (odd sized masks)
• The matrix of coefficients is called a filter, a • Correlation operations process mask as
mask, a filter mask, a kernel, a template or a shown in figure.
• Convolution operations need w to be
window rotated by 180 degrees prior to passing
it by f.
2009‐07‐07 33 2009‐07‐07 34
Linear Spatial Filtering
Linear Spatial Filtering –– 1D Signal Linear Spatial Filtering –– 2D Signals
Linear Spatial Filtering
(a) Original signal and mask
(b) Starting positions alignments
(c) zero padding of signal at
beginning and end
((d)) Position after a single shift
g
(e) Position after four shifts
(f) Final position
(g) Full correlation/convolution
results
(h) Correlation/convolution
results with initial signal
frame aligning with the
center of the mask
2009‐07‐07 35 2009‐07‐07 36
6
2009-07-07
Spatial Filtering Spatial Filtering
Spatial Filtering ‐‐
‐‐ MATLAB
i Correlation of a filter mask w( x, y ) of size m × n
• g = imfilter(f, w, filtering_mode, boundary_options, size_options);
with a function f ( x, y ), denoted by w( x, y ) ⊕ f ( x, y )
• f is input image
is given by :
• w is filter mask
a b
w( x, y ) ⊕ f ( x, y ) = ∑ ∑ w(s, t ) f ( x + s, y + t )
s =− a t =− b
• g is filtered result
Spatial Filtering ‐‐
Spatial Filtering ‐‐ MATLAB
Spatial Filtering
Spatial Filtering ‐‐
‐‐ MATLAB Original Image Zero Padding
Replicate
Borders
• Most common usage:
w=ones(31); % should
– g = imfilter(f, w, ’replicate’); be:
– filters are pre‐rotated by 180 degrees so we can w=ones(31)/(31).^2; %
use correlation default in imfilter for proper normalization
• rot90(w, 2); % rotate mask by 180 degrees
rot90(w 2); % rotate mask by 180 degrees
– g = imfilter(f, w, ’conv’, ’replicate’);
– filter is not pre‐rotated and we want convolution Symmetric Circular Borders uint8 image with
Borders (same as zero replicate option
– imfilter converts output image to same class as padding) (clipping at edges)
input image (need to be careful about exceeding (a)
(b)
f is image of size 512 x 512 pixels; w is filter of 31 x 31 ones;
gd=imfilter(f, w); imshow(gd, [ ]); edges blurred and boundary blurred
range of integer type leading to truncation, as well (c) gr=imfilter(f, w, ’replicate’); figure, imshow(gr, [ ]); boundary blurring removed
as rounding issues for integer formats) (d)
(e)
gs=imfilter(f, w, ’symmetric’); figure,imshow(gs, [ ]); boundary blurring removed
gc=imfilter(f, w, ’circular’); figure, imshow(gc, [ ]); blurring due to signal periodicity
assumption
(f) f8=im2uint8(f); g8r=imfilter(f8, w, ’replicate’); figure, imshow(g8r, [ ]); clipping
2009‐07‐07 39 caused data loss since coefficients of mask did not sum to [0 1] range resulting in
2009‐07‐07 40
filtered output outside the [0 255] range
Image Processing Toolbox Nonlinear Spatial Filtering
• nonlinear spatial filtering is again based on
neighborhood operations on the pixels of the image
– linear spatial filtering is based on computing sums of
products (linear operations)
w=fspecial(‘type’ ,parameters);
f i l(‘t ’ t )
‘type’ specifies filter type (see table)
– nonlinear spatial filtering is based on nonlinear operations
parameters define the specified filter in the neighborhood of the pixels; e.g., letting the
response at each center pixel be equal to the maximum
pixel value in its neighborhood
• concept of a mask (filtering function) is not as
prevalent in nonlinear processing
2009‐07‐07 41 2009‐07‐07 42
7
2009-07-07
2009‐07‐07 43 2009‐07‐07 44
colfilt for Nonlinear Spatial
function handles
Filtering
• syntax for colfilt is: • a function handle is a MATLAB data type that
g = colfilt(f, [m n], ‘sliding’, fun) contains information used in referencing a
where function
fi h i
f is the input image
i • simple function handle such as:
simple function handle such as:
m and n are dimensions of the filter region – f = @sin; f(pi/4)=0.7071
‘sliding’ indicates that we slide the m x n region • anonymous function handle such as:
from pixel to pixel in the input image f – @ (input‐argument‐list) expression
fun is a function handle – g = @(x) x.^2; g(3)=9
– r = @(x, y) sqrt(x.^2 + y.^2); f(3, 4)=5
2009‐07‐07 45 2009‐07‐07 46
function handles colfilt
• function handles can be used as arguments in • function fun must operate on each of the columns of A
individually and return a row vector, v, whose k‐th element is
MATLAB calls the result of the operation performed by fun on the k‐th
• quad is MATLAB function that numerically column of A
integrates a mathematical function over a set
integrates a mathematical function over a set • the input image, f, must be padded explicitly before filtering
the input image f must be padded explicitly before filtering
using the function padarray which has the syntax
of limits; quad(@sin, x1, x2); quad(f,x1,x2); – fp = padarray(f, [r c], method, direction)
• quad(f, 0, pi/4)=0.2929; – f is the input image
– fp is the padded image
• quad(g, 0, 1)=0.3333 – [r c] gives the number of rows and columns by which to pad f
– method and direction are padding options
2009‐07‐07 47 2009‐07‐07 48
8
2009-07-07
colfilt Example –
Example – Simple Mean colfilt Example –
Example – Simple Mean
• f=imread('lena.tif');
• m=3; n=3;
• fp=padarray(f,[m n],'replicate','both'); % both
directions
• g=colfilt(fp,[m n],'sliding',@mean);
• [M,N]=size(f);
• gp=uint8(g((1:M)+m,(1:N)+n)); restore original size
• figure;imshow(f); % plot original lena
• figure;imshow(gp); % plot 2 x 2 mean smoothed lena
2009‐07‐07 51 2009‐07‐07
Original lena.tif 2 x 2 Mean Processed lena.tif52
9
2009-07-07
⎡0 1 0 ⎤
w = ⎢⎢1 −4 1 ⎥⎥
Laplacian mask/filter is a derivative operator – it sharpens the
image but drives constant areas to zero;
Adding the original image back restores the gray-level tonality
2009‐07‐07 ⎣⎢0 1 0 ⎥⎦ 57 2009‐07‐07 58
10
2009-07-07
MATLAB Commands Summary Lecture Summary
• g = imadjust(f, [low_in high_in], [low_out high_out], gamma);
– low_in, high_in in [0 1] range; low_out, high_out in [0 1] range; 0 ≤ γ ≤ big
• g = imcomplement(f); % inverts image intensity • Intensity transformations and spatial filtering
– imcomplement(f) == imadjust(f, [0 1], [1 0]); are the foundation for many aspects of image
g = c * log(1 + double(f)); % log intensity transform
•
– return to image scale using gs = im2uint8(mat2gray(g)); % mat2gray scales to [0 1];
processing, such as:
im2uint8 scales to [0 255] – image restoration
• g = gscale(f, method, low, high); % scales to image range [0 255]; method = ‘full8’
or ‘minmax’ – noise reduction
• p = imhist(f, b)/numel(f); % histogram computation; b=# bins; – noise‐generation
– bar(horz, v, width); % bar histogram with v = histogram values; horz = size(v) =
increments of horizontal scale; width in [0 1] and is the bar width (0.8 standard) • Spatial masks used extensively in edge
• g = histeq(f, nlev); % equalize (approximately) histogram; nlev = number intensity
levels detection and image segmentation
• g = imfilter(f, w, ‘replicate’); % filter image with w applications
– g = imfilter(f, w, ‘conv’, ‘replicate’); full set of options; ‘conv’ or ‘corr’; P = padding by P –
valued samples or ‘replicate’ boundary values
2009‐07‐07 61 2009‐07‐07 62
11