Hough transform MATLAB - пользовательская реализация - PullRequest
2 голосов
/ 28 ноября 2011

Я пытаюсь реализовать грубое преобразование для строк в Matlab. И я боролся с этим в течение нескольких дней, я не знаю, почему мой код работает не так, как должен. Да, это часть домашней работы, но, пожалуйста, помогите мне, потому что я полностью сдался.

Входные параметры: Т.е. - логическое изображение «ребра» (0 означает не ребро, 1 означает ребро).

function [out_ro, out_theta]=houghTransform(Ie,nBinsRo,nBinsTheta,tresh)

A = zeros(nBinsRo, nBinsTheta);

theta = 1:nBinsTheta;

theta = scale(theta, nBinsTheta, 1, (pi / 2), - (pi / 2));

D = size(diag(Ie));

D = D(1);

ro = 1:nBinsRo;

ro = scale(ro, nBinsRo, 1, D, -D);

len = size(Ie);

%checks all edges

for i = 1:len(1)

for j = 1:len(2)

    %if it is an edge

    if ((Ie(i,j) == 1))

        %generate all functions x cos(theta) + y sin(theta) = ro
        for m=1:nBinsTheta

            roVal = i * cos(theta(m)) + j * sin(theta(m));

            idx = scale2(roVal, D, -D, nBinsRo, 1);

            if (idx > 0 && idx < nBinsRo)

                A(idx, m) = A(idx, m) + 1;

            end

        end

    end

end

end

figure(1);
clf;
imagesc(A)


% -------------------------------------------------- %

function idx = scale(val, max_val_in, min_val_in, max_val_out, min_val_out)

skalirni_faktor = (max_val_out - min_val_out) / (max_val_in - min_val_in) ;

idx = min_val_out + (val-min_val_in) .* skalirni_faktor;

% -------------------------------------------------- %

function idx = scale2(val, max_val_in, min_val_in, max_val_out, min_val_out)

skalirni_faktor = (max_val_out - min_val_out) / (max_val_in - min_val_in) ;

idx = min_val_out + round((val-min_val_in) .* skalirni_faktor);

Большое спасибо за ваше время и ответы.

Ответы [ 2 ]

2 голосов
/ 29 ноября 2011

Я не смог найти, что случилось с кодом, но я предполагаю, что масштабирование проблематично.

Если кто-нибудь найдет это, вот другая реализация грубого преобразования.

function [rho,theta,houghSpace] = houghTransform(theImage,thetaSampleFrequency)

%#Define the hough space
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);

%#Find the "edge" pixels
[xIndicies,yIndicies] = find(theImage);

%#Preallocate space for the accumulator array
numEdgePixels = numel(xIndicies);
accumulator = zeros(numEdgePixels,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

accumulator((1:numEdgePixels),:) = cosine(xIndicies,:) + sine(yIndicies,:);

%#Scan over the thetas and bin the rhos 
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
1 голос
/ 24 декабря 2015

Hough Наивный Внедрение

function [ Hough, theta_range, rho_range ] = naiveHough(I)
%NAIVEHOUGH Peforms the Hough transform in a straightforward way.
%   
    [rows, cols] = size(I);

    theta_maximum = 90;
    rho_maximum = floor(sqrt(rows^2 + cols^2)) - 1;
    theta_range = -theta_maximum:theta_maximum - 1;
    rho_range = -rho_maximum:rho_maximum;

    Hough = zeros(length(rho_range), length(theta_range));

    wb = waitbar(0, 'Naive Hough Transform');

    for row = 1:rows
        waitbar(row/rows, wb);
        for col = 1:cols
            if I(row, col) > 0
                x = col - 1;
                y = row - 1;
                for theta = theta_range
                    rho = round((x * cosd(theta)) + (y * sind(theta)));                   
                    rho_index = rho + rho_maximum + 1;
                    theta_index = theta + theta_maximum + 1;
                    Hough(rho_index, theta_index) = Hough(rho_index, theta_index) + 1;
                end
            end
        end
    end

    close(wb);

end

Если хотите, у меня есть суть Наивная реализация Hough тоже.

...