1.19k likes | 1.37k Views
Introduction to Computer Vision Lecture 8. Roger S. Gaborski. Straight Line Detection. Man made object often contain straight lines Straight lines can be detected by first filtering the image with line kernels and then linking together edges that are on a straight line
E N D
Introduction to Computer VisionLecture 8 Roger S. Gaborski
Straight Line Detection • Man made object often contain straight lines • Straight lines can be detected by first filtering the image with line kernels and then linking together edges that are on a straight line • The Hough Transform is another method for detecting straight lines- works with EDGE DATA • Hough transforms can also detect other shapes, such as, circles • Chapter 11 in textbook Roger S. Gaborski
image26.jpg Roger S. Gaborski
>> im = imread('image26.jpg'); >> figure, imshow(im) >> im = imcrop(im); >> figure, imshow(im) Roger S. Gaborski
>> imGray = rgb2gray(im); >> imGray = im2double(imGray); >> whos Name Size Bytes Class Attributes ans 1x1 1 uint8 im 355x410x3 436650 uint8 imGray 355x410 1164400 double >> max(imGray(:)) ans = 1 >> min(imGray(:)) ans = 0.0745 Roger S. Gaborski
>> imSobelH = edge(imGray, 'sobel'); >> figure, imshow(imSobelH), title('Sobel'); Roger S. Gaborski
>> imEdge = edge(imGray, 'canny', [ ], 3); >> figure, imshow(imEdge) Roger S. Gaborski
Sobel – Canny Comparison Roger S. Gaborski
Consider a set of points • We would like to find a subsets of the points that lie on straight lines • We would like to know the end points, orientation, location, join segments, etc. Roger S. Gaborski
X-Y Coordinate System • Consider x-y coordinate system • Assume we have a straight line in the x-y coordinate system • The slope and intercept are constant parameters for a particular line y y = mix+bi or y = aix+bi mi , ai : slope of line bi : y intercept x Roger S. Gaborski
The Line is a Point in Parameter Space bi (intercept) y b ai x a (slope) Spatial Domain Parameter Domain Roger S. Gaborski
A Single Point in the Spatial Domain is a Line in Parameter Space y1 y b b = -ax1 + y1 Infinite number of Lines can pass through x1 y1 each with its own a,b x1 x a Spatial Domain Parameter Domain (xi , yi ) is constant, a and b are the variables Roger S. Gaborski
GOAL: Given Two Points in the spatial domain use the Hough transform to find the equation of the line passing through the points y1 y2 y x2 x1 x Spatial Domain Roger S. Gaborski
A Two Points in the Spatial Domain are Two Lines in Parameter Space y1 y2 b’ y b x2 x1 b = -ax2+ y2 b = -ax1 + y1 a’ x a Spatial Domain Parameter Domain a’ and b’ are the parameters of the straight line that passes through the two points in the spatial domain (xi , yi ) is constant, a and b are the variables Roger S. Gaborski
Mapping from Spatial Domain to a Parameter Space • Consider a particular point (xi,yi) in the x,y plane • An infinite number of lines can pass through this point • All satisfy slope-intercept equation: yi= a xi + b for some a,b • Solve for b: b = -a xi + yi • Parameter space a-b plane yields single line for (xi,yi) Roger S. Gaborski
Parameter Space • Consider a second point (xj,yj) in the x,y plane • Also has a line associated with it in parameter space • This line intersects the line associated with (xi,yi) at ( a’,b’ ) • a’ is the slope and b’ is the intercept of the line containing both (xi,yi) and (xj,yj) in the x-y space • All points on this line have lines in the parameter space that intersect at ( a’, b’ ) Roger S. Gaborski
Chapter 10 Image Segmentation Roger S. Gaborski
Parameter Space • (a, b) space usually referred to as (, ) space • Each line plots to a point in (, ) parameter space, but must satisfy: • = x1cos + y1 sin where x1 y1 are constants (, ) ( rho, theta) Roger S. Gaborski
Parameter Space • Locus of such lines in x,y space is a sinusoid in parameter space • Any point in x,y plane corresponds to a sinusoid curve in , space Roger S. Gaborski
Chapter 10 Image Segmentation Roger S. Gaborski
( , ) Space • Define and • For each point from from edge detector • Plug in values for x and y: = x cos + ysin • For each value of in parameter space, solve for • For each pair (from previous step) record in quantized space (this is a hit) • After evaluation of all edge points, number of hits in each block corresponds to the number of pixels on the line as defined by the values Roger S. Gaborski
Hough Transform – General Idea (one point) min max min Sample of two lines (x1y1) max Two of an infinite number of lines through point (x1,y1) Space Roger S. Gaborski
Hough Transform – General Idea (three points) min max min Line through three Points (count=3) (x1y1) max Two of an infinite number of lines through point (x1,y1) Space Roger S. Gaborski
YouTube Videos • EGGN 512 - Lecture 13-1 Hough • http://www.youtube.com/watch?v=uDB2qGqnQ1g • EGGN 512 - Lecture 13-2 Hough • http://www.youtube.com/watch?v=o-n7NoLArcs&feature=relmfu • EGGN 512 - Lecture 13-3 Hough • http://www.youtube.com/watch?v=GZ61BRH9KFE&feature=relmfu Roger S. Gaborski
Simulated Vertical Edge %'Edge' image for Hough Transform imH = zeros(100); imH(:, 80) = 1; figure, imshow(imH, [ ],'InitialMagnification', 'fit' ),title('Edge Map') [H,theta,rho] = hough(imH); figure, imshow(H,[],'XData',theta,'YData',rho,'InitialMagnification','fit'); %figure, imshow(theta,rho,H, [ ], 'notruesize'),title('Vertical Line') axis on, axis normal; xlabel('\theta'), ylabel('\rho'), colormap(gray), colorbar Roger S. Gaborski
Edge image and Hough results rho = 80 units Roger S. Gaborski
Simulated Horizontal Edge >> %Horizontal line imH = imH'; figure, imshow(imH, [ ],'InitialMagnification', 'fit' ),title('Edge Map') [H,theta,rho] = hough(imH); figure, imshow(H,[],'XData',theta,'YData',rho,'InitialMagnification','fit'); axis on, axis normal; xlabel('\theta'), ylabel('\rho'), colormap(gray), colorbar Roger S. Gaborski
>> %Line at -45 Degrees imH45 = zeros(100); for a=1:100 imH45(a,a) = 1; end figure, imshow(imH45, [ ],'InitialMagnification', 'fit' ),title('Edge Map, -45 degrees') [H,theta,rho] = hough(imH45); figure, imshow(H,[],'XData',theta,'YData',rho,'InitialMagnification','fit'); axis on, axis normal; xlabel('\theta'), ylabel('\rho'), colormap(gray), colorbar Roger S. Gaborski
>> %Line at 45 Degrees imH45_2 = zeros(100); for a=1:100 imH45_2(a,101-a) = 1; end figure, imshow(imH45_2, [ ],'InitialMagnification', 'fit' ),title('Edge Map, 45 degrees') [H,theta,rho] = hough(imH45_2); figure, imshow(H,[],'XData',theta,'YData',rho,'InitialMagnification','fit'); axis on, axis normal; xlabel('\theta'), ylabel('\rho'), colormap(gray), colorbar Roger S. Gaborski
Simple square %Simple square im = zeros(100); im(20:80,20)=1; im(20:80,80)=1; im(20, 20:80)=1; im(80, 20:80)=1; figure, imshow(im, [ ],'InitialMagnification', 'fit' ),title('Box') [H,T,R] = hough(im); imshow(H,[],'XData',T,'YData',R,'InitialMagnification','fit'); xlabel('\theta'), ylabel('\rho'); axis on, axis normal Roger S. Gaborski
Peak Detection • Need to find peaks in Hough Transform • Due to quantization, pixels not in perfectly straight line, peaks lie in more than one cell • Approach: • Find Hough cell containing maximum number of entries and record location • Set immediate neighbors to zero • Look for next maximum peak, • etc Roger S. Gaborski
Find Peaks %Simple square im = zeros(100); im(20:80,20)=1; im(20:80,80)=1; im(20, 20:80)=1; im(80, 20:80)=1; figure, imshow(im, [ ],'InitialMagnification', 'fit' ),title('Box') [H,T,R] = hough(im); P = houghpeaks(H,4); imshow(H,[],'XData',T,'YData',R,'InitialMagnification','fit'); xlabel('\theta'), ylabel('\rho'); axis on, axis normal, hold on; plot(T(P(:,2)),R(P(:,1)),'s','color','white'); Roger S. Gaborski
Peak Roger S. Gaborski
% Find lines and plot them lines = houghlines(im,T,R,P,'FillGap',5,'MinLength',7); figure, imshow(im), hold on max_len = 0; for k = 1:length(lines) xy = [lines(k).point1; lines(k).point2]; plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green'); % plot beginnings and ends of lines plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow'); plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red'); % determine the endpoints of the longest line segment len = norm(lines(k).point1 - lines(k).point2); if ( len > max_len) max_len = len; xy_long = xy; end end % highlight the longest line segment plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','cyan'); Roger S. Gaborski
>> [H,T,R] = hough(imEdge); P = houghpeaks(H,4); imshow(H,[],'XData',T,'YData',R,'InitialMagnification','fit'); xlabel('\theta'), ylabel('\rho'); axis on, axis normal, hold on; plot(T(P(:,2)),R(P(:,1)),'s','color','white'); % Find lines and plot them lines = houghlines(imEdge,T,R,P,'FillGap',5,'MinLength',7); figure, imshow(imEdge), hold on max_len = 0; for k = 1:length(lines) xy = [lines(k).point1; lines(k).point2]; plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green'); % plot beginnings and ends of lines plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow'); plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red'); % determine the endpoints of the longest line segment len = norm(lines(k).point1 - lines(k).point2); if ( len > max_len) max_len = len; xy_long = xy; end end % highlight the longest line segment plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','cyan'); Roger S. Gaborski
Canny Edge Image Roger S. Gaborski
P=8, Gap Fill = 5 P=12 Gap Fill = 5 P = 8, Gap Fill = 25 P = 12, Gap Fill = 25 Roger S. Gaborski
Chapter 10 Image Segmentation Roger S. Gaborski
Pineapple and Orange Examples Roger S. Gaborski
Histograms • A histogram is just a method for summarizing data, for example, gray level pixel values • Can also summarize edge data Roger S. Gaborski
GRAY PIXEL VALUE HISTOGRAM >> imP1=double(imread('Pineapple1.jpg’)); >> figure, imshow(imP1) >> %Convert to Gray scale >> imP1gray = rgb2gray(imP1); >> figure, imshow(imP1gray) >>%Minimum and Maximum code values >> MAXcode = max(imP1gray(:)) MAXcode = 214 >> MINcode = min(imP1gray(:)) MINcode = 1 >>%Histogram of Gray Level Pixel Values Roger S. Gaborski
Edges >> imP1=double(imread('Pineapple1.jpg')); >> figure, imshow(imP1) >> %Convert to Gray scale >> imP1gray = rgb2gray(imP1); >> figure, imshow(imP1gray) >> figure, hist(imP1gray(:)) >> >> w=[1 1 1;0 0 0; -1 -1 -1] w = 1 1 1 0 0 0 -1 -1 -1 >> imP1edge = imfilter(imP1gray,w); >> figure, imshow(imP1edge, [ ]) Roger S. Gaborski
>> figure, imshow(imP1edge,[ ]) >>figure, imshow(abs(imP1edge),[ ]) Roger S. Gaborski
Edge Histogram Edge histogram Absolute Edge Histogram Roger S. Gaborski
Edge Image Thresholds >> figure, imshow(abs(imP1edge)>1.0),title('Threshold 1.0') Roger S. Gaborski