繁体   English   中英

耳朵图像处理-在MATLAB中找到直线与曲线的交点

[英]Ear Image Processing - Finding the point of intersection of line and curve in MATLAB

1我有耳朵的Canny边缘输出...我用一条线(绿色)连接了最远的两个边界。 现在,我想从该线的中点到外边界(左侧)绘制法线。 我编写的代码可以帮助我绘制法线,但是我希望红线恰好与白色边界对齐。 我也希望相交点位于相交的点。 我还考虑了另一种方法,通过更改50到60像素(在代码中),红线穿过白色边界。 如果得到相同的交点,则可以轻松绘制所需长度的线。 我在互联网和Mathworks上找到了一些代码,但这是两行交叉的代码。...任何人都可以帮忙。

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 ;

在此处输入图片说明

![] [2]

这里拾取输入图像。

这是获取相交点并将其绘制的代码-

%% 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;

输出 -

在此处输入图片说明

希望这会有所帮助,并让我们知道。 有趣的项目!

编辑1:根据下面显示的编辑的照片,我有几个问题要问您。

在此处输入图片说明

问题:您是否需要一条从PT1到MP的线,并使其延伸并接触PT3的外耳? 如果是这样,您如何定义PT1? 您如何区分PT1和PT2? 您本可以尝试绘制一条从PT2到MP的线,并使其在其他点接触外耳,那么为什么不使用PT2而不是PT1?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM