dip2 image enhancement1
TRANSCRIPT
-
7/23/2019 DIP2 Image Enhancement1
1/18
Image Enhancement Spatial domain techniques
Point operations Linear transformation Logarthmatic transformation Power-law transformation
Piece wise linear transformation Contrast Stretching
1
Arithmetic operation Filtering
Filter mask, convolution Smoothing Spatial Filters
Averaging filters or low pass filters Weight average
Sharpening Spatial Filters
Point Operations Overview
Point operations are zero-memory operations where
a given gray level x[0,L] is mapped to anothergray level y[0,L] according to a transformation
)(xfy =y
2
L
L
x
L=255: for grayscale images
Pointoperationsxy =
L
y
3
Lx
No influence on visual quality at all
Digital Negative
xLy =
Lx
0
L
4
Load a imagehh=255-a(1:1536,1:2048,1); digitanegative.figureimshow(hh)hh=255-a(1:1536,1:2048,1:3);g=a(:,:,2);imshow(g)
Matlabfunction for same purpose:
5
Imadjust (f,[low_in,high_in],[low_out,hight_out],gamma
Maps the intensity values in in imagef to new values in g.Values between low_in and high_in map to value between low_out andhigh_out.
A=imread(test.jpg)g1=imadjust(a,[0 1],[1 0]);imshow(g1)
Contrast Stretching
Contraststretch(alsoknown as normalization)operates bystretchingthe pixel range in the input image over a larger dynamic range in theoutputimage.By applyingonly this linearscalingof the dynamic range itis less sophisticated than histogramequalizationandprovides a visuallyless harsh enhancement.
U erandlowerlimit(a,b) shouldbe known 0 ,255
6
In its simplest form this transform then scans the present image to determinethe maximum and minimum pixel values currently present, denoted c and drespectfully.
In reality this method of choosing c and d is very naive as a single outlier inthe image will effect the overall enhancement result
-
7/23/2019 DIP2 Image Enhancement1
2/18
Contrast Stretching
-
7/23/2019 DIP2 Image Enhancement1
3/18
Gamma tranform can make pixel brighter or darker depends on its value.% powelawof intesitytransformation. when f(x,y) is in the range of of% [0,1] and gamma is larger than one it makes the image darker.% when gamma is smaller than one it makes the image brighter.
MatLab code
clear allclose allf=imread('winter.jpg');
13
f=im2double(f);figure ,imshow(f),title ('org img')[m,n]=size(f)c=1;y=input('Gmmavalue');for i=1:mfor j=1:n
s(i,j)=c*(f(i,j) y);endendfigure, imshow(s),title ('gamma=0.25');
%muliply image , image gets darker bymultiplying it short numberclose allf=imread('test.jpg');
f=im2double(f);figure,imshow(f), title('gray scale');m=0.75; %contrastE=0.55 % slope of function
Contrast Stretching Matlab code with exponential function
EE465: Introduction to Digital ImageProcessing
14
g=1./(1+(m./(f+eps)). E);figure,imshow(g), title('emhance image');
SummaryofPointOperation
Sofar,wehavediscussedvariousformsofmappingfunctionf(x)thatleadstodifferentenhancementresults
MATLABfunction>imadjust
Thenaturalquestionis:Howtoselectanappropriatef(x)foranarbitraryimage?
Onesystematicsolutionisbasedonthehistograminformationofanimage
Histogramequalizationandspecification
15
HistogramProcessing
Histogramofadigitalimagewithgraylevelsintherange[0,L1]isadiscretefunction
h(rh(rkk) = n) = nkk W ere
rk :thekth graylevel
nk :thenumberofpixelsintheimagehavinggraylevelrk h(rk) :histogramofadigitalimagewithgraylevelsrk
Whatishistogram
Histogramisagraphshowingthenumberofpixelsinanimageateachdifferentintensity
valuefoundinthatimage.
different possible intensities, and so the
histogram will graphically display 256 numbers
showing the distribution of pixels amongst
those grayscale values.
17
Example
2 3 3 2
4 2 4 3 4
5
6
No. of pixels
3 2 3 5
2 4 2 4
4x4 image
Gray scale = [0,9]histogram
0 1
1
2
2
3
3
4 5 6 7 8 9
Gray level
-
7/23/2019 DIP2 Image Enhancement1
4/18
NormalizedHistogram
dividingeachofhistogramatgraylevelrrkk bythetotal
number
of
pixels
in
the
image,
nnp(rp(rkk) = n) = nkk/ n/ n
For =0,1,,L1
p(rp(rkk)) givesanestimateoftheprobabilityofoccurrenceofgraylevelrrkk
Thesumofallcomponentsofanormalizedhistogramisequalto1
Histogram based Enhancement
Histogram of an image represents the relative frequency
of occurrence of various gray levels in the image
2000
2500
3000
20
0 50 100 150 200
0
500
1000
1500
MATLAB function >imhist(x)
Examplerk
h(rk) or p(rk)
Dark image
Components ofhistogram are
Bright image
concentrated on thelow side of the grayscale.
Components ofhistogram areconcentrated on the
high side of the grayscale.
Example
Low-contrast image
histogram is narrowand centered toward
High-contrast image
e m e o egray scale
histogram covers broadrange of the gray scaleand the distribution ofpixels is not too far fromuniform, with very fewvertical lines being muchhigher than the others
WhyHistogram?
1
1.5
2
2.5
3
3.5
4
x 104
23
Histogram information reveals that image is under-exposed
0 50 100 150 200 250
0
0.5
It is a baby in the cradle!
AnotherExample
4000
5000
6000
7000
24
0 50 100 150 200 250
0
1000
2000
Over-exposed image
-
7/23/2019 DIP2 Image Enhancement1
5/18
HistogramEqualization OfaImage
Oneoftheseveraltechniquestoenhanceanimageinsuchamannerishistogramequalization,whichiscommonlyusedto
compareimages
made
in
entirely
different
circumstances.
Theconceptofhistogramequalizationistospreadotherwisecluttered fre uencies more evenl over the len th of the
histogram.Frequenciesthatlieclosetogetherwill
dramaticallybestretchedout
25
HistogramEqualization OfaImage
Histogramequalizationassignstheintensity
values
of
pixels
in
the
input
image
such
that
theoutputimagecontainsauniformdistributionofintensities.
Histogramequalizationredistributesintensitydistributions.Ifthehistogramofanyimagehasmanypeaksandvalleys,itwillstillhavepeaksandvalleyafterequalization,butpeaksandvalleywillbeshifted.
26
HistogramEqualization
Asthelowcontrastimageshistogramisnarrowandcenteredtowardthemiddleofthegrayscale,ifwe
distributethehistogramtoawiderrangethequality
oftheimagewillbeimproved.
Wecandoitbyadjustingtheprobabilitydensityfunctionoftheoriginalhistogramoftheimageso
thattheprobabilityspreadequally
Example
before after Histogramequalization
Example
before after Histogramequalization
The quality isnot improvedmuch becausethe originalimage alreadyhas a broadengray-level scale
GeneralworkingThe histogram equalization is operated on an image in three step:1). Histogram Formation2). New Intensity Values calculation for each Intensity Levels3). Replace the previous Intensity values with the new intensity values
New intensity values are calculated for each intensity level by applying thefollowing equation:
30
if the image is in the grayscale domain, then the count is 255. And if theimage is of size 256x256 then, the No. of pixels is 65536.
The next step is to replace the previous intensity level with the new intensitylevel. This is accomplished by putting the value ofOi in the image for allthe pixels, where Oi represents the new intensity value, whereas i
represents the previous intensity level.
-
7/23/2019 DIP2 Image Enhancement1
6/18
Example
2 3 3 2
4 2 4 3 4
5
6
No. of pixels
3 2 3 5
2 4 2 4
4x4 image
Gray scale = [0,9]histogram
0 1
1
2
2
3
3
4 5 6 7 8 9
Gray level
Gray
Level(j)0 1 2 3 4 5 6 7 8 9
No. ofpixels 0 0 6 5 4 1 0 0 0 0
k
0 0
6
/
16
11
/
16
15
/
16
16
/
16
16
/
16
16
/
16
16
/
16
16
/
16
s x9 0 03.3
3
6.1
6
8.4
89 9 9 9 9
=jj
0
=
=k
j
j
n
ns
0
Example
3 6 6 3
8 3 8 6 4
5
6
No. of pixels
6 3 6 9
3 8 3 8
Output image
Gray scale = [0,9]Histogram equalization
0 1
1
2
2
3
3
4 5 6 7 8 9
Gray level
Histogramequalizationclear allclose alla=imread('humd.jpg');h=imhist(a(:,:,1));h1=h(1:10:256);horz=1:10:256;figure,title('histogram')bar(horz,h1);
34
g= s eq a :,:, , ;gg=g(1:10:256);bar(horz,gg);
figure ,title('histogrameq')bar(horz,gg);figure,title('orgpic'),imshow(a(:,:,1))figure,title('histogrameqpic'),imshow(g)
Histogramequalizationimplemenationclear all, close allx0=imread('histest1.jpg');imshow(x0);x0=rgb2gray(x0);figure,imshow(x0),title('gray')[m,n]=size(x0);len=m*n;x=reshape(x0,len,1);
L=256
35
xpdf=hist(x,[0:L-1]); % pdf, 1 x Ltr=round(xpdf*triu(ones(L))*(L-1)/len); % cdf, range from 0 to L-1y0=zeros(m,n);for i=1:L,
if xpdf(i)>0,y0=y0+[x0==i-1]*tr(i);
endendypdf=hist(reshape(y0,len,1),[0:L-1]); % pdfof y, 1 x L
Histogramequalizationimplemenation
figure,subplot(211),stem([0:L-1],xpdf),title('histogram, original')axis([0 256 0 500])
subplot(212),stem([0:L-1],ypdf),title('histogram, equalized'),axis([0 256 0 500])
figure,subplot(121),imshow(uint8(x0)),title('before')
36
subplot(122),imshow(uint8(y0)),title('after')
figurestairs([0:L-1],tr),title('transformation'),axis([0 256 0 256])
AssignmentApply the image histogram on color imageHint apply on single frame then find color value see lecture 1.
-
7/23/2019 DIP2 Image Enhancement1
7/18
Image Example
37
before after
Arithmetic & logic operations on images used extensively inmost image processing applications
May cover the entire image or a subsetArithmetic operation between pixels p and q are definedas:
Arithmetic operation on image
38
on: p+q :a ng a vaue o eac mage pxe vaueContrast Adjustment:Adding a +ve constant value to each pixel locationincreases its value and hence its brightness.Blending:Adding images together produces a composite image ofboth input images.
39
Image subtraction
Subtraction: subtracting a value from each pixel location .....
Contrast Adjustment: as per addition
Image differencing :
subtracting one image from another shows us the difference between images.If we subtract two images in a video sequence, taken from a static camera, weget a difference image showing the movement that has occurred betweenthese video frames in the sceneA useful variation on subtraction is the absolute difference Ioutput=| IA - IB |
40
e ween mages.
Division: dividing each pixel value ....
Contrast Adjustment: to uniformly scale image contrast by a given
factor (e.g. reduce contrast by 25% =division by 4 (=100/25). Oftenreferred to as image colour scaling. Image differencing: dividing image by another gives a result of 1
where the image pixel values are identical and a value !=1 wheredifferences occur. Notably image differencing using subtraction iscomputationally more efficient.
Multiplication
Contrast Adjustment: image colour scaling as per division
Saturation
This is commonly known as saturation in the image space the valueexceedsthe representational capacity of the image.A solution is todetectthis overflowand avoid itby setting all such values tothe max value forthe image representation (e.g. 255). We mustalso be aware of -ve pixelvalues resulting fromsubtraction and dealwiththese accordingly
41
.
Image blending
This can be used to produce ghosting or overlay effects between differentimages.
equal weights for each image (1/N) produces and equally weightedcomposite of each input image 1...N. Alternatively different weights can beused between images to enhance/suppress the features of differentimages in the final result.
Alpha Bl end ing
Transparency in a given image can be introduced by giving a weight between 0 and1 to individual in the image. The combined alpha for each pixel location in theimage form an additional image channel, the alpha-channel, indexed as alpha(i,j).
An alpha value of 0 is transparent (no pixel colourvisible, only background)and a value of 1 is opaque (full pixel colourvisible, no background visible).
A value in between gives a level of blending with the background so that the
dis la ed ixel colour, Idis la (i, ), is:
42
-
7/23/2019 DIP2 Image Enhancement1
8/18
Adding an image
when we add 128, all grey values of 127 or greater will be mapped to 255. Andwhen we subtract 128, all grey values of 128 or less will be mapped to 0. Bylooking at these graphs, we see that in general adding a constant will lighten animage, and subtracting a constant will darken it.
b=imread('blocks.tif');b1=b+128 ??? E rrorb1=uint8(double(b)+128); orb1=imadd(b 128)
43
,b2=imsubtract(b,128);% Adding two images
a1=imread('sence1.jpg');a2=imread('sence2.jpg');ad=a2(:,1:138,1:3); % should have samesizeb=imadd(a1,ad);imshow(a1)imshow(a2)imshow(b)
We can also perform lightening or darkening of an image by multiplication;
b3=immultiply(b,0.5); or b3=imdivide(b,2)b4=immultiply(b,2);
b5=imadd(immultiply(b,0.5),128); orb5=imadd(imdivide(b,2),128);
44
Filtering
Filteringisatechniqueformodifyingorenhancinganimagetoemphasizecertainfeaturesorremoveotherfeatures.
Filteringisaneighborhoodoperation,inwhichthevalueofanygivenpixelintheoutputimageisdeterminedbyapplyingsomealgorithmtothevaluesofthepixelsintheneighborhoodofthecorresponding inputpixel.
includesmoothing,sharpening,andedgeenhancement.
FilterterminDigitalimageprocessing isreferredtothesubimage ,mask,kernel,template,orwindow.
45
Spatialfilteringisapixelneighborhoodoperation
Linearfilteringisaccomplishedbyconvolutionandcorrelation
meansthevalueofanygivenpixelinoutputimageisrepresentedbyweightedsumofthepixelvalueofitneighborhood
Commonelementofafilterare neighborhood
anoperationonneighborhoodincludingpixelitself.
AFilterMaskofdifferentsize3X3,5X5etc.
46
FilterMask
Thesumofweightsinamaskaffecttheoverallintensityoftheresultingimage.
Typically,amaskisnormalized suchthatthesumofweightsisequaltoone.
47
,thatthesumofweightsisequaltozero.
Processoffiltering Theprocessconsistssimplyofmovingthefiltermaskfrompoint
topointinanimage.
Spatialfilteringrequiresthreesteps: 1.positionthemaskoverthecurrentpixel, 2.formallproductsoffilterelementswiththe
correspondingelementsoftheneighbourhood, 3.addupalltheproducts.
Thismustberepeatedforeverypixelintheimage.
48
-
7/23/2019 DIP2 Image Enhancement1
9/18
Frequenciesinimage
Fundamentally,thefrequenciesofanimagearetheamountbywhichgreyvalueschangewithdistance
Highfrequencycomponents arecharacterizedbylargechangesingreyvaluesoversmall
distances;
i.e.edges andnoise.
Lowfrequency components,ontheotherhand,arepartsoftheimagecharacterized
bylittlechangeinthegreyvalues.Thesemayincludebackgrounds,skintextures.
49
SpatialFiltering
Twotypeoffiltering Linearfiltering
Medianfiltering
averagefiltering Gaussianfiltering
Orderstatistic
erscan ec ass e as:
Lowpass(preservelowfrequencies,reducesoreliminateshigh frequencycomponents) Highpass (i.e.,preservehighfrequencies,reducesoreliminateslow frequencycomponents)
Bandpass (i.e.,preservefrequencieswithinaband) Bandreject (i.e.,preservefrequenciesoutsideaband)
CorrelationinlinearSpatialFiltering
f(x-1,y-1)f(x-1,y)f(x-1,y+1)
f(x,y-1) f(x,y)f(x,y+1)
w(-1,-1) w(-1,0) w(-1,1)
w(0,-1) w(0,0) w(0,1)
The result is the sum of productsof the mask coefficients with thecorresponding pixels directly underthe mask
Pixels of image
Mask coefficientsw(-1,-1) w(-1,0) w(-1,1)
w(0,-1) w(0,0) w(0,1)
f(x+1,y-1)f(x+1,y)f(x+1,y+1)w(1,-1) w(1,0) w(1,1)
w(1,-1) w(1,0) w(1,1)
)1,1()1,1(),1()0,1()1,1()1,1(
)1,()1,0(),()0,0()1,()1,0(
)1,1()1,1(),1()0,1()1,1()1,1(
++++++
++++
++++
yxfwyxfwyxfw
yxfwyxfwyxfw
yxfwyxfwyxfw=),( yxf
Linearfiltering
Thecoefficientw(0,0)coincideswithimagevaluef(x,y),indicatingthatthemaskis
centeredat(x,y)whenthecomputationof
Ingeneral,linearfilteringofanimagefofsizeMxN withafiltermaskofsizemxn isgivenby
theexpression:
= =++=
a
as
b
bt tysxftswyxg ),(),(),(
Eq-A
53
IssuesinspatialFilteringWhat happens when center of filter approaches image border
For nxn mask , at least one edge of the mask will coincide with borderof the image at distance (n-1)/2One or more column of the mask will be located outside image plane.
Remedy:Limit the excursions of centre of mask to be at distance no less than
(n-1)/2 pixels from border.
54
Problem: resulting image will be shorter than orginal but all the pixels inthe filter image would be processed by mask.
PaddingAdding rows and column of zeros or other constant gray level.Padding by replicating rows and columnPadding is then stripped off at the end of filtering. filtered and originalimage would have same size.Value of the padding will effect at the edges which increase with themask size.
-
7/23/2019 DIP2 Image Enhancement1
10/18
SmoothingSpatialFilters
Smoothingfiltersareusedforblurringandfornoisereduction.
Blurringisusedinpreprocessingsteps,suchas
removalof
small
details
from
an
image
prior
to
objectextraction,andbridgingofsmallgapsinlinesorcurves
Noisereductioncanbeaccomplished byblurringduetoreducedsharptransitioningraylevels
Imageblurringcanbeobtainedinspatialdomainbypixelaveragingorintegrationinaneighborhood.
Theimagecanbefurtherblurredbyusinganaveragingfilteroflargersize
LinearFilters Averaging,GaussianandHighpassfilter. Averagingfilter: IsaLowPassFiltermeansitpassesonlylow
frequencyrangespixelsandblockthehigher
frequencypixels.
That
swhy
edges
which
are
attributesofsharpnessinanimageduetotheirhighchangeinfrequenciesareblurredafter
. Theideaisreplacingthevalueofeverypixelinan
imagebytheaverageofthegraylevelsintheneighborhoodi.e.outputpixelvalueisthemeanofitsneighborhood.
Sizeofthemaskcontrolsthedegreeofsmoothingandlossofthedetails.
Two3x3SmoothingAverageFiltersMasks
1 1 1
1 1 1
1 1 1
9
1
1 2 1
2 4 2
1 2 1
16
1
Standard average Weighted average
=
9
19
1
i
zi
Pixels aremultiplied bydifferentcoefficients givingmore weights toSomepixels.Forexamplepixel atcentre is givenmore weightTheother pixels are inverselyrelatedtotheirdistancefromcentre
A averaging filter in which all cofficientare equal to one is called as box filter
SmoothingLinearFilters
ThegeneralimplementationforfilteringanMxNimagewithaweightedaveragingfilterof
sizemxnisgivenbytheexpression
= =
= =
++
=a
as
b
bt
a
as
b
bt
tsw
tysxftsw
yxg
),(
),(),(
),(
ExampleofAverageFilter Theeffectofcomputingtheaverageofaneighborhoodofpixelsis
toeliminateanysuddenjumpsinthegreylevelwhichcouldbecausedbysomenoiseprocesses i.e.largedeviationsfromthenorm
Supposewehavethe3 3neighborhood:2 2 3
3 30 2
1
3
2 Compared to the numbers 1,2 and 3, the number 30 is relatively
large and can be taken to be a digital representation of a noisespike. The average value of this group of numbers is 5.3. By assigningthis value to the central pixel we obtain the neighborhood
2 2 3
3 5.3 2
1 3 2
59
Averagefilter:Imfilter examplesa=imread('p.jpg');
h=ones(5,5)/25;
f1=imfilter(a,h);%useofzeropadding
h10=ones(10,10)/100;
f2=imfilter(a,h10);%useofzeropadding
f3=imfilter(a,h10,'replicate');%useofzeropadding
figure
subplot(2,2,1),imshow(a),title('orginal')
, , , ,
title('5X5withzeropadding')
subplot(2,2,3),imshow(f2),
title('10X10withzeropadding')
subplot(2,2,4),imshow(f3),
title('10X10withoutzeropadding,replicate'
Effectofimfilter withandwithoutzeropadding,replicatemeanswithout.
Withzeropaddingablacklineisobservedatboundariesespeciallyathigherfilterbutnotwhenusedwithreplicate.
60
-
7/23/2019 DIP2 Image Enhancement1
11/18
Gaussianfiltering
61
Gaussianfilter
a=imread('p.jpg');
h=fspecial('gaussian',5,1)
f1=imfilter(a,h,'conv');%useofzeropadding
h10=fspecial('gaussian',10,1)
f2=imfilter(a,h10,'conv');
%
use
of
zero
paddingf3=imfilter(a,h10,'replicate','conv');%useofzeropadding
figure
subplot(2,2,1),imshow(a),title('orginal')
subplot(2,2,2),imshow(f1),
title('5X5withzeropadding')
subplot(2,2,3),imshow(f2),
title('10X10withzeropadding')
subplot(2,2,4),imshow(f3)
,title('10X10withoutzeropadding,rep)
Doesnotmuchdifferencethanaveragefilter.Testdifferent valueofsigma
62
HighPassfilter
Sumofthecoefficients(thatis,thesumofalleelementsinthematrix),inthe
highpassfilteriszero. Thismeansthatinalowfrequencypartofanimage,wherethegrey
valuesaresimilar,theresultofusingthisfilteristhatcorrespondinggrey
valuesinthenewimagewillbeclosetozero.
Theresultingvaluesareclosetozero,
whichistheexpectedresultofapplying
ahighpassfiltertoalowfrequencycomponent
63
a=imread('coins.bmp');
h=fspecial('laplacian',0.9)
f1=imfilter(a,h);%useofzeropadding
h10=fspecial('laplacian',0.9)
f2=imfilter(a,h10);%useofzeropadding
f3=imfilter(a,h10,'replicate');%useofzeropadding
figure
subplot(2,2,1),imshow(a),title('orginal')
subplot(2,2,2),imshow(f1),title('5X5withzeropadding')
subplot(2,2,3),imshow(f2),title('10X10withzeropadding')
subplot(2,2,4),imshow(f3),title('10X10withoutzeropadding,replicate')
64
Medianfilter Themedianfilterisalsoaslidingwindowspatialfilter,butit
replacesthecentervalueinthewindowwiththemedianof
allthepixelvaluesinthewindow.Asforthemeanfilter,the
kernelisusuallysquarebutcanbeanyshape.Anexampleof
medianfilteringofasingle3x3windowofvaluesisshown
below.
65
ProcessofMedianfilter
Corpregionofneighborhood
10 15 20
thepixelinour
region
IntheMxN maskthemedianisMxN
div2+1
20 100 20
20 20 25
10, 15, 20, 20, 20, 20, 20, 25, 100
5th
-
7/23/2019 DIP2 Image Enhancement1
12/18
SharpeningSpatialFilters
Thehighlightingoffinedetailsinanimage ortoenhancedetailsthathasbeenblurrediscalledas
sharpeningofspatialfilters.
Applicationinvolveselectronicprinting,medicalimaging,industrialinspectionandautonomous
gui anceinmi itarysystem
Blurring isaccomplishedinthespatialdomainbypixelaveragingorintegrationinaneighborhood.
Sharpeningcouldbeaccomplishedbyspatialdifferentiation
67
ImageProcessingoperations
VerticalImageFlipping
Thetranspose
image
B
(M
XN)
of
A
(N
XM)
can
beobtainedas
B(j;i)=A(i;j)
fori =1:512
forj=1:512
B(j;i)=A(i;j);OR>>B=A0;
end
end
68
Thresholdingis a vital part of image segmentation, produces a binaryimage from a graycale or colour image by setting pixel values to 1 or 0depending on whether they are above or below the threshold value. It isused to separate out a region or object within the image based upon itspixel value.
A pixel becomes white if its grey level is >TA pixel becomes black if its grey level is T will perform thethresholding.
r=imread('rice.tif');imshow(r),figure,imshow(r>110)The command X>T will thus return 1 (for true) for all those pixels for which thegrey values are greater than T, and 0 (for false) for all those pixels for whichthe grey values are less than or equal to T.We thus end up with a matrix of 0's and 1's, which can be viewed as a binary
image.Matlab has the im2bw function, which thresholds an image of any data type,using the general syntax im2bw(image, level)
Noise
Noiseisanydegradationintheimagesignal,causedbyexternaldisturbance.
Cleaninganimagecorruptedby noisethusan.
TypesofNoises
SaltandPeeperNoise
GaussianNoise
PeriodicNoise
70
SaltandpeeperNoise
SaltandpeeperNoise presenceofwhiteorblackorbothpixelontheimage also
calledbinarynoise.
Canbecausedbysharp,suddendisturbancesintheimagesignal.
Toadd
noise
we
use
the
Matlab function
imnoise,whichtakesanumberof different parameters.To addsaltandpeppernoise:
t_sp=imnoise(t,'salt &pepper');
weincludeanoptionalparameter,beingavaluebetween0and1indicatingthefractionofpixelstobecorrupted.
Exercise:create40%saltandpeepernoiseinagivenimage
71
Cleaningsaltandpeppernoise
Giventhatpixelscorruptedbysaltandpeppernoisearehighfrequencycomponentsofanimage,weshouldexpectalowpassfiltershouldreduce them.
a3=fspecial('average');
t s a3=filter2 a3 t s_ _ _ Exercise:Observetheeffectwithlarger
averagefilter:
a7=fspecial('average',[7,7]);
t_sp_a7=filter2(a7,t_sp);
72
-
7/23/2019 DIP2 Image Enhancement1
13/18
Cleaningsaltandpeppernoise
Medianfilter:willingeneralreplaceanoisyvaluewithoneclosertoitssurroundings.Amedianfilterismoreeffective
than
convolution
when
the
goal
is
to
simultaneously
reducenoiseandpreserveedges. I=imread(coins.tif');
J=imnoise(I,'salt &pepper',0.02);
K=medfilt2(J);
imshow(J),figure,imshow(K)
Exercise:observethedifferenceofaverageandmedianfilterwithincreasingorderi.e.5x5etconincreasingsaltandpeppernoisei.e.0.4ormore.
73
Guassian noise
Gaussiannoiseisanidealizedformofwhitenoise,whichiscausedbyrandomfluctuationsinthesignal.Gaussiannoiseis
whitenoise
which
is
normally
distributed.
If
the
image
is
representedasI,andtheGaussiannoisebyN
then we can model a nois ima e b sim l addin the two, I+N
t_ga=inoise(t,'gaussian');
Aswithsaltandpeppernoise,thegaussian
parameteralsocantakeoptionalvalues,givingthemeanandvarianceofthenoise.
74
CleaningGaussiannoise
TherecouldmanycopiesofcorruptedimagehavingGaussiannoise.E.g satelliteimages
ifasatellitepassesoverthesamespotmanytimes,wewillobtainmanydifferentimagesof
the same lace.
InsuchacaseaverysimpleapproachtocleaningGaussiannoiseistosimplytakethe
averagethemeanofalltheimages.
75
edges
Edgescontainsomeofthemostusefulinformationinanimage.Wemayuseedgestomeasure:
thesizeofobjectsinanimage;
toisolateparticularobjectsfromtheirbackground;
.
Anedgemaybelooselydefinedasalineofpixelsshowinganobservabledifference.
76
OriginofEdges
depth discontinuity
surface colordiscontinuit
surface normal discontinuity
Edgesarecausedbyavarietyoffactors
illumination discontinuity
EdgeTypes
Step Edges
Line Edges
Change is measured by derivative in 1DBiggest change, derivative has maximummagnitude Or 2nd derivative is zero.Edge Detection:Difference operatorsParametric-model matchers
-
7/23/2019 DIP2 Image Enhancement1
14/18
Graylevelprofile
660 1 2 30 0 2 2 2 2 23 3 3 3 30 0 0 0 0 0 0 0 7 7 5 5
7
6
5
4
3
2
1
0
Edgesbydifference
suppose that the values of the "ramp" edge infigure
are, from left to right:20,20,20,20,20,20,100,180,180,180,180,180If we form the differences, by subtracting eachvalue from its successor, we obtain:
80
0,0,0,0,0,80,0,0,0,0and it is these values which are plotted nextfigure:
It appears that the difference tends toenhance edges, and reduce othercomponents.
We can define the difference in three separate ways:
81
Edgesdetectors
Threesteps:
Noisereduction
E.g.,medianfilter
E.g.,meanfilter
82
Largefilter=>removenoise
Largefilter=>removeedges
Smallfilter=>keepedges
Smallfilter=>keepnoise
Edgeenhancement
Edge
localisation
Edgesdetectors
Threesteps:
Noisereduction
Edgeenhancement
Calculatecandidatesfortheedges
83
Decidewhichedgecandidatestokeep
Edgesdetectors
Welllookatfourmethods(butothersexist!)
Withrespecttocomplexity(simplestfirst): Sobel
84
Laplacian
Canny
rew
-
7/23/2019 DIP2 Image Enhancement1
15/18
Gradientoperators
(a): Roberts cross operator (b): 3x3 Prewitt operator(c): Sobel operator (d) 4x4 Prewitt operator
clear
ic=imread('ic.jpg')
figure,imshow(ic),title('orginal')
ic=ic(:,:,1)
px=[101;101;101];%highlightsverticaledges
icx=filter2(px,ic);
%divide255converttheics matrixintodoubletobeshowninimshow
figure,imshow(icx/255),
title('px')
py=px';
icy=filter2(py,ic);%highlightshorizontaledges
figure,imshow(icy/255),title('py')
%wecancreateafigurecontainingalltheedgeswith:
edge_p=sqrt(icx.^2+icy.^2);
figure,imshow(edge_p/255),title('alledges')
%binaryimagecontainingedgesonlycanbeproducedbythresholding.
edge_t=im2bw(edge_p/255,0.3);
figure,imshow(edge_t);title('bwl')
%WecanobtainedgesbythePrewittlters directlybyusingthecommand
edge_p=edge(ic,'prewitt');
figure,imshow(edge_p);title('directperwettl')
Exercise:useRobertandsobel filtertodosamejob
86
TheCannyEdgeDetector
originalimage
(Lena)
The Canny Edge Detector
magnitude of the gradient
TheCannyEdgeDetector
After non-maximum suppression
Seconddifferences Abasicdefinitionofthefirstorderderivativeofaone
dimensionalfunctionf(x)is
Wedefineasecondorderderivativeasthedifference.
)()1( xfxfx
f+=
2
).(2)1()1(2
xfxfxfx
++=
-
7/23/2019 DIP2 Image Enhancement1
16/18
-
7/23/2019 DIP2 Image Enhancement1
17/18
Implementation
+=
),(),(
),(),(),(2
2
yxfyxf
yxfyxfyxgIf the center coefficient is negative
If the center coefficient is positive
Laplacian filter image is superimposed on a dark featureless backgroundBackground can be recovered preserving the sharpness effect of thelaplacian operator simply by adding the original image and lapician image
),(2 yxf
Where f(x,y) is the original image
is Laplacian filtered image
g(x,y) is the sharpen image
Implementation closeall
f=imread('moon.jpg');
! fspecial istogenerate2Dfilter
w=fspecial('laplacian',0)
! implementationofequation3Aofslides,
g1=imfilter(f,w,'replicate')
figure,title('Lapician filteredimagewithoutim2double')
imshow(g1)
l l l l l l
!butbecaz ofuint8negativevalueshave beentruncated
!convertimageintodouble
f2=im2double(f);
g2=imfilter(f2,w,'replicate')
figure,title('Lapician filteredimagewithim2double')
imshow(g2)
! Restoregraytonelostbyusinglapician bysubtractingas centrecoefficientisnegative.
g=f2g2;
figure,title('sharpern imageusingLapician implementation')
imshow(g)98
Usingdifferentlaplacian operator,diognal maskaresharper.
closeall
f=imread('moon.jpg');
! fspecial istogenerate2Dfilter
w4=fspecial('laplacian',0)
w8=[111;1 81;111];
f=im2double(f);
g4=fimfilter(f,w4,'replicate');
g8=fimfilter(f,w8,'replicate');
imshow(f)
figure,imshow(g4)
figure,imshow(g8)
99
ZeroCrossing Ingeneralthesearetheplaceswherethefilterschanges
sign.Zerocrossingisdefinedinfilteredimagessatisfying
eitheroffollowing
Theyhavenegativevalueandarenexttoapixelwhosegreyvalueispositive.
Theyhaveavalueofzeroandarebetweennegativeandpositivevaluespixels.
100
OnemoremethodoftheEdge
Detection
TaketheLaplacefilterandapplyzerocrossing
I=imread('coins.bmp');
LP_filter=fspecial('laplacian',0);
= ' ', , , ,
_
verygoodresult????
Toeliminatethem,wemayfirstsmooththeimagewithaGaussianfilter.
Edgedetection;theMarrHildreth method:
101
MarrHildreth method
1. smooththeimagewithaGaussianlter,2. convolvetheresultwithalaplacian,3. fin dthezerocrossings. Methodtobeascloseaspossibletobiologicalvision. Thefirsttwostepscanbecombinedintoone,toproduceaLaplacian of
Gaussianor
LoG filter.
fspecial('log',13,2) h=fspecial('log',hsize,sigma)returnsarotationallysymmetric
Laplacian ofGaussianfilterofsizehsize withstandarddeviationsigma(positive).hsize canbeavectorspecifyingthenumberofrowsandcolumnsinh,oritcanbeascalar,inwhichcasehisasquarematrix.Thedefaultvalueforhsize is[55]and0.5forsigma.
log=fspecial('log',13,2); edge(ic,'zerocross',log);
102
-
7/23/2019 DIP2 Image Enhancement1
18/18
Edge Enhancement: Unsharp masking
Arelatedoperationistomakeedgesinanimageslightlysharperratherthanisolating,whichgenerallyresultsinanimagemorepleasingtothehumaneye.
Aprocesstosharpenimagesconsistsofsubtracting ablurredversionofanimagefromtheimageitself.Thisprocess,calledunsharpmasking,isexpressedas
),(),(),( yxfyxfyxfs =),( yxfs
),( yxf),( yxf
Where denotes the sharpened image obtained by unsharpmasking, and is a blurred version of
Sharpeningimage closeall
x=imread('butterfly.bmp');
figure,imshow(x(:,:,1)),title('original')
x=x(:,:,1);
f=fspecial('average');
104
xf=filter2(f,x);
xu=double(x)xf/1.5
figure,imshow(xu/70),title('sharpen edges')
Thelastcommandscalestheresultsothatimshow displaysanappropriateimage;
Highboostfiltering
Alliedtounsharp maskinglters arethehighboostfilters,whichareobtainedby
Highboost=A(orginal) lowpass,
g oos ere mage, hb s e ne a any
point(x,y)as1),(),(),( = AwhereyxfyxAfyxfhb
),(),(),()1(),( yxfyxfyxfAyxfhb +=
),(),()1(),( yxfyxfAyxf shb +=
This equation is applicable general and does not state explicityhow the sharp image is obtained
HighboostfilteringandLaplacian
IfwechoosetousetheLaplacian,thenweknowfs(x,y)
+
=
),(),(
),(),(2
2
yxfyxAf
yxfyxAff
hb
If the center coefficient is negative
If the center coefficient is positive
-1
-1
A+4 -1
-1
0 0
0 0
-1
-1
A+8 -1
-1
-1 -1
-1 -1