Вы находитесь на странице: 1из 27

EGGN512ComputerVisionColoradoSchoolofMines,EngineeringDivisionProf.

WilliamHoff
BinaryImageProcessing
1
EGGN512ComputerVisionColoradoSchoolofMines,EngineeringDivisionProf.WilliamHoff
BinaryImages
Binarymeans
0or1valuesonly
Alsocalledlogicaltype(true/false)
Obtainedfrom
Thresholding graylevelimages
Resultoffeaturedetectors
Oftenwanttocountormeasure
shapeof2Dbinaryimageregions
Typicalapplications
Objectsonaconveyorbelt
Charactersandtext,maps
Chromosomes
Fingerprints
Circuitboards
Overheadaerialimages
2
EGGN512ComputerVisionColoradoSchoolofMines,EngineeringDivisionProf.WilliamHoff
Outline
Thresholding
Logicoperations
Connectedcomponents
Morphologicaloperators
Regionproperties,moments
Lab exampletofindfiducial target
3
EGGN512ComputerVisionColoradoSchoolofMines,EngineeringDivisionProf.WilliamHoff
Thresholding
Convertgrayscaleimagetobinary(0sand1s)
Simplifiesprocessingandcomputationoffeatures
Canuseasinglethresholdvalue(global)oralocalvalue(adaptive)
0
500
1000
1500
2000
2500
3000
0 50 100 150 200 250
Thresholding inMatlab:
B = I > t;
Imageeight.tif
4
EGGN512ComputerVisionColoradoSchoolofMines,EngineeringDivisionProf.WilliamHoff
GlobalThresholdComputation
Assumehistogramisbimodal
Choosethresholdvaluetoseparatemodes,andminimize
classificationerror
Otsualgorithm:minimizethewithingroupvariances
5
EGGN512ComputerVisionColoradoSchoolofMines,EngineeringDivisionProf.WilliamHoff
MatlabExamples
Images
cameraman.tif,eight.tif,coins.png
Functions
t = graythresh(I) % Otsu algorithm
BW = im2bw(I,t); % performs thresholding
6
EGGN512ComputerVisionColoradoSchoolofMines,EngineeringDivisionProf.WilliamHoff
LogicOperations
Recalltruthtablesforlogicoperators
AlsoXOR,
NAND,NOR
7
EGGN512ComputerVisionColoradoSchoolofMines,EngineeringDivisionProf.WilliamHoff
Matlab
Createbinaryimages
A=false(10,10), A(1:6,1:6)=true;
B=false(10,10), B(4:10,4:10)=true;
NOT
~A
imcomplement(A) % works on gray level too
AND
A&B;
OR
C = A|B;
CanalsodobitwiseAND,OR,etc ofunsignedintegers
A=uint8(zeros(10,10)), A(1:6,1:6)=30;
B=uint8(zeros(10,10)), B(4:10,4:10)=120;
bitand(A,B);
Doesaphotonegative:255A
8
EGGN512ComputerVisionColoradoSchoolofMines,EngineeringDivisionProf.WilliamHoff
ConnectedComponents
Defineadjacency
4adjacent
8adjacent
TwopixelsareconnectedinSifthereis
apathbetweenthemconsistingentirely
ofpixelsinS
Sisa(4 or8)connectedcomponent
(blob)ifthereexistsapathbetween
everypairofpixels
Labelingistheprocessofassigning
thesamelabelnumbertoeverypixelin
aconnectedcomponent
9
EGGN512ComputerVisionColoradoSchoolofMines,EngineeringDivisionProf.WilliamHoff
Example
1 1
1 1
1 1
1 1 1
1 1 1
Handlabelsimplebinaryimage
Binaryimage Labeledimage(4
connected)
Labeledimage(8
connected)
10
EGGN512ComputerVisionColoradoSchoolofMines,EngineeringDivisionProf.WilliamHoff
AFastLabelingAlgorithm
Onepassthroughimagetoassigntemporarylabels
andrecordequivalences
Secondpasstoreplacetemporarylabelswith
equivalencelabels
Data:
B(r,c)istheinputbinaryimage
L(r,c)istheoutputlabeledimage
11
EGGN512ComputerVisionColoradoSchoolofMines,EngineeringDivisionProf.WilliamHoff
for c=1 to MAXCOL {
for r=1 to MAXROW {
if B(r,c) == 0 then
L(r,c) = 0; % if pixel not white, assign no label
else {
12
EGGN512ComputerVisionColoradoSchoolofMines,EngineeringDivisionProf.WilliamHoff
Example
1 1 1
1 1 1
1 1 1
1 1 1
Binaryimage Temporarylabelsafter
1
st
pass
Final(equivalence)
labelsafter2
nd
pass
13
EGGN512ComputerVisionColoradoSchoolofMines,EngineeringDivisionProf.WilliamHoff
MatlabExample
Labelingconnectedcomponents(white
blobs)
im2bw
thresholdtoconverttobinaryimage
bwlabel
doconnectedcomponentlabeling
generateanimageoflabels
label2rgb
forvisualization
convertseachlabeltoarandomcolor
Ifwewanttofindblackblobs
imcomplement
Flipblackandwhiteregions
thenrepeatsteps
ImageFig916(a).jpg
14
EGGN512ComputerVisionColoradoSchoolofMines,EngineeringDivisionProf.WilliamHoff
>>I=imread('Fig9.16(a).jpg');
>>imshow(I,[])
>>whos
NameSizeBytesClassAttributes
I512x512262144uint8
>>BW=im2bw(I,graythresh(I));
>>figure,imshow(BW)
>>[L,num]=bwlabel(BW);
>>figure,imshow(L,[])
>>num
num=
17
>>RGB=label2rgb(L);
>>figure,imshow(RGB)
>>BW=imcomplement(BW);
>>[L,num]=bwlabel(BW);
>>RGB=label2rgb(L);
>>figure,imshow(RGB)
15
EGGN512ComputerVisionColoradoSchoolofMines,EngineeringDivisionProf.WilliamHoff
BinaryImageMorphology
Operationsonbinaryimages:
dilationanderosion
openingandclosing
Usesinclude
shrinkingandexpandingregions
eliminatingsmallregionsorholes
Operationsareperformedwithastructuring
elementS
asmallbinaryimage
likeafiltermask
16
EGGN512ComputerVisionColoradoSchoolofMines,EngineeringDivisionProf.WilliamHoff
Dilation
Definedas
where
S
b
isthestructuring
elementS,shiftedtob
Procedure
SweepSoverB
Everywheretheoriginof
Stouchesa1,ORSwith
B
Expandsregions

B b
b
S S B
e
=
1 1
1 1
1 1 1
1 1 1
1 1 1
S B
B
S
17
EGGN512ComputerVisionColoradoSchoolofMines,EngineeringDivisionProf.WilliamHoff
18
EGGN512ComputerVisionColoradoSchoolofMines,EngineeringDivisionProf.WilliamHoff
Erosion
Definedas
Procedure
SweepSoverB
EverywhereSis
completelycontainedin
B,outputa1atthe
originofS
Shrinksregions
{ } S s B s b b S B e e + = O , |
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1
1 1 1
1 1 1
1 1 1
B
S
S BO
19
EGGN512ComputerVisionColoradoSchoolofMines,EngineeringDivisionProf.WilliamHoff
Matlab
Createastructuringelement
strel(disk, 5) % disk shape, radius=5
Dilation
imdilate(I,S)
Erosion
imerode(I,S)
20
EGGN512ComputerVisionColoradoSchoolofMines,EngineeringDivisionProf.WilliamHoff
OpeningsandClosings
Opening
Erosionfollowedbydilation
Eliminatesmallregionsand
projections
Closing
Dilationfollowedbyerosion
Fillinsmallholesandgaps
Matlabimopen, imclose
( ) S S B S B O =
( ) S S B S B O = -
21
EGGN512ComputerVisionColoradoSchoolofMines,EngineeringDivisionProf.WilliamHoff
22
EGGN512ComputerVisionColoradoSchoolofMines,EngineeringDivisionProf.WilliamHoff
ClosingExample
Matlabimageeight.tif
threshold,complement
closewithdiskofradius=5
23
EGGN512ComputerVisionColoradoSchoolofMines,EngineeringDivisionProf.WilliamHoff
Basicfeatures
Higherordermoments
Principalaxes
computedfromsecondordermoments
Matlab functionregionprops(L)
RegionProperties

e
=
R c r
A
) , (
1 Area

e e
= =
R c r R c r
c
A
c r
A
r
) , ( ) , (
1
,
1
Centroid
( ) ( ) ( )( )

e e e
= = =
R c r
rc
R c r
cc
R c r
rr
c c r r
A
c c
A
r r
A
) , ( ) , (
2
) , (
2
1
,
1
,
1

24
EGGN512ComputerVisionColoradoSchoolofMines,EngineeringDivisionProf.WilliamHoff
MatlabStructures
Astructureisanarraywithnamedfields
Accessusing:structurename.fieldname
SeeMatlabhelpformoreinfo
Example
>> blobs = regionprops(L);
>> blobs(1)
ans =
Area: 2058
Centroid: [15.7216 179.8717]
BoundingBox: [0.5000 133.5000 34 93]
25
EGGN512ComputerVisionColoradoSchoolofMines,EngineeringDivisionProf.WilliamHoff
MatlabGraphics
rectangle('Position', [x y w h]), EdgeColor, r);
line([x1 x2], [y1 y2], Color, r);
(x,y)
w
h
(x1,y1)
(x2,y2)
26
EGGN512ComputerVisionColoradoSchoolofMines,EngineeringDivisionProf.WilliamHoff
MatlabExample
Drawboundingboxaroundlargestblob
Drawcrosshaironitscentroid
>>I=imread('Fig9.16(a).jpg');
>>BW=im2bw(I,graythresh(I));
>>L=bwlabel(BW);
>>blobs=regionprops(L);
>>areas=cat(1,blobs(:).Area);%concatentatealongdimension1
>>[value,index]=max(areas);
>>
>>value
value=
14240
>>index
index=
6
>>imshow(I)
>>rectangle('Position',blobs(index).BoundingBox,'EdgeColor','w');
>>rectangle('Position',[x01y0133],'EdgeColor','k');
27