ray
Posts:
1
Registered:
5/9/13


Find the begining and end of multiple lines
Posted:
May 9, 2013 10:44 PM


Hello world,
So I have been assigned a project that is due in three days. I have completed most of the project but I am running into a problem. The assignment involves finding different shapes on an image and displaying what each shape is. I have found all of the small and large circles in the image and displayed the centroid for each shape. I have also done the same for the squares. The problem is for the lines. I need to know the beginning and ending pixel location for each line. Unfortunately, the line is more than a few pixels wide. I did a canny filter and then hough lines. The problem is that I get the hough lines on each side of the line, not the center. I also tried bwmorph thin and skeleton to try and make the line as thin as possible. After I applied the morph, the image was severely altered. I could not find what I was looking for. Is there any suggestion on how I should approach this problem? One other problem is that once I find all of the pixel locations, I have to report them in inches from the lower left pixel location. Any idea on how to do that? I am not asking anyone to write code for me, I really am interested in learning what is going on here. However, I will not look down on anyone who would like to provide code.
The image can be seen here: http://www.flickr.com/photos/63235735@N05/8725185346/ Sorry I don't know how to make that a hyperlink
I am including the code that I have so far below:
clc; clear all; close all;
A = imread('C:\Users\Ray\Desktop\board_final_1.png'); imshow(A) [c1, r1] = imfindcircles(A, [18 22], 'ObjectPolarity' , 'dark', 'Sensitivity', 0.95); [c2, r2] = imfindcircles(A, [1 4], 'ObjectPolarity' , 'dark', 'Sensitivity', 0.9); [c3, r3] = imfindcircles(A, [6 9], 'ObjectPolarity' , 'dark', 'Sensitivity', 0.95); C = [c3 r3]; C = sortrows(C, 1); figure; imshow(A, []); viscircles(c1, r1,'EdgeColor','b'); viscircles(c2,r2,'EdgeColor','b'); viscircles(c3, r3, 'Edgecolor','r'); hold on P12 = plotAndFind(C(1,1), C(1,2), C(1,3), C(2,1), C(2,2), C(2,3)); P34 = plotAndFind(C(6,1), C(6,2), C(6,3), C(7,1), C(7,2), C(7,3)); BoardBW = im2bw(A); BW1 = bwperim(BoardBW, 8); for i = 1:649 if (BW1(i,1) == 0) BW1(i,1) =1; elseif (BW1(i, 1) == 1) BW1(i, 1) = 0; end if (BW1(648,i) == 0) BW1(648,i) =1; elseif (BW1(648,i) == 1) BW1(648,i) = 0; end if (BW1(i,648) == 0) BW1(i,648) =1; elseif (BW1(i, 648) == 1) BW1(i, 648) = 0; end end STATS = regionprops(BW1, 'Perimeter', 'Area', 'Centroid'); Perimeter = cat(1, STATS.Perimeter); Area = cat(1, STATS.Area); Centroid = cat(1, STATS.Centroid); [m n] = size(Perimeter); j = 1; k = 1; h = 1;
for i = 1:m if(Area(i) == 36) redcircles(j,:) = Centroid(i,:); j = j + 1; else if((Area(i) >100) && (Area(i) < 150)) squares(k,:) = Centroid(i,:); k = k + 1; else if((Area(i) >170) && (Area(i) < 190)) blackcircles(h,:) = Centroid(i,:); h = h + 1; end end end end redcircles squares blackcircles [m n] = size(redcircles); rad = 2*ones(m,1); viscircles(redcircles, rad, 'EdgeColor', 'b' ); [m n] = size(squares); rad = 2*ones(m,1); viscircles(squares, rad, 'EdgeColor', 'b' ); [m n] = size(blackcircles); rad = 2*ones(m,1); viscircles(blackcircles, rad, 'EdgeColor', 'b' );
%EVERYTHING FROM HERE DOWN IS WHAT I HAVE BEEN TRYING TO DO. %EVERYTHING ABOVE IS WHAT I HAVE ALREADY DONE.
%{ I = imread('C:\Users\Ray\Desktop\board_final_1.png'); % Use the EdgeDetection System object to find edges in the % intensity image. This step outputs a binary image % required by the HoughTransform System object and % improves the efficiency of the HoughLines System object. hedge = vision.EdgeDetector; csc = vision.ColorSpaceConverter('Conversion', 'RGB to intensity'); hidtypeconv = vision.ImageDataTypeConverter('OutputDataType','single'); img = imread('C:\Users\Ray\Desktop\board_final_1.png'); img1 = step(hidtypeconv, img); edges = hedge, img1; imshow(edges); hhoughtrans = vision.HoughTransform(pi/360,'ThetaRhoOutputPort', true); hfindmax = vision.LocalMaximaFinder(1,'HoughMatrixInput', true); hhoughlines = vision.HoughLines('SineComputation','Trigonometric function'); % Find the edges in the intensity image BW = step(hedge, I);
% Run the edge output through the transform [ht, theta, rho] = step(hhoughtrans, BW);
% Find the location of the max value in the Hough matrix. idx = step(hfindmax, ht);
% Find the longest line. linepts = step(hhoughlines, theta(idx(1)), rho(idx(2)), I); %* View the image superimposed with the longest line. imshow(I); hold on; line(linepts([1 3]), linepts([2 4])) %}
%I = imread(''); J = imread('C:\Users\Ray\Desktop\board_final_1.png'); I = im2bw(J);
BW2=bwmorph(I,'skel',inf); figure, imshow (BW2) endPoints = bwmorph(BW2, 'endpoints'); % Display the image. subplot(2, 2, 4); imshow(endPoints, []); title('Endpoints Image', 'FontSize', 20); % Find the row and column locations. [rows cols] = find(endPoints); numberOfPointsFound = length(rows); % Plot them. hold on; plot(cols, rows, 'bs');
%rotI = imrotate(I,0,'crop'); BW = edge(I,'canny'); figure, imshow(BW)
[H,T,R] = hough(BW); %figure,imshow(H,[],'XData',T,'YData',R,... %'InitialMagnification','fit'); xlabel('\theta'), ylabel('\rho'); axis on, axis normal, hold on; P = houghpeaks(H,100,'threshold',ceil(0.1*max(H(:))));
x = T(P(:,2)); y = R(P(:,1)); plot(x,y,'s','color','white'); % Find lines and plot them lines = houghlines(BW,T,R,P,'FillGap',50,'MinLength',90); figure, imshow(I), 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','blue'); %}

