2014-05-08 31 views
1

我用下面的代碼來從給定的25×25黑色&白圖像中提取行:hough爲什麼找不到線?

[H, theta, rho] = hough(image); 
peaks = houghpeaks(H, 20,'NHoodSize',[19 19]); 
lines = houghlines(image, theta, rho, peaks, 'FillGap', 1, 'MinLength', 3); 

我然後繪製所述給定圖像上所找到的線。結果是這樣的:

The lines found by my hough transform

我不能理解的是,爲什麼這個過程不會發現在圖像的左側邊界線,從頂部到底部(反之亦然)。取而代之的是找到了粉紅色的線條,我認爲在這個空間中有更少的證據(因爲它接觸的是較少的白色像素)。 有沒有人有直覺,爲什麼這可能是這種情況?

我試着改變參數一點點或添加一些填充圖像,但沒有任何工作到目前爲止。

編輯: 的要求原始圖像:

original image

+1

你是怎麼墊呢?邊緣複製或零填充?我的猜測是,它被直接放在邊緣,因此被忽略。零填充可以解決這個問題。 – Raab70

+0

使用image = padarray填充它(圖像,[3 3])。沒有幫助 – user1809923

+0

你能包含原始圖像,以便我們可以測試它嗎? – Raab70

回答

2

默認閾值太高太行沒有找到。由於你想找到水平線和垂直線,而不是角度,所以我也減小了尺寸,所以它們都會非常接近。另請注意,在頂部,我將邊緣設置爲零,在您張貼的圖像中,外側有204個薄邊框,這隻會消除邊框。這是我的腳本。

clc;clearvars;close all; 
im=imread('B5oOc.png'); 
im=rgb2gray(im); 
im(:,1:2)=0; 
im(1,:)=0; 
im(end,:)=0; 
im(:,end)=0; 
BW=edge(im,'canny'); 

[H, T, R] = hough(BW); 
P = houghpeaks(H, 20,'NHoodSize',[1 1],'threshold',ceil(0.3*max(H(:)))); 
lines = houghlines(BW, T, R, P, 'FillGap', 1, 'MinLength', 3); 

imshow(imadjust(mat2gray(H)),'XData',T,'YData',R,... 
     'InitialMagnification','fit'); 
title('Hough Transform of Image'); 
xlabel('\theta'), ylabel('\rho'); 
axis on, axis normal, hold on; 
colormap(hot); 

x = T(P(:,2)); 
y = R(P(:,1)); 
plot(x,y,'s','color','blue'); 

figure; 
imagesc(im);hold on;colormap gray; 
axis image; 
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'); 

輸出是這樣的: Hough transform result

+0

嗨!感謝您的回答。那麼,我也想要找到非水平/垂直線。另外,我不明白爲什麼在我的代碼中找到了粉紅色的線條,但沒有在右邊框處找到這條線。爲什麼會這樣做?低門檻不可能是原因?我也認爲使用* .png和25 * 25矩陣是有區別的。 – user1809923

+0

不幸的是,我無法重現第一個示例圖像的矩陣,但還有另一個具有類似行爲的矩陣。必須設定爲真的指數= [10 11 12 13 14 15 16 33 43 57 70 82 107 132 147 148 157 158 169 170 171 172 183 193 194 208 217 218 232 233 242 257 267 282 292 307 317 332 333 342 343 358 368 375 376 377 378 383 393 403 404 408 418 429 433 443 454 458 468 473 478 479 483 493 498 508 518 533 543 544 558 569 584] – user1809923