SlideShare a Scribd company logo
Gonzalez, Rafael,C.
digital image processing
using MATLAB
chapter 3 : 

Intensity transformations & spatial filtering
1
By: H.Mostafavi
Urmia university of technology
Introduction
• Spatial domain : all the methods
are directly operate on the pixels
of an image.
• In this season we’ll going to focus on a
important category of spatial domain
process.
• *most of the examples are about image
enhancement.
Intensity transforms
3
A. Background
• The total form of processes that they are related to the
spatial domain is : g(x,y)=T[f(x,y)]
4
B.Intensity Transformation Functions
• The simplest form of the transformation T is when the
Neighborhood is of size 1x1 (a single pixel).
• In this case :
• Because the T function depends only on the intensity value at a
point, it’s frequently are written in simplified form as :
g at (x,y) intensity of f at that point
* And T, becomes an intensity or gray-level transformation function.
s = T( r ) The intensity of f
The intensety of g
5
B.1.imadjust Function
• Function imadjust is the basic IPT toolbox function for intensity transformations of
gray-scale images. It has the general syntax :
• As the Fig illustrates,
this function maps
the intensity values
in image f to new
value in g.
• The input image can be of class :
g = imadjust(f, [low_in high_in], [low_out high_out], gamma)
Uint8
Uint16
Int16
Single
Double
6
• All inputs to function imadjust, other than f and gamma, are
specified as values between 0 and 1, independently of the class of f.
Class
of
image
Uint8:
values x 255
Uint16:
values x 65535
Using
empty
matrix [ ]
[low_in high_in]
[low_out high_out]
Result in
Default range
[0 1]
7
• Parameter gamma shape of the curve.
Gamma < 1
Gamma = 1
Gamma >1
Brighter
Darker
Omitted from function
8
clc,clear
f = imread('Fig0304(a)(breast_digital_Xray).tif');
g1 = imadjust(f,[0 1],[1 0]);
g2 = imadjust(f,[0.5 0.75],[0 1]);
g3 = imadjust(f,[],[],2);
g4 = imadjust(f, stretchlim(f), [ ]);
g5 = imadjust(f, stretchlim(f), [1 0]);
subplot(2,3,1);imshow(f)
subplot(2,3,2);imshow(g1)
subplot(2,3,3);imshow(g2)
subplot(2,3,4);imshow(g3)
subplot(2,3,5);imshow(g4)
subplot(2,3,6);imshow(g5)
Negative image
Note:
The negative of an image can be
obtained also with
toolbox function imcomplement :
g = imcomplement(f)
Note:
Sometimes, it is of interest to be able to use function imadjust
“automatically,” without having to be concerned about the low and
high parameters discussed above. Function stretchlim is
useful in that regard, its basic syntax is :
low_high = stretchlim(f)
9
B.2. Logarithmic and Contrast-Stretching Transformation
• Logarithmic and contrast-stretching transformations are basic tools for dynamic range
manipulation.
• log transformation Compress dynamic range.
• To bring the resulting compressed values back to the full range:
g = c*log(1 + double(f))
The shape is similar to the gamma curve
For example:
Fourier spectrom:[0,106]
Loge(106) = 13.8
1 of the Principal uses:
For 8 bits: gs = im2uint8(mat2gray(g)); mat2gray:
Back to [0 1]
im2uint8:
Back to [0 255]
10
• The function in fig 3.4(a) is called
a contrast-stretching
transformation function
because it expands
a narrow range of input
level into a wide range
of output levels
• The the function in 3.4(b) is called
a thresholding function
Is a simple tools used for image segmentation.
• The function in fig 3.4(a) has the form:
• r : input image intensity
• s : output image intensity
• E : control the slope of the
function
• less than m = narrow &
dark
• more than m = narrow &
bright
11
• In MATLAB :
g = 1./(1 + (m./(double(f)+eps)).^E)
eps : avoid overflow
This transformation :
Maps output into [0 1]
Fig 3.4(a) E = 20
Figure 3.5(a) is a Fourier spectrum with values in the range
0 to 106, displayed on a linearly scaled, 8-bit display system.
Figure 3.5(b) shows the result obtained using the commands
g = im2uint8(mat2gray(log(1 + double(f))));
12
B.3.Some Utility M-Functions for Intensity Transformations
• Handling a Variable Number of Inputs and/or Outputs:
• To check the number of input arguments
n = nargin
• To check the number of output arguments
n = nargout
Ex : T = testhv(4,5)
{
nargin = 2
nargout = 1
Function : msg = nargchk(low, high, number)
Number < low : not
enough input parameter
Number > high : too many input
parameter
low < Number < high :
Empty matrix
13
• function G = testhv2(x, y, z)
error(nargchk(2, 3, nargin));
Typing
>> testhv2(6);
Not enough input arguments
• function [m, n] = testhv3(varargin)
• function [varargout] = testhv4(m, n, p)
• function [m, n] = testhv3(x, varargin)
14
Cell array
[m n] = testhv3(f,[0 0.5 1.5],A,’label’);
B.4 another M-function for intensity transformations
• In this section we develop a function that computes the following transformation
functions: negative, log, gamma and contrast stretching.
• Intrans function:
g = changeclass(newclass, f) Class of f new class
• Logical
• Uin8
• Uint16
• Int16
g = intrans(f, 'stretch', mean2(im2double(f)), 0.9);
Note:
mean2 : mean of f directly.
im2double : f to [0 1]
15
B.5 an M-function for Intensity Scale
• When working with digital images, computations that result in pixel values that span a
wide negative to positive range are common.when we want to use an 8-bit or 16-
bit format for saving or viewing an image(uint8, uint16), in which case it usually is
desirable to scale the image to the full, maximum range, [0, 255] or [0, 65535] .

• The syntax of function gscale is :
g = gscale(f, method, low, high)
full8 [0,255]
full16 [0,65535]
minmax [low high]:[0 1]
For example:
If input : f of class uint8 ‘minmax’=[0,0.5]
Output : g of class uint8 : [0,128]
16
C.Histogram Processing and Function Plotting
• Intensity transformation functions based on 

information extracted from image intensity
histograms play a central role in image 

processing, in areas such as enhancement,
compression, segmentation,
and description. The focus of this section is 

on obtaining, plotting, and using histograms 

for image enhancement.
17
C.1.Generating and Plotting Image Histograms
• The histogram of a digital image with L total possible
intensity levels in the range [0,G] is defined as the
discrete function :
h(rk ) = nk
Is the kth intensity level in [0,G] Is the number of pixels in the image whose intensity level is rk
G values for :
Uint8 = 255
Uint16 = 65535
Double = 1
Note :
For Images of
class uint8, uint16
G = L-1
*To work with
normalized
histogram:
p(rk ) = h(rk ) / n
= nk / n
18
• The core function in the toolbox for dealing with image histogram is
imhist, with the basic syntax :
h = imhist(f, b)
Is the number of bins used in forming the histogram
Is the input imageIs the input image’s histogram
For example:
For b = 2.
The intensity scale :
[0 127] & [128 255]
h(1) = [0 127]
h(2) = [128 255]
The normalized histogram:
p = imhist(f, b)/numel(f)
Histogram using bar graph:
bar(horz, z, width)
Is a row vector containing the points to be plotted
Is a vector of same dimension as z
Contains the increments of horizontal
Scale.if it’s omitted the horizontal axis is
divided in units from 0 to length(z).
A number between 0 and 1, when it’s 0
The bars are vertical lines & when it’s 1
The bars touch
19
clc,clear
f = imread('pout.tif');
h = imhist(f,25);
hi = h(1:10:256);
horz = 1:10:256;
bar(horz,h1)
axis([0 255 0 15000])
set(gca,'xtick',0:50:250)
set(gca,'ytick',0:2000:15000)
xlabel('text string', 'fontsize', size)
ylabel('text string', 'fontsize', size)
axis([horzmin horzmax vertmin vertmax])
20
• text(xloc, yloc,’text string’,’fontsize’, size)
• title(‘titlestring’)
• stem(horz, v, ‘color_linestyle_marker’,’fill’)
• plot(horz,v,’color_linestyle_marker’)
• Hold on
21
C.2.Histogram Equalization
• Assume for a moment that intensity levels are continuous quantities normalized to the range [0, 1],
and let pr (r) denote the probability density function (PDF) of the intensity levels in a given image.

• The probability density function(PDF) of output level :
***The net result of this intensity-level equalization process is an image with increased dynamic range,

which will tend to have higher contrast.
22
It’s just a CDF
• When dealing with discrete quantities we work with histograms and call the preceding
technique histogram equalization :
• Histogram equalization is implemented in the toolbox by function histeq, which has
the syntax:
g = histeq(f, nlev)
Is the number of intensity levels
nlev < L : histeq approximate
flat
Default nlev = 64
23
• imshow(f); % Fig. 3.8(a).
• figure, imhist(f) % Fig. 3.8(b).
• ylim('auto')
• g = histeq(f, 256);
• figure, imshow(g) % Fig. 3.8(c).
• figure, imhist(g) % Fig. 3.8(d).
• ylim('auto')
24
• Plotting CDF:
clc,clear
f = imread('Fig0320(4)(bottom_left).tif');
hnorm = imhist(f)./numel(f); % Normalized
histogram.
cdf = cumsum(hnorm); % CDF.
x = linspace(0, 1, 256);
plot(x, cdf)
axis([0 1 0 1]);
set(gca, 'xtick', 0:.2:1)
set(gca, 'ytick', 0:.2:1)
xlabel('Input intensity values', 'fontsize', 9)
ylabel('Output intensity values', 'fontsize', 9)
25
C.3.Histogram matching
• The toolbox implements histogram matching using the following syntax in histeq:
g = histeq(f, hspec)
clc,clear
f = imread(‘Fig0323(a)
(mars_moon_phobos).tif');
g = histeq(f,256);
subplot(2,2,1);imshow(f)
subplot(2,2,3);imshow(g)
subplot(2,2,2);imhist(f)
subplot(2,2,4);imhist(g)
26
• Using of two below functions for enhancing the
histogram:
• g = histeq(f,p)
• p=twomodogauss.
• p=manualhist.
27
D.Spatial Filtering
• As have been mentioned before in earlier lectures, some
neighborhood operations work with the values of the image
pixels in the neighborhood and the corresponding values of a
subimage that has the same dimensions as the neighborhood. 

• The sub-image is called a filter, mask, kernel, template, or
window. The values in a filter sub-image are referred to as
coefficients rather than pixels.
28
• These neighborhood operations consists of:
A. Defining a center point, (x,y)

B. Performing an operation that involves only the pixels in a predefined
neighborhood about that center point.

C. Letting the result of that operation be the “response” of the process at
that point.

D. Repeating the process for every point in the image.
29
D.1.Two spatial filtering methods
• linear spatial filtering : If the computations performed on the
pixels of the neighborhoods are linear. 

• Nonlinear spatial filtering : If the computations performed on the
pixels of the neighborhoods are nonlinear.
30
• The mechanism of linear spatial filtering
31
D.2.Correlation Vs Convolution(1D)
32
D.3.Correlation Vs Convolution(2D)
33
D.4.Implementation on MATLAB
• In image processing toolbox ( IPT ) linear spatial filtering
known by imfilter, which the syntax is:
• Common mode for calling imfilter:
g = imfilter(f,w,filtering_mode,boundary_options,size_options) f = input image
w = mask
g = outputs
filtering_mode = kind of filter
boundary_options = kind of
expanding
size_options = 1-full
2-same
g = imfilter(f,w,’replicate’)
34
• Two ways for filters that, they aren’t symmetric and
has not 180 degree rotation :
A. Calling by below code:
B. Pre-processing by below code for 180 degree rotation:
g=imfilter(f,w,’conv’,’replicate’)
Rot90(w,z)
35
clc,clear
f = imread('8 copy.jpg');
w = ones(31);
gd = imfilter(f,w);
gr = imfilter(f,w,'replicate');
gs = imfilter(f,w,'symmetric');
gc = imfilter(f,w,'circular');
f8 = im2uint8(f);
g8r = imfilter(f8,w,'replicate');
subplot(2,3,1);imshow(f,[])
subplot(2,3,2);imshow(gd,[])
subplot(2,3,3);imshow(gr,[])
subplot(2,3,4);imshow(gs,[])
subplot(2,3,5);imshow(gc,[])
subplot(2,3,6);imshow(g8r,[])
36
D.5.nonlinear spatial filtering
• There is two kind of nonlinear filtering :

• Colfilt needs more space than nlfilters but the speed of
processing in colfilts is higher than the nlfilters, they
preference against the nlfilters.
1. Colfilt
2. Nlfilter
37
• The syntax is:
• In the nonlinear filtering we must first added the boundary values to the image.So we using
padarray function.
g = colfilt(f,[m n],’sliding’,@fun, parameters)
[m n] = filtering area
Sliding = filtering is pixel by pixel
@fun = reference to a function
Parameters = desired parameters of @fun
fp = padarray(f,[r c],method,direction)
38
39
D.6.Standard Spatial Filters of IPT
• Linear Spatial Filters:
w = fspecial(‘type’,parameters)
40
fspecial(‘average’,[r c])
A triangle mean filter of sixe
r x c
fspecial(‘disk’,r)
A circular mean filter with
radius of r (defult r = 5)
fspecial(‘gaussian’,[r c],sig)
ABS = r x c ,
standard deviation = sig
fspecial(‘laplacian’,alpha)
Alpha=[0 1] default=0.5
fspecial(‘log’,[r c],sig)
ABS = r x c , “gaussian Laplace”
standard deviation = sig
fspecial(‘motion’,len ,theta)
Its combining with image,
estimating moving of the Cam Len
pixel with horz angle theta
fspecial(‘prewitt’)
3x3 mask, estimate vertical
gradient
fspecial(‘sobel’)
estimate vertical sobel gradient
fspecial(‘unsharp’,alpha)
3x3 mask, alpha for controlling
of shape, alpha > 0 & alpha <= 1
Default = 0.2
41
42
43
• Nonlinear spatial filtering:
g = ordfilt2(f,order,domain)
EX:g = ordfilt2(f,1,ones(m,n)) >>Min filter(order1)
g=ordfilt2(f,median(1:m*n),ones(m,n));
g=medfilt2(f,[m n],padopt);
44
45
References
• Gonzalez, Rafael.(2009).digital image processing using
MATLAB.
Thanks For Listening
47

More Related Content

PPTX
Intensity Transformation
PPTX
Image feature extraction
PPT
Lec 07 image enhancement in frequency domain i
PDF
Image restoration
PPTX
B&W Image Coloring (1).pptx
PPTX
Chain code in dip
PDF
DIP - Image Restoration
PPTX
HSI MODEL IN COLOR IMAGE PROCESSING
Intensity Transformation
Image feature extraction
Lec 07 image enhancement in frequency domain i
Image restoration
B&W Image Coloring (1).pptx
Chain code in dip
DIP - Image Restoration
HSI MODEL IN COLOR IMAGE PROCESSING

What's hot (20)

PPTX
Image enhancement
PPT
08 frequency domain filtering DIP
PPT
Chapter 2 Image Processing: Pixel Relation
PDF
Lecture 15 DCT, Walsh and Hadamard Transform
PPT
Spatial domain and filtering
PPTX
Advance image processing
PPT
Image trnsformations
PPTX
discrete wavelet transform
PPTX
Spatial Filters (Digital Image Processing)
PDF
Machine learning of structured outputs
PPTX
Histogram Processing
PPTX
Wavelet based image fusion
PPT
Chapter 6 Image Processing: Image Enhancement
PPTX
Image Sensing and Acquisition.pptx
PPTX
Edge Detection using Hough Transform
PPTX
Chapter 3 image enhancement (spatial domain)
PPTX
Video Segmentation
PDF
Arithmetic Coding
PPTX
Simultaneous Smoothing and Sharpening of Color Images
PPTX
Texture,pattern and pattern classes
Image enhancement
08 frequency domain filtering DIP
Chapter 2 Image Processing: Pixel Relation
Lecture 15 DCT, Walsh and Hadamard Transform
Spatial domain and filtering
Advance image processing
Image trnsformations
discrete wavelet transform
Spatial Filters (Digital Image Processing)
Machine learning of structured outputs
Histogram Processing
Wavelet based image fusion
Chapter 6 Image Processing: Image Enhancement
Image Sensing and Acquisition.pptx
Edge Detection using Hough Transform
Chapter 3 image enhancement (spatial domain)
Video Segmentation
Arithmetic Coding
Simultaneous Smoothing and Sharpening of Color Images
Texture,pattern and pattern classes
Ad

Similar to Gonzalez, rafael,c.digitalimageprocessingusing matlab (20)

PPTX
Intensity Transformation and Spatial filtering
PPTX
UNIT-2-PPT1-Image Enhancement in Spatial Domain.pptx
PDF
Image processing
PDF
4.intensity transformations
PDF
DIP Lecture 7-9.pdf
PDF
Image enhancement
PPT
Intensity Transformation and Spatial Filtering -Gonzales Chapter 3.1-3.3 (4).ppt
PPTX
Digital Image Processing Unit -2 Notes complete
PPTX
Image Processing - Unit II - Image Enhancement discussed
PPT
Image Enhancement in the Spatial Domain1.ppt
PPT
Image enhancement in the spatial domain1
PPTX
Image Enhancement research document.pptx
PPTX
Intensity Transformation Functions of image with Matlab
PPT
image processing intensity transformation
PDF
Image Enhancement
PPT
Digital Image through Scanner, Digital Camera. Concept of Gray Levels.
PPTX
Image Enhancement in Spatial Domain and Frequency Domain
PDF
PPTX
Image Enhancement in Spatial Frequency Domain
PPTX
IMAGE ENHANCEMENT IN THE SPATIAL DOMAIN.pptx
Intensity Transformation and Spatial filtering
UNIT-2-PPT1-Image Enhancement in Spatial Domain.pptx
Image processing
4.intensity transformations
DIP Lecture 7-9.pdf
Image enhancement
Intensity Transformation and Spatial Filtering -Gonzales Chapter 3.1-3.3 (4).ppt
Digital Image Processing Unit -2 Notes complete
Image Processing - Unit II - Image Enhancement discussed
Image Enhancement in the Spatial Domain1.ppt
Image enhancement in the spatial domain1
Image Enhancement research document.pptx
Intensity Transformation Functions of image with Matlab
image processing intensity transformation
Image Enhancement
Digital Image through Scanner, Digital Camera. Concept of Gray Levels.
Image Enhancement in Spatial Domain and Frequency Domain
Image Enhancement in Spatial Frequency Domain
IMAGE ENHANCEMENT IN THE SPATIAL DOMAIN.pptx
Ad

Recently uploaded (20)

PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
Digital Logic Computer Design lecture notes
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
web development for engineering and engineering
PPTX
Lecture Notes Electrical Wiring System Components
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
composite construction of structures.pdf
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PDF
Well-logging-methods_new................
PPTX
Geodesy 1.pptx...............................................
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
DOCX
573137875-Attendance-Management-System-original
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Foundation to blockchain - A guide to Blockchain Tech
Digital Logic Computer Design lecture notes
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
web development for engineering and engineering
Lecture Notes Electrical Wiring System Components
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
composite construction of structures.pdf
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Well-logging-methods_new................
Geodesy 1.pptx...............................................
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
573137875-Attendance-Management-System-original
CYBER-CRIMES AND SECURITY A guide to understanding
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Mechanical Engineering MATERIALS Selection
Internet of Things (IOT) - A guide to understanding
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Model Code of Practice - Construction Work - 21102022 .pdf

Gonzalez, rafael,c.digitalimageprocessingusing matlab

  • 1. Gonzalez, Rafael,C. digital image processing using MATLAB chapter 3 : Intensity transformations & spatial filtering 1
  • 3. Introduction • Spatial domain : all the methods are directly operate on the pixels of an image. • In this season we’ll going to focus on a important category of spatial domain process. • *most of the examples are about image enhancement. Intensity transforms 3
  • 4. A. Background • The total form of processes that they are related to the spatial domain is : g(x,y)=T[f(x,y)] 4
  • 5. B.Intensity Transformation Functions • The simplest form of the transformation T is when the Neighborhood is of size 1x1 (a single pixel). • In this case : • Because the T function depends only on the intensity value at a point, it’s frequently are written in simplified form as : g at (x,y) intensity of f at that point * And T, becomes an intensity or gray-level transformation function. s = T( r ) The intensity of f The intensety of g 5
  • 6. B.1.imadjust Function • Function imadjust is the basic IPT toolbox function for intensity transformations of gray-scale images. It has the general syntax : • As the Fig illustrates, this function maps the intensity values in image f to new value in g. • The input image can be of class : g = imadjust(f, [low_in high_in], [low_out high_out], gamma) Uint8 Uint16 Int16 Single Double 6
  • 7. • All inputs to function imadjust, other than f and gamma, are specified as values between 0 and 1, independently of the class of f. Class of image Uint8: values x 255 Uint16: values x 65535 Using empty matrix [ ] [low_in high_in] [low_out high_out] Result in Default range [0 1] 7
  • 8. • Parameter gamma shape of the curve. Gamma < 1 Gamma = 1 Gamma >1 Brighter Darker Omitted from function 8
  • 9. clc,clear f = imread('Fig0304(a)(breast_digital_Xray).tif'); g1 = imadjust(f,[0 1],[1 0]); g2 = imadjust(f,[0.5 0.75],[0 1]); g3 = imadjust(f,[],[],2); g4 = imadjust(f, stretchlim(f), [ ]); g5 = imadjust(f, stretchlim(f), [1 0]); subplot(2,3,1);imshow(f) subplot(2,3,2);imshow(g1) subplot(2,3,3);imshow(g2) subplot(2,3,4);imshow(g3) subplot(2,3,5);imshow(g4) subplot(2,3,6);imshow(g5) Negative image Note: The negative of an image can be obtained also with toolbox function imcomplement : g = imcomplement(f) Note: Sometimes, it is of interest to be able to use function imadjust “automatically,” without having to be concerned about the low and high parameters discussed above. Function stretchlim is useful in that regard, its basic syntax is : low_high = stretchlim(f) 9
  • 10. B.2. Logarithmic and Contrast-Stretching Transformation • Logarithmic and contrast-stretching transformations are basic tools for dynamic range manipulation. • log transformation Compress dynamic range. • To bring the resulting compressed values back to the full range: g = c*log(1 + double(f)) The shape is similar to the gamma curve For example: Fourier spectrom:[0,106] Loge(106) = 13.8 1 of the Principal uses: For 8 bits: gs = im2uint8(mat2gray(g)); mat2gray: Back to [0 1] im2uint8: Back to [0 255] 10
  • 11. • The function in fig 3.4(a) is called a contrast-stretching transformation function because it expands a narrow range of input level into a wide range of output levels • The the function in 3.4(b) is called a thresholding function Is a simple tools used for image segmentation. • The function in fig 3.4(a) has the form: • r : input image intensity • s : output image intensity • E : control the slope of the function • less than m = narrow & dark • more than m = narrow & bright 11
  • 12. • In MATLAB : g = 1./(1 + (m./(double(f)+eps)).^E) eps : avoid overflow This transformation : Maps output into [0 1] Fig 3.4(a) E = 20 Figure 3.5(a) is a Fourier spectrum with values in the range 0 to 106, displayed on a linearly scaled, 8-bit display system. Figure 3.5(b) shows the result obtained using the commands g = im2uint8(mat2gray(log(1 + double(f)))); 12
  • 13. B.3.Some Utility M-Functions for Intensity Transformations • Handling a Variable Number of Inputs and/or Outputs: • To check the number of input arguments n = nargin • To check the number of output arguments n = nargout Ex : T = testhv(4,5) { nargin = 2 nargout = 1 Function : msg = nargchk(low, high, number) Number < low : not enough input parameter Number > high : too many input parameter low < Number < high : Empty matrix 13
  • 14. • function G = testhv2(x, y, z) error(nargchk(2, 3, nargin)); Typing >> testhv2(6); Not enough input arguments • function [m, n] = testhv3(varargin) • function [varargout] = testhv4(m, n, p) • function [m, n] = testhv3(x, varargin) 14 Cell array [m n] = testhv3(f,[0 0.5 1.5],A,’label’);
  • 15. B.4 another M-function for intensity transformations • In this section we develop a function that computes the following transformation functions: negative, log, gamma and contrast stretching. • Intrans function: g = changeclass(newclass, f) Class of f new class • Logical • Uin8 • Uint16 • Int16 g = intrans(f, 'stretch', mean2(im2double(f)), 0.9); Note: mean2 : mean of f directly. im2double : f to [0 1] 15
  • 16. B.5 an M-function for Intensity Scale • When working with digital images, computations that result in pixel values that span a wide negative to positive range are common.when we want to use an 8-bit or 16- bit format for saving or viewing an image(uint8, uint16), in which case it usually is desirable to scale the image to the full, maximum range, [0, 255] or [0, 65535] . • The syntax of function gscale is : g = gscale(f, method, low, high) full8 [0,255] full16 [0,65535] minmax [low high]:[0 1] For example: If input : f of class uint8 ‘minmax’=[0,0.5] Output : g of class uint8 : [0,128] 16
  • 17. C.Histogram Processing and Function Plotting • Intensity transformation functions based on information extracted from image intensity histograms play a central role in image processing, in areas such as enhancement, compression, segmentation, and description. The focus of this section is on obtaining, plotting, and using histograms for image enhancement. 17
  • 18. C.1.Generating and Plotting Image Histograms • The histogram of a digital image with L total possible intensity levels in the range [0,G] is defined as the discrete function : h(rk ) = nk Is the kth intensity level in [0,G] Is the number of pixels in the image whose intensity level is rk G values for : Uint8 = 255 Uint16 = 65535 Double = 1 Note : For Images of class uint8, uint16 G = L-1 *To work with normalized histogram: p(rk ) = h(rk ) / n = nk / n 18
  • 19. • The core function in the toolbox for dealing with image histogram is imhist, with the basic syntax : h = imhist(f, b) Is the number of bins used in forming the histogram Is the input imageIs the input image’s histogram For example: For b = 2. The intensity scale : [0 127] & [128 255] h(1) = [0 127] h(2) = [128 255] The normalized histogram: p = imhist(f, b)/numel(f) Histogram using bar graph: bar(horz, z, width) Is a row vector containing the points to be plotted Is a vector of same dimension as z Contains the increments of horizontal Scale.if it’s omitted the horizontal axis is divided in units from 0 to length(z). A number between 0 and 1, when it’s 0 The bars are vertical lines & when it’s 1 The bars touch 19
  • 20. clc,clear f = imread('pout.tif'); h = imhist(f,25); hi = h(1:10:256); horz = 1:10:256; bar(horz,h1) axis([0 255 0 15000]) set(gca,'xtick',0:50:250) set(gca,'ytick',0:2000:15000) xlabel('text string', 'fontsize', size) ylabel('text string', 'fontsize', size) axis([horzmin horzmax vertmin vertmax]) 20
  • 21. • text(xloc, yloc,’text string’,’fontsize’, size) • title(‘titlestring’) • stem(horz, v, ‘color_linestyle_marker’,’fill’) • plot(horz,v,’color_linestyle_marker’) • Hold on 21
  • 22. C.2.Histogram Equalization • Assume for a moment that intensity levels are continuous quantities normalized to the range [0, 1], and let pr (r) denote the probability density function (PDF) of the intensity levels in a given image. • The probability density function(PDF) of output level : ***The net result of this intensity-level equalization process is an image with increased dynamic range, which will tend to have higher contrast. 22 It’s just a CDF
  • 23. • When dealing with discrete quantities we work with histograms and call the preceding technique histogram equalization : • Histogram equalization is implemented in the toolbox by function histeq, which has the syntax: g = histeq(f, nlev) Is the number of intensity levels nlev < L : histeq approximate flat Default nlev = 64 23
  • 24. • imshow(f); % Fig. 3.8(a). • figure, imhist(f) % Fig. 3.8(b). • ylim('auto') • g = histeq(f, 256); • figure, imshow(g) % Fig. 3.8(c). • figure, imhist(g) % Fig. 3.8(d). • ylim('auto') 24
  • 25. • Plotting CDF: clc,clear f = imread('Fig0320(4)(bottom_left).tif'); hnorm = imhist(f)./numel(f); % Normalized histogram. cdf = cumsum(hnorm); % CDF. x = linspace(0, 1, 256); plot(x, cdf) axis([0 1 0 1]); set(gca, 'xtick', 0:.2:1) set(gca, 'ytick', 0:.2:1) xlabel('Input intensity values', 'fontsize', 9) ylabel('Output intensity values', 'fontsize', 9) 25
  • 26. C.3.Histogram matching • The toolbox implements histogram matching using the following syntax in histeq: g = histeq(f, hspec) clc,clear f = imread(‘Fig0323(a) (mars_moon_phobos).tif'); g = histeq(f,256); subplot(2,2,1);imshow(f) subplot(2,2,3);imshow(g) subplot(2,2,2);imhist(f) subplot(2,2,4);imhist(g) 26
  • 27. • Using of two below functions for enhancing the histogram: • g = histeq(f,p) • p=twomodogauss. • p=manualhist. 27
  • 28. D.Spatial Filtering • As have been mentioned before in earlier lectures, some neighborhood operations work with the values of the image pixels in the neighborhood and the corresponding values of a subimage that has the same dimensions as the neighborhood. • The sub-image is called a filter, mask, kernel, template, or window. The values in a filter sub-image are referred to as coefficients rather than pixels. 28
  • 29. • These neighborhood operations consists of: A. Defining a center point, (x,y) B. Performing an operation that involves only the pixels in a predefined neighborhood about that center point. C. Letting the result of that operation be the “response” of the process at that point. D. Repeating the process for every point in the image. 29
  • 30. D.1.Two spatial filtering methods • linear spatial filtering : If the computations performed on the pixels of the neighborhoods are linear. • Nonlinear spatial filtering : If the computations performed on the pixels of the neighborhoods are nonlinear. 30
  • 31. • The mechanism of linear spatial filtering 31
  • 34. D.4.Implementation on MATLAB • In image processing toolbox ( IPT ) linear spatial filtering known by imfilter, which the syntax is: • Common mode for calling imfilter: g = imfilter(f,w,filtering_mode,boundary_options,size_options) f = input image w = mask g = outputs filtering_mode = kind of filter boundary_options = kind of expanding size_options = 1-full 2-same g = imfilter(f,w,’replicate’) 34
  • 35. • Two ways for filters that, they aren’t symmetric and has not 180 degree rotation : A. Calling by below code: B. Pre-processing by below code for 180 degree rotation: g=imfilter(f,w,’conv’,’replicate’) Rot90(w,z) 35
  • 36. clc,clear f = imread('8 copy.jpg'); w = ones(31); gd = imfilter(f,w); gr = imfilter(f,w,'replicate'); gs = imfilter(f,w,'symmetric'); gc = imfilter(f,w,'circular'); f8 = im2uint8(f); g8r = imfilter(f8,w,'replicate'); subplot(2,3,1);imshow(f,[]) subplot(2,3,2);imshow(gd,[]) subplot(2,3,3);imshow(gr,[]) subplot(2,3,4);imshow(gs,[]) subplot(2,3,5);imshow(gc,[]) subplot(2,3,6);imshow(g8r,[]) 36
  • 37. D.5.nonlinear spatial filtering • There is two kind of nonlinear filtering : • Colfilt needs more space than nlfilters but the speed of processing in colfilts is higher than the nlfilters, they preference against the nlfilters. 1. Colfilt 2. Nlfilter 37
  • 38. • The syntax is: • In the nonlinear filtering we must first added the boundary values to the image.So we using padarray function. g = colfilt(f,[m n],’sliding’,@fun, parameters) [m n] = filtering area Sliding = filtering is pixel by pixel @fun = reference to a function Parameters = desired parameters of @fun fp = padarray(f,[r c],method,direction) 38
  • 39. 39
  • 40. D.6.Standard Spatial Filters of IPT • Linear Spatial Filters: w = fspecial(‘type’,parameters) 40
  • 41. fspecial(‘average’,[r c]) A triangle mean filter of sixe r x c fspecial(‘disk’,r) A circular mean filter with radius of r (defult r = 5) fspecial(‘gaussian’,[r c],sig) ABS = r x c , standard deviation = sig fspecial(‘laplacian’,alpha) Alpha=[0 1] default=0.5 fspecial(‘log’,[r c],sig) ABS = r x c , “gaussian Laplace” standard deviation = sig fspecial(‘motion’,len ,theta) Its combining with image, estimating moving of the Cam Len pixel with horz angle theta fspecial(‘prewitt’) 3x3 mask, estimate vertical gradient fspecial(‘sobel’) estimate vertical sobel gradient fspecial(‘unsharp’,alpha) 3x3 mask, alpha for controlling of shape, alpha > 0 & alpha <= 1 Default = 0.2 41
  • 42. 42
  • 43. 43
  • 44. • Nonlinear spatial filtering: g = ordfilt2(f,order,domain) EX:g = ordfilt2(f,1,ones(m,n)) >>Min filter(order1) g=ordfilt2(f,median(1:m*n),ones(m,n)); g=medfilt2(f,[m n],padopt); 44
  • 45. 45
  • 46. References • Gonzalez, Rafael.(2009).digital image processing using MATLAB.