2012-04-09 87 views
4

我試圖讓Hough變換在MATLAB中工作,但我遇到了問題。我有一個非常糟糕的方法來檢測需要修復的峯,但在此之前,我需要能夠逆轉hough變換以正確再次創建線。這是東西我得到現在的類型:行hough變換

enter image description here

看起來像90度旋轉它,但我不知道爲什麼。我不確定這是否是我的霍夫空間是錯誤的,或者如果我是這樣抽取線條的方式。也有人可以幫助提高我的峯值檢測?

在代碼中使用的圖像是here

謝謝

%% load a sample image; convert to grayscale; convert to binary 

%create 'x' image (works well) 
a = eye(255); 
b = flipud(eye(255)); 
x = a + b; 
x(128,128) = 1; 

%image = rgb2gray(imread('up.png')) < 255; 
%image = rgb2gray(imread('hexagon.png')) < 255; 
%image = rgb2gray(imread('traingle.png')) < 255; 
%%% these work 
%image = x; 
%image = a; 
image = b; 

%% set up variables for hough transform 
theta_sample_frequency = 0.01;            
[x, y] = size(image); 
rho_limit = norm([x y]);             
rho = (-rho_limit:1:rho_limit); 
theta = (0:theta_sample_frequency:pi); 
num_thetas = numel(theta); 
num_rhos = numel(rho); 
hough_space = zeros(num_rhos, num_thetas); 

%% perform hough transform 
for xi = 1:x 
    for yj = 1:y 
     if image(xi, yj) == 1 
      for theta_index = 1:num_thetas 
       th = theta(theta_index); 
       r = xi * cos(th) + yj * sin(th); 
       rho_index = round(r + num_rhos/2);      
       hough_space(rho_index, theta_index) = ... 
        hough_space(rho_index, theta_index) + 1; 
      end 
     end 
    end 
end 


%% show hough transform 
subplot(1,2,1); 
imagesc(theta, rho, hough_space); 
title('Hough Transform'); 
xlabel('Theta (radians)'); 
ylabel('Rho (pixels)'); 
colormap('gray'); 

%% detect peaks in hough transform 
r = []; 
c = []; 
[max_in_col, row_number] = max(hough_space); 
[rows, cols] = size(image); 
difference = 25; 
thresh = max(max(hough_space)) - difference; 
for i = 1:size(max_in_col, 2) 
    if max_in_col(i) > thresh 
     c(end + 1) = i; 
     r(end + 1) = row_number(i); 
    end 
end 

%% plot all the detected peaks on hough transform image 
hold on; 
plot(theta(c), rho(r),'rx'); 
hold off; 


%% plot the detected line superimposed on the original image 
subplot(1,2,2) 
imagesc(image); 
colormap(gray); 
hold on; 

for i = 1:size(c,2) 
    th = theta(c(i)); 
    rh = rho(r(i)); 
    m = -(cos(th)/sin(th)); 
    b = rh/sin(th); 
    x = 1:cols; 
    plot(x, m*x+b); 
    hold on; 
end 

十字發佈: https://dsp.stackexchange.com/questions/1958/help-understanding-hough-transform

回答

5

如果再生圖像看起來90度或以其他方式翻轉旋轉,這可能是因爲繪圖不像您期望的那樣發生。您可以嘗試axis ij;移動圖的原點和/或可以扭轉你的繪圖命令:

plot(m*x+b, x); 

至於峯值檢測,你可能想看看imregionalmax