Hough transform: Difference between revisions

Content deleted Content added
→‎{{header|MATLAB}}: Changed the code, made it more efficient.
Line 142:
<lang MATLAB>function [rho,theta,houghSpace] = houghTransform(theImage,thetaSampleFrequency)
 
%Define the hough space
theImage = flipud(theImage); %Because the "origin" of a picture is the top-left corner
if indextheImage <= numelflipud(theImage);
[width,height] = size(theImage);
Line 150 ⟶ 151:
numThetas = numel(theta);
houghSpace = zeros(numel(rho),numThetas);
accumulator = zeros(numel(theImage),numThetas);
%Find the "edge" pixels
[xIndicies,yIndicies] = find(theImage);
%Preallocate space for the accumulator array
numEdgePixels = numel(xIndicies);
accumulator = zeros(numel(theImage)numEdgePixels,numThetas);
%Preallocate cosine and sine calculations to increase speed. In
Line 163 ⟶ 169:
sine = (0:height-1)'*sin(theta); %Matrix Outerproduct
accumulator(index(1:numEdgePixels),:) = cosine(xxIndicies,:) + sine(yyIndicies,:);
%Calculate the hough transform of each "edge" pixel and store that
 
%curve in the accumulator array. The columns of which are indexed by
%thetaScan andover the rowsthetas areand anbin the arbitraryrhos 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);