Hough transform: Difference between revisions
Content deleted Content added
m →{{header|C}}: trans tcl |
Added a solution for MATLAB |
||
Line 131: | Line 131: | ||
return EXIT_SUCCESS; |
return EXIT_SUCCESS; |
||
}</lang> |
}</lang> |
||
=={{header|MATLAB}}== |
|||
<lang MATLAB>function [rho,theta,houghSpace] = houghTransform(theImage,thetaSampleFrequency) |
|||
theImage = flipud(theImage); |
|||
[width,height] = size(theImage); |
|||
rhoLimit = norm([width height]); |
|||
rho = (-rhoLimit:1:rhoLimit); |
|||
theta = (0:thetaSampleFrequency:pi); |
|||
numThetas = numel(theta); |
|||
houghSpace = zeros(numel(rho),numThetas); |
|||
accumulator = zeros(numel(theImage),numThetas); |
|||
%Preallocate cosine and sine calculations to increase speed. In |
|||
%addition to precallculating sine and cosine we are also multiplying |
|||
%them by the proper pixel weights such that the rows will be indexed by |
|||
%the pixel number and the columns will be indexed by the thetas. |
|||
%Example: cosine(3,:) is 2*cosine(0 to pi) |
|||
% cosine(:,1) is (0 to width of image)*cosine(0) |
|||
cosine = (0:width-1)'*cos(theta); %Matrix Outerproduct |
|||
sine = (0:height-1)'*sin(theta); %Matrix Outerproduct |
|||
%Calculate the hough transform of each "edge" pixel and store that |
|||
%curve in the accumulator array. The columns of which are indexed by |
|||
%theta and the rows are an arbitrary index. |
|||
index = 1; |
|||
for x = (1:width) |
|||
for y = (1:height) |
|||
if(theImage(x,y)) |
|||
accumulator(index,:) = cosine(x,:) + sine(y,:); |
|||
index = index + 1; |
|||
end |
|||
end |
|||
end |
|||
%%During the accumulator preallocation there is enough memory allocated |
|||
%%should every single pixel happen to be an "edge" pixel. This little |
|||
%%line deallocates the unused memory. |
|||
if index < numel(theImage) |
|||
accumulator(index:end,:) = []; |
|||
end |
|||
for i = (1:numThetas) |
|||
houghSpace(:,i) = hist(accumulator(:,i),rho); |
|||
end |
|||
pcolor(theta,rho,houghSpace); |
|||
shading flat; |
|||
title('Hough Transform'); |
|||
xlabel('Theta (radians)'); |
|||
ylabel('Rho (pixels)'); |
|||
colormap('gray'); |
|||
end</lang> |
|||
Sample Usage: |
|||
<lang MATLAB>>> uiopen('C:\Documents and Settings\owner\Desktop\Chris\MATLAB\RosettaCode\180px-Pentagon.png',1) |
|||
>> houghTransform(cdata(:,:,1)<255,1/200);</lang> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |