!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]
答案 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;
输出 -
希望这会有所帮助,让我们知道它。有趣的项目!
编辑1:根据下面显示的已编辑照片,我收到了一些问题。
问题:你想要从PT1到MP的线路,让它在PT3上延伸并触摸外耳吗?如果是这样,你如何定义PT1?你如何区分PT1和PT2?您可能试图绘制从PT2到MP的一条线,并让它在其他位置触摸外耳,那么为什么PT2而不是PT1呢?