Images for testing
images_r.m
kaist.jpg
lib.bmp lib2.bmp
5
lena.bmp
rerc.bmp
tool.bmp
2009 Optomechatronics
Image reading and showing
image_r_s.m
%image reading
I = imread( ‘tao.jpg’ );
%image showing
figure; imshow(I);figure; imshow(I);
6
2009 Optomechatronics
Image Histogram
image_histogram.m
%estimating the histogram
H = imhist( I );
%histogram showing
figure; bar(H);figure; bar(H);
7
2009 Optomechatronics
Histogram equalization
ihit
%histogram equalization
J = histeq(I);
image_histogram_eq.m
J = histeq(I);
figure; imshow(J);
8
2009 Optomechatronics
HISTEQ Enhance contrast using histogram equalization.
HISTEQ enhances the contrast of images by transforming the values in an intensity image, or the values in the colormap
of an indexed image, so that the histogram of the output image approximately matches a specified histogram.
J HISTEQ(I HGRAM) t f th i t it i I th t th hi t f th t t i J ith l th(HGRAM) biJ = HISTEQ(I,HGRAM) transforms the intensity image I so that the histogram of the output image J with length(HGRAM) bins
approximately matches HGRAM. The vector HGRAM should contain integer counts for equally spaced bins with intensity
values in the appropriate range: [0,1] for images of class double, [0,255] for images of class uint8, and [0,65535] for
images of class uint16. HISTEQ automatically scales HGRAM so that sum(HGRAM) = prod(size(I)). The histogram of J will
better match HGRAM when length(HGRAM) is much smaller than the number of discrete levels in I.
J = HISTEQ(I,N) transforms the intensity image I, returning in J an intensity image with N discrete levels. A roughly equalJ HISTEQ(I,N) transforms the intensity image I, returning in J an intensity image with N discrete levels. A roughly equal
number of pixels is mapped to each of the N levels in J, so that the histogram of J is approximately flat. (The histogram of J
is flatter when N is much smaller than the number of discrete levels in I.) The default value for N is 64.
[J,T] = HISTEQ(I) returns the gray scale transformation that maps gray levels in the intensity image I to gray levels in J.
NEWMAP = HISTEQ(X,MAP,HGRAM) transforms the colormap associated with the indexed image X so that the histogram ofNEWMAP HISTEQ(X,MAP,HGRAM) transforms the colormap associated with the indexed image X so that the histogram of
the gray component of the indexed image (X,NEWMAP) approximately matches HGRAM. HISTEQ returns the transformed
colormap in NEWMAP. length(HGRAM) must be the same as size(MAP,1).
NEWMAP = HISTEQ(X,MAP) transforms the values in the colormap so that the histogram of the gray component of the
indexed image X is approximately flat. It returns the transformed colormap in NEWMAP.
NEWMAP,T] = HISTEQ(X, ) returns the gray scale transformation T that maps the gray component of MAP to the gray
component of NEWMAP.
9
2009 Optomechatronics
Noise add and filtering
Salt & pepper noise -> median filtering
salt pepper median m
%Salt & pepper noise add
J = imnoise(I,'salt & pepper', 0.02);
salt_pepper_median.m
(, p pp , )
figure; imshow(J);
%median filtering
f2=medfilt2(J,[3 3]);
figure; imshow(f2);
10
2009 Optomechatronics
IMNOISE Add noise to image.
J = IMNOISE(I,TYPE, ) Add noise of a given TYPE to the intensity image I. TYPE is a string that can have one of these values:
'gaussian' Gaussian white noise with constant mean and variance
'localvar' Zero-mean Gaussian white noise with an intensity-dependent variance
'poisson' Poisson noise
'salt & pepper' "On and Off" pixels
'speckle' Multiplicative noisepp
Depending on TYPE, you can specify additional parameters to IMNOISE. All numerical parameters are normalized; they correspond
to operations with images with intensities ranging from 0 to 1.
J = IMNOISE(I,'gaussian',M,V) adds Gaussian white noise of mean M and variance V to the image I. When unspecified, M and V
default to 0 and 0.01 respectively.
J = imnoise(I,'localvar',V) adds zero-mean, Gaussian white noise of local variance, V, to the image I. V is an array of the same
size as I.
J = imnoise(I,'localvar',IMAGE_INTENSITY,VAR) adds zero-mean, Gaussian noise to an image, I, where the local variance of the
noise is a function of the image intensity values in I. IMAGE_INTENSITY and VAR are vectors of the same size, and
()PLOT(IMAGE_INTENSITY,VAR) plots the functional relationship between noise variance and image intensity. IMAGE_INTENSITY must
contain normalized intensity values ranging from 0 to 1.
J = IMNOISE(I,'poisson') generates Poisson noise from the data instead of adding artificial noise to the data. In order to respect
Poisson statistics, the intensities of uint8 and uint16 images must correspond to the number of photons (or any other quanta of
information). Double-precision images are used when the number of photons per pixel can be much larger than 65535 (but less
than 10^12); the intensities values vary between 0 and 1 and correspond to the number of photons divided by 10^12than 10 12); the intensities values vary between 0 and 1 and correspond to the number of photons divided by 10 12.
J = IMNOISE(I,'salt & pepper',D) adds "salt and pepper" noise to the image I, where D is the noise density. This affects
approximately D*PROD(SIZE(I)) pixels. The default for D is 0.05.
J = IMNOISE(I,'speckle',V) adds multiplicative noise to the image I, using the equation J = I + n*I, where n is uniformly distributed
random noise with mean 0 and variance V The default for V is 0 04
11
random noise with mean 0 and variance V. The default for V is 0.04.
2009 Optomechatronics
MEDFILT2 Perform 2-D median filtering.
B = MEDFILT2(A [M N]) performs median filtering of the matrix A in twoB MEDFILT2(A,[M N]) performs median filtering of the matrix A in two
dimensions. Each output pixel contains the median value in the M-by-N
neighborhood around the corresponding pixel in the input image.
MEDFILT2 pads the image with zeros on the edges, so the median
values for the points within [M N]/2 of the edges may appear distorted.
B = MEDFILT2(A) performs median filtering of the matrix A using the default
3-by-3 neighborhood.
B = MEDFILT2( ,PADOPT) controls how the matrix boundaries are padded.
PADOPT may be 'zeros' (the default), 'symmetric', or 'indexed'. If ADOPT
is 'zeros', A is padded with zeros at the boundaries. If PADOPT is
'
symmetric', A is symmetrically extended at the boundaries. If PADOPT issymmetric , A is symmetrically extended at the boundaries. If PADOPT is
'indexed', A is padded with ones if it is double; otherwise it is padded
with zeros.
12
2009 Optomechatronics
Gaussian noise -> mean filtering
gaussian_mean.m
%Gaussian noise add
J = imnoise(I, 'gaussian', 0, 0.001);
fi i h (J)figure; imshow(J);
%mean filtering
H fspecial('average' [3 3] );H = fspecial('average', [3 3] );
f2 = imfilter(I,H);
figure; imshow(f2);
13
2009 Optomechatronics
FSPECIAL Create 2-D special filters.
H = FSPECIAL(TYPE) creates a two-dimensional filter H of the specified type. Possible values for TYPE are:
'average' averaging filter
'disk' circular averaging filtergg
'gaussian' Gaussian lowpass filter
'laplacian' filter approximating the 2-D Laplacian operator
'log' Laplacian of Gaussian filter
'motion' motion filter
'prewitt' Prewitt horizontal edge-emphasizing filter
'sobel' Sobel horizontal edge-emphasizing filter
'unsharp' unsharp contrast enhancement filter
Depending on TYPE, FSPECIAL may take additional parameters which you can supply. These parameters all have default values.
H = FSPECIAL('average',HSIZE) returns an averaging filter H of size HSIZE. HSIZE can be a vector specifying the number of rows and
columns in H or a scalar, in which case H is a square matrix. The default HSIZE is [3 3].
H = FSPECIAL('disk',RADIUS) returns a circular averaging filter (pillbox) within the square matrix of side 2*RADIUS+1. The default RADIUS
is 5.
H = FSPECIAL('gaussian',HSIZE,SIGMA) returns a rotationally symmetric Gaussian lowpass filter of size HSIZE with standard deviation
SIGMA (positive). HSIZE can be a vector specifying the number of rows and columns in H or a scalar, in which case H is a square matrix.
The default HSIZE is [3 3], the default SIGMA is 0.5.
H = FSPECIAL('laplacian',ALPHA) returns a 3-by-3 filter approximating the shape of the two-dimensional Laplacian operator. The
parameter ALPHA controls the shape of the Laplacian and must be in the range 0.0 to 1.0. The default ALPHA is 0.2.
H = FSPECIAL('log',HSIZE,SIGMA) returns a rotationally symmetric Laplacian of Gaussian filter of size HSIZE with standard deviation
SIGMA (positive). HSIZE can be a vector specifying the number of rows and columns in H or a scalar, in which case H is a square matrix.
The default HSIZE is [5 5], the default SIGMA is 0.5.
H = FSPECIAL('motion',LEN,THETA) returns a filter to approximate, once convolved with an image, the linear motion of a camera byLEN
pixels, with an angle of THETA degrees in a counter-clockwise direction. The filter becomes a vector for horizontal and vertical motions.
The default LEN is 9, the default THETA is 0, which corresponds to a horizontal motion of 9 pixels.
H = FSPECIAL('prewitt') returns 3-by-3 filter that emphasizes horizontal edges by approximating a vertical gradient. If you need to
emphasize vertical edges, transpose the filter H: H'.
[1 1 1;0 0 0;-1 -1 -1].
H = FSPECIAL('sobel') returns 3-by-3 filter that emphasizes horizontal edges utilizing the smoothing effect by approximating a vertical
gradient. If you need to emphasize vertical edges, transpose the filter H: H'.
[1 2 1;0 0 0;-1 -2 -1].
H=FSPECIAL('unsharp' ALPHA) returns a 3-by-3 unsharp contrast enhancement filter FSPECIAL creates the unsharp filterfromthe
14
H = FSPECIAL( unsharp ,ALPHA) returns a 3 by 3 unsharp contrast enhancement filter. FSPECIAL creates the unsharp filter from the
negative of the Laplacian filter with parameter ALPHA. ALPHA controls the shape of the Laplacian and must be in the range 0.0 to 1.0. The
default ALPHA is 0.2.
2009 Optomechatronics
Không có nhận xét nào:
Đăng nhận xét