110 likes | 277 Views
Image Segmentation I. Comp344 Tutorial Kai Zhang. Outline. Line & Edge detection Hough transform. Line Detection. Apply a mask and compute the image response Design of Mask determines what kind of line is detected -1 -1 -1 -1 -1 2 -1 2 -1 2 -1 -1
E N D
Image Segmentation I Comp344 Tutorial Kai Zhang
Outline • Line & Edge detection • Hough transform
Line Detection • Apply a mask and compute the image response • Design of Mask determines what kind of line is detected • -1 -1 -1 -1 -1 2 -1 2 -1 2 -1 -1 • 2 2 2 -1 2 -1 -1 2 -1 -1 2 -1 • -1 -1 -1 2 -1 -1 -1 2 -1 -1 -1 2 • Horizontal +45o vertical -45o
Codes • f = imread('wirebond.tif'); • w = [2 -1 -1; -1 2 -1; -1 -1 2]; • g = imfilter(double(f),w); • imshow(g,[]); • gtop = g(1:120,1:120); • gtop = pixeldup(gtop, 4); • figure,imshow(gtop, []); • gbot = g(end - 119:end, end-119:end); • gbot = pixeldup(gbot, 4); • figure,imshow(gbot, []); • g = abs(g); • figure,imshow(g,[]); • T = max(g(:)); • g = g >= T; • figure,imshow(g,[]);
Hough Transform • A method for line detection on binary images • Transform points in the x-y plane into the parameter space (\rho, \theta) • Detect local maximum on the parameter space (kind of voting), which corresponds to strong line signals • Complexity much lower than naïve method
Sparse matrices in matlab • Sparse matrix: Matrices that contain only a small number of non-zero elements • Sparse matrix can reduce both the storage and the computational complexity • Example • A = [ 0 0 0 5; 0 2 0 0; 1 3 0 0; 0 0 4 0]; • S = sparse(A); • S = • (3,1) 1 • (2,2) 2 • (3,2) 3 • (4,3) 4 • (1,4) 5
Codes • f = zeros(101,101); • f(1,1) = 1; f(101,1) = 1; f(1,101) = 1; • f(101,101) = 1; f(51,51) = 1; • figure,imshow(f,[]); • H = hough(f); • figure, imshow(histeq(H),[]); • [H, theta, rho] = hough(f); • figure, imshow(theta, rho, histeq(H), [], 'notruesize'); • axis on; axis normal; • xlabel('theta'),ylabel('\rho');
codes • f = double(imread('building.tif')); • [canny,tc] = edge(f,'canny',[0.04 0.1], 1.5); • figure,imshow(canny,[]); • f = g_canny_default; • [H, theta, rho] = hough(f,0.5); • figure, imshow(theta, rho, H/30, 'notruesize'),axis on; axis normal; • [r,c] = houghpeaks(H, 5); • hold on; plot(theta(c),rho(r),'linestyle','none','marker','s','color','w'); • lines = houghlines(f, theta, rho, r,c); • figure,imshow(f,[]);hold on; • for k = 1:length(lines); • xy =[lines(k).point1; lines(k).point2]; • plot(xy(:,2),xy(:,1),'LineWidth',4,'Color',[.9 .0 .0]); • end;