2014-02-25 53 views
1

1我有一個耳朵的Canny邊緣輸出...我用線連接了最遠的兩個邊界(綠色)。現在我想從這條線的中點繪製一個法線到外邊界(左邊)。 我寫的代碼可以幫助我繪製一個法線,但是我想要紅線恰好符合白色邊界。另外我想要在交會點交點。我也想過另一種方法。通過改變50到60像素(在代碼中),紅線穿過白色邊界。如果我得到相同的交點,那麼我可以輕鬆地繪製所需長度的線。我在互聯網和Mathworks上發現了一些代碼,但它是用於2行的交叉點....任何人都可以幫忙。人耳圖像處理 - 在MATLAB中找到直線和曲線的交點

for i=1:numel(p) 
    x = [ p{i}(1), p{i}(3)]; 
    y = [p{i}(2), p{i}(4)]; 
    line(x,y,'color','g','LineWidth',2); 
    m = (diff(y)/diff(x)); 
    minv = -1/m; 
    line([mean(x) mean(x)-50],[mean(y) mean(y)-50*minv],'Color','red') 
    axis equal 

end ; 

enter image description here

![] [2]

+0

看看這個:。可能它可以幫助你找到解決方案。 –

回答

4

here撿起輸入圖像。

這是代碼來獲取交叉點並畫出它 -

%% Read image and convert to BW 
img1 = imread('ear.png'); 
BW = im2bw(img1); 

L = bwlabel(BW,8); 
[bw_rows,bw_cols] =find(L==1); 
bw_rowcol = [bw_rows bw_cols]; 
bw_rowcol(:,1) = size(BW,1) - bw_rowcol(:,1); % To offset for the MATLAB's terminology of showing height on graphs 

%% Get the farthest two points on the outer curve and midpoint of those points 
distmat = dist2s(bw_rowcol,bw_rowcol); 
[maxdist_val,maxdist_ind] = max(distmat(:),[],1); 
[R,C] = ind2sub(size(distmat),maxdist_ind); 

farther_pt1 = bw_rowcol(R,:); 
farther_pt2 = bw_rowcol(C,:); 
midpoint = round(mean([farther_pt1 ; farther_pt2])); 

%% Draw points on the normal from the midpoint across the image 
slope_farthest_pts = (farther_pt1(1) - farther_pt2(1))/(farther_pt1(2) - farther_pt2(2)); 
slope_normal = -1/slope_farthest_pts; 

y1 = midpoint(1); 
x1 = midpoint(2); 
c1 = y1 -slope_normal*x1; 

x_arr = [1:size(BW,2)]'; 
y_arr = slope_normal*x_arr + c1; 
yx_arr = round([y_arr x_arr]); 

%% Finally get the intersection point 
distmat2 = dist2s(bw_rowcol,yx_arr); 

[mindist_val2,mindist_ind2] = min(distmat2(:),[],1); 
[R2,C2] = ind2sub(size(distmat2),mindist_ind2); 

intersection_pt = bw_rowcol(R2,:); % Verify that this is equal to -> yx_arr(C2,:) 

%% Plot 
figure,imshow(img1) 

hold on 
x=[farther_pt1(2),farther_pt2(2)]; 
y=size(BW,1)-[farther_pt1(1),farther_pt2(1)]; 
plot(x,y) 

hold on 
x=[intersection_pt(2),midpoint(2)]; 
y=size(BW,1)-[intersection_pt(1),midpoint(1)]; 
plot(x,y,'r') 
text(x(1),y(1),strcat('Int Pt = ','[',num2str(x(1)),',',num2str(y(1)),']'),'Color','green','FontSize',24,'EdgeColor','red','LineWidth',3) 

不要忘記使用此相關功能 -

function out = dist2s(pt1,pt2) 

out = NaN(size(pt1,1),size(pt2,1)); 
for m = 1:size(pt1,1) 
    for n = 1:size(pt2,1) 
     if(m~=n) 
      out(m,n) = sqrt((pt1(m,1)-pt2(n,1)).^2 + (pt1(m,2)-pt2(n,2)).^2); 
     end 
    end 
end 

return; 

輸出 -

enter image description here

希望這有助於並讓我們知道它。有趣的項目!

編輯1:根據下面顯示的編輯照片,我幾乎沒有問題給你。

enter image description here

問題:你想從PT1一行MP和讓它延伸並PT3觸摸外耳?如果是這樣,你如何定義PT1?你如何區分PT1和PT2?你可能試圖畫出從PT2到MP的一條直線,讓它在其他點接觸外耳,爲什麼不用PT2代替PT1?

+0

完美!正是我想要的。謝謝!! – user3262170

+0

請忽略編輯我建議...我成功地完成了所有的事情..謝謝! – user3262170

+0

我有一個小問題。我用你的代碼畫了幾條線。我已經在我的問題中上傳了問題圖片。我試圖延伸穿過紅線中點然後觸摸邊界的藍線。延長線是黃色的。我嘗試過使用你的代碼來實現它,但它沒有得到正確的對齊。 – user3262170