5

我想要檢測圖像中的彎曲傳送帶。我用下面的代碼中使用Hough變換來檢測其邊緣如何檢測matlab中的光滑曲線

%# load image, and process it 
I = imread('ggp\2.jpg'); 
g = rgb2gray(I); 
bw = edge(g,'Canny'); 

[H,T,R] = hough(bw); 

P = houghpeaks(H,500,'threshold',ceil(0.4*max(H(:)))); 

% I apply houghlines on the grayscale picture, otherwise it doesn't detect 
% the straight lines shown in the picture 
lines = houghlines(g,T,R,P,'FillGap',5,'MinLength',50); 
figure, imshow(g), hold on 

for k = 1:length(lines) 

    xy = [lines(k).point1; lines(k).point2]; 

    deltaY = xy(2,2) - xy(1,2); 
    deltaX = xy(2,1) - xy(1,1); 
    angle = atan2(deltaY, deltaX) * 180/pi; 
    if (angle == 0) 

     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');   
    end 
end 

enter image description here

正如所示,兩條直線成功地檢測傳送帶的頂部和底部邊緣,但我不知道如何檢測如果彎曲或不彎曲(圖片中彎曲)以及如何計算其程度。

曲線大致是手動繪製在下面的圖片(紅色):

enter image description here

我發現霍夫沒有代碼或功能在MATLAB變換來檢測這樣的平滑的曲線(例如,第二次多項式:y= a*x^2)。任何其他解決方案也是可喜的。

這是原始圖像: enter image description here

+0

你能否給我們提供原始圖片? – Tapio

+0

@Tapio我在帖子中添加了圖片。 – Ahmad

+1

我不明白你想要檢測圖像中的曲線。您可以手動繪製圖像並突出顯示您想要查找的曲線嗎? – Shai

回答

3

看你的直線檢測傳送帶,我想你可以關注感興趣的區域周圍的處理(行圖像中的750至950)。從該點
論文集:圍繞的區域的中心

oimg = imread('http://i.stack.imgur.com/xfXUS.jpg'); %// read the image 
gimg = im2double(rgb2gray(oimg(751:950, :, :))); %// convert to gray, only the relevant part 
fimg = imfilter(gimg, [ones(7,50);zeros(1,50);-ones(7,50)]); %// find horizontal edge 

僅選擇強水平邊緣像素

[row, col] = find(abs(fimg)>50); 
sel = row>50 & row < 150 & col > 750 & col < 3250; 
row=row(sel); 
col=col(sel); 

安裝一個第二度多項式和線對這些邊緣點

[P, S, mu] = polyfit(col,row,2); 
[L, lS, lmu] = polyfit(col, row, 1); 

繪製估計曲線

xx=1:4000; 
figure;imshow(oimg,'border','tight'); 
hold on; 
plot(xx,polyval(P,xx,[],mu)+750,'LineWidth',1.5,'Color','r'); 
plot(xx,polyval(L,xx,[],lmu)+750,':g', 'LineWidth', 1.5); 

結果是

enter image description here

您可以直觀地體會到第二高度契合P如何更好地適應輸送帶的邊界。綜觀第一系數

>> P(1) 
ans = 
1.4574 

你看到的曲線x^2係數是不能忽略的使曲線明顯不在一條直線上。

+1

非常感謝!它的工作原理,如果我沒有收到其他答案,我會接受你的。 – Ahmad

+0

您使用了'imfilter'與15 x 50矩陣,它與'prewitt' 3 x 3矩陣類似。你能說說爲什麼我不能在這種情況下使用'fspecial('prewitt')'進行水平邊緣檢測嗎?但是,正如我測試過的那樣,它不起作用。 – Ahmad

+0

15乘50不是3乘3。濾鏡的基本原理是相同的:檢測水平邊緣,但3乘3會得到非常局部的結果,這些結果會受到圖像中小邊緣的影響。爲了僅檢測非常明顯的和大的水平邊緣,濾波器平滑了50個像素寬的區域以清楚地檢測邊緣。您可以嘗試將濾鏡大小更改爲5 x 50或5 x 100,並查看它會如何影響檢測的準確性。 – Shai