Lecture 06 Binary Image Analysis Lecture 06 Binary Image Analysis Mata kuliah: T Computer Vision Tahun: 2010
January 20, 2010T Computer Vision3 Learning Objectives After carefullylistening this lecture, students will be able to do the following : After carefully listening this lecture, students will be able to do the following : show how connected component labeling is performed and its uses in shape classifier show how connected component labeling is performed and its uses in shape classifier demonstrate object area and perimeter calculation based on binary images demonstrate object area and perimeter calculation based on binary images
January 20, 2010T Computer Vision4 From Processing to Analysis Holistic perspective in binary image analysis PHIL High-level vision Low-level vision Localized perspective in binary image processing
January 20, 2010T Computer Vision5 Connectivity X Y X’ Y’ X and Y are connected X’ and Y’ are NOT connected a and b are connected if there exists a path from a to b Notation: if a and b are connected, we write a ~ b
January 20, 2010T Computer Vision6 Connected Components in Digital Images A set S of pixels is a CC if there is at least one path that joins every pair {p,q} of pixels in S, and contains exclusively of pixels in S. Two types of connectivity: 4 - (edge) connectivity and 8- (vertex) connectivity
January 20, 2010T Computer Vision7 Connected Component Two pixels are c-adjacent (c=4 or 8) if they share at least an edge (c=4), or a vertex (c=8). Two pixels are c-connected (c=4 or 8) if it is possible to find a path between these two pixels through pairs of c-adjacent (c=4,8) pixels. A c-connected component is a maximal connected set where each pixel is c-connected to other pixels in the set.
January 20, 2010T Computer Vision8 Example p q p ~ q no matter 4-neighbors or 8-neighbors p q p ~ q only when 8-neighbors is considered
January 20, 2010T Computer Vision9 Component Labeling If 4-neighbors, three connected components If 8-neighbors, two connected components original binary image
January 20, 2010T Computer Vision10 Connected Component Labeling
January 20, 2010T Computer Vision11 MATLAB Function BWLABEL >help bwlabel BWLABEL Label connected components in binary image. L = BWLABEL(BW,N) returns a matrix L, of the same size as BW, containing labels for the connected components in BW. N can have a value of either 4 or 8, where 4 specifies 4-connected objects and 8 specifies 8-connected objects; if the argument is omitted, it defaults to 8. The elements of L are integer values greater than or equal to 0. The pixels labeled 0 are the background. The pixels labeled 1 make up one object, the pixels labeled 2 make up a second object, and so on. [L,NUM] = BWLABEL(BW,N) returns in NUM the number of connected objects found in BW. See also bwareaopen, bweuler, bwlabeln, bwselect, label2rgb.
January 20, 2010T Computer Vision12 Euler Number Euler Number EN=number of connected components – number of holes EN=-3 EN=0 EN=-1
January 20, 2010T Computer Vision13 CC Algorithm Process the image row by row Assign a label to the first pixel of each CC Otherwise assign its label by propagating from left or top Clash! (equivalence) ?
January 20, 2010T Computer Vision14 One approach Propagate the smaller label in case of clash Record the equivalence in a table After the entire image is processed, find the set of equivalence classes Second pass replaces each label with its equivalent class Two passes!
January 20, 2010T Computer Vision15 Boundary of Binary Objects XX X X=X-(X B) _ X=(X B) – B + or
January 20, 2010T Computer Vision16 Chain Codes Boundary Representation 4-directional chain code: directional chain code:
January 20, 2010T Computer Vision17 Two Problems with the Chain Code Chain code representation is conceptually appealing, yet has the following two problems Dependent on the starting point Dependent on the orientation To use boundary representation in object recognition, we need to achieve invariance to starting point and orientation Normalized codes Differential codes
January 20, 2010T Computer Vision18 Normalization Strategy Sort rows First row gives the normalized chain code
January 20, 2010T Computer Vision19 Differential Strategy Differential coding: d k =c k -c k-1 (mod 4) for 4-directional chain codes d k =c k -c k-1 (mod 8) for 8-directional chain codes normalize 90 o
January 20, 2010T Computer Vision20 Shape Numbers= Normalized Differential Chain Codes Differential code: d k =c k -c k-1 (mod 4) differentiate normalize differentiate normalize Note that the shape numbers of two objects related by 90 o rotation are indeed identical
January 20, 2010T Computer Vision21 Examples : Chain Encoding x y Encoding start point 1 unit pixel 2 unit pixel
January 20, 2010T Computer Vision22 PDirection Start Perimeter P = S E + V2 S O units = V2 = units Perimeter Calculation
January 20, 2010T Computer Vision23 Y Direction 0 Additive comp. = 1 x y Y Direction 5 Subtractive comp = (1 x y) – 0.5 Y Direction 1 Subtrac. comp. = (1 x y) Direction 2 dan 6 Zero component (neutral) Area Calculation Y
January 20, 2010T Computer Vision24 P Start AdditiveSubtractive y-coordinate Area = – – 2 – 2 – 2 – 2 – 2.5 – = 29 square units Area Calculation (cont’d)
January 20, 2010T Computer Vision25 MATLAB Implementation
January 20, 2010T Computer Vision26 Segmen citra biner # # #0#0 00####00 00###0# ## Run Length Encoding (RLE) 10(0), 3(1), 1(0), 1(1), 3(0), 4(1), 4(0), 3(1), 1(0), 1(1), 6(0), 2(1), 9(0)
January 20, 2010T Computer Vision27 Segmen citra biner # # #0#0 00####00 00###0# ## Chord Encoding 1 (2,4) (6,6); 2 (2,5); 3 (2,4) (6,6); 4 (5,6). baris kolom
January 20, 2010T Computer Vision28 Matlab Implementation on Shapes Classifier Step 1: Read image Step 2: Convert image from rgb to gray Step 3: Threshold the image Step 4: Invert the Binary Image Step 5: Find the boundaries Concentrate Step 6: Determine Shapes properties Step 7: Classify Shapes according to
January 20, 2010T Computer Vision29 Step 1: Read image RGB = imread('test.bmp'); figure, imshow(RGB), title('INPUT IMAGE');
January 20, 2010T Computer Vision30 Step 2: Convert image from rgb to gray GRAY = rgb2gray(RGB); figure,imshow(GRAY),title('GRAY IMAGE');
January 20, 2010T Computer Vision31 Step 3: Threshold the image. threshold = graythresh(GRAY); BW = im2bw(GRAY, threshold); figure, imshow(BW), title('BINARY IMAGE');
January 20, 2010T Computer Vision32 Step 4: Invert the Binary Image BW = ~ BW; figure,imshow(BW),title('INVERTED BINARY IMAGE');
January 20, 2010T Computer Vision33 Step 5: Find the boundaries Concentrate only on the exterior boundaries. Option 'noholes' will accelerate the processing by preventing bwboundaries from searching for inner contours. [B,L] = bwboundaries(BW, 'noholes'); %[L, N] = bwlabel(BW,8); Step 6: Determine objects properties STATS = regionprops(L, 'all'); % we need 'BoundingBox' and 'Extent'
January 20, 2010T Computer Vision34 Step 7: Classify Shapes according to properties % Square = 3 = (1 + 2) = (X=Y + Extent = 1) % Rectangular = 2 = (0 + 2) = (only Extent = 1) % Circle = 1 = (1 + 0) = (X=Y, Extent < 1) % UNKNOWN = 0 for i = 1 : length(STATS) W(i) = uint8(abs(STATS(i).BoundingBox(3) - STATS(i).BoundingBox(4)) < 0.1); W(i) = W(i) + 2 * uint8((STATS(i).Extent - 1) == 0 ); centroid = STATS(i).Centroid; switch W(i) case 1 plot(centroid(1),centroid(2),'wO'); case 2 plot(centroid(1),centroid(2),'wX'); case 3 plot(centroid(1),centroid(2),'wS'); end
January 20, 2010T Computer Vision35 Step 7: Classify Shapes according to properties (cont’d) figure, imshow(RGB), title('HASIL IDENTIFIKASI SQUARE, CIRCLE & RECTANGLE'); hold on