Help with detecting "Cross feature" using Hough transform
Show older comments
Hello, I have followed the examples to try and find the cross hairs in the image on the left (also attached)

The code I have used is below, but its not picked up the lines at all. I have used the example in the help.
Thanks for any suggestions
Jason
IM=getimage(handles.axes4); %Get Image from axes component (12 bit grayscale)
%Binarise witgh a fixed threshold for now
threshold=2100
filteredImage=IM;
BW = filteredImage < threshold;
%Hough Transform
[H,theta,rho] = hough(BW,'RhoResolution',0.5,'Theta',-90:0.5:89);
figure
subplot(1,4,1);
myImshow(IM,3)
title('my Raw Image');
subplot(1,4,2);
myImshow((BW),1)
title('Binary Image');
subplot(1,4,3);
imshow(imadjust(rescale(H)),'XData',theta,'YData',rho,...
'InitialMagnification','fit');
title('Hough transform');
xlabel('\theta'), ylabel('\rho');
axis on, axis normal, hold on;
colormap(gca,hot);
%find the peaks in the hough transform using houghpeaks
P = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
x = theta(P(:,2));
y = rho(P(:,1));
subplot(1,4,3); hold on
plot(x,y,'s','color','black');
%Find lines in image using houghlines
lines = houghlines(BW,theta,rho,P,'FillGap',5,'MinLength',7);
subplot(1,4,4)
myImshow(IM,3), 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','red');
1 Comment
Answers (0)
Categories
Find more on Object Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!