【问题标题】:Ear Image Processing - Finding the point of intersection of line and curve in MATLAB耳朵图像处理 - 在 MATLAB 中查找直线和曲线的交点
【发布时间】:2014-02-25 09:11:40
【问题描述】:

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

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]

【问题讨论】:

  • 看看这个:stackoverflow.com/questions/16384516/…>。也许它可以帮助您找到解决方案。

标签: matlab image-processing line intersection curve


【解决方案1】:

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;

输出 -

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

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

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

【讨论】:

  • 完美!正是我想要的。谢谢!!
  • 请忽略我建议的编辑...我成功完成了一切...谢谢!
  • 我有小问题。我用你的代码画了几行。我已经在我的问题中上传了问题图片。我试图延伸的蓝线穿过红线的中点,然后触及边界。延长线是黄色的。我尝试使用您的代码实现它,但它没有正确对齐。
  • 是的,无论哪种方式都可以,但我必须坚持一种方法,因为我将基于相同的方法测试许多耳朵。 Pt1 延伸到 MP 并延伸到点 3 是我想要的,这样我就得到了 pt1、pt2 和 pt3 之间的三角形。我尝试使用您之前编写的代码来获得相同的结果,但我在某个地方出错了。
  • 我已在问题中添加了代码,该代码来自您给出的问题。我不知道出了什么问题... :(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多