Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
耳图像处理——在MATLAB中求直线与曲线的交点_Matlab_Image Processing_Line_Intersection_Curve - Fatal编程技术网

耳图像处理——在MATLAB中求直线与曲线的交点

耳图像处理——在MATLAB中求直线与曲线的交点,matlab,image-processing,line,intersection,curve,Matlab,Image Processing,Line,Intersection,Curve,!!我有一只耳朵精明的边缘输出。。。我用一条线(绿色)连接了最远的两个边界。现在我想画一条从这条线的中点到外边界(左侧)的法线。 我编写的代码帮助我绘制法线,但我希望红线正好与白色边界相交。我还要求交点在它相交的地方。我还考虑了另一种方法,通过改变50到60像素(在代码中),红线穿过白色边界。如果我得到了相同的交点,那么我可以很容易地画出所需长度的线。我在网上和Mathworks上找到了一些代码,但它是用于两条线的交叉点…有人能帮我吗 for i=1:numel(p) x = [ p{i

!!我有一只耳朵精明的边缘输出。。。我用一条线(绿色)连接了最远的两个边界。现在我想画一条从这条线的中点到外边界(左侧)的法线。 我编写的代码帮助我绘制法线,但我希望红线正好与白色边界相交。我还要求交点在它相交的地方。我还考虑了另一种方法,通过改变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 ;


!![]从中拾取输入图像

这是获取交点并绘制它的代码-

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

从中拾取输入图像

这是获取交点并绘制它的代码-

%% 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之间的三角形。我试着用你之前写的代码来得到同样的结果,但我在某个地方出了问题。我在问题中添加了你给出的代码。我不知道出了什么问题…:(太好了!正是我想要的。谢谢!!请忽略我建议的编辑…我成功地完成了一切..谢谢!我有一个小问题。我用你的代码画了几行。我上传了问题中的问题图片。我试图延伸的蓝线穿过红线的中点,然后触到边界ary。扩展线是黄色的。我曾尝试使用您的代码实现它,但它没有正确对齐。是的,无论哪种方法都可以,但我只能坚持一种方法,因为我将基于相同的方法测试多个耳朵。Pt1扩展到MP并扩展到点3是我想要的,这样我就可以得到Pt1,p之间的三角形t2和pt3。我尝试使用您以前编写的代码来获得相同的结果,但我在某个地方出错了。我在问题中添加了您给出的问题中使用的代码。我不知道哪里出错了…:(请看这张:。可能它可以帮助您找到解决方案。请看这张:。可能它可以帮助您找到解决方案。