简体   繁体   中英

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

! 1 I have the Canny edge output of a ear... i have connected the farthest two boundaries with a line(green). Now I want to draw a normal from the midpoint of this line to the outer boundary(left side). The code i have written helps me to plot a normal but i want the red line to exactly meet the white boundary. Also I want the point of intersection at the point where it meets. I have also thought about another method for the same.By changing 50 to 60 pixels (in the code) the red line crosses the white boundary. If I get the point of intersection of the same then I can easily plot the line of the desired length. I found some code on the internet and Mathworks, but it is for intersection of 2 lines....Can anybody plz help.

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]

Picked up the input image from here .

This is the code to get the intersection point and plot it-

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

Don't forget to use this associated function-

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;

The output -

在此处输入图片说明

Hope this helps and let us know it goes. Interesting project!

EDIT 1: Based on the edited photo shown below, I got few questions for you.

在此处输入图片说明

Questions: Do you want a line from PT1 to MP and let it extend and touch the outer ear at PT3? If so, how do you define PT1? How do you differentiate between PT1 and PT2? You could have tried to plot a line from PT2 to MP and let it touch the outer ear at some other point, so why not PT2 instead of PT1?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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