Finding the edge pixels on only one side of the object For my work on lung images, I get a binary image mostly resembling the pattern shown below. I need to find the pixels on the borders shown by yellow arrows. The edges on the other end are shown in pink color. 


the edge function on MATLAB highlights all the edges irrespective of the side.

edge_BW_portionR = edge(BW_portionR)


I also used distance transform in which the lower right corner was used as a reference , and with respect to that, I put some conditions, but in vain.

How can this be solved?
I found a way to detect the right hand side edge with MATLAB:

1.- acquiring image

A=imread('001.jpg');
figure(1);imshow(A)




A1=A(:,:,1);
[sz1 sz2]=size(A1)

figure(2);[P,h1]=imcontour(A1,1);


this detects the contour, but function imcontour produces an additional small amount of points completely unrelated to the contour, and quite far away

hold all;
plot(P(1,:),P(2,:),'r*')  % check




2.- quik-fix for imcontour producing a few points far away.

Px=P(1,:);Py=P(2,:);
nx_out=find(Px&gt;sz2)
P(:,nx_out)=[]
ny_out=find(Py&gt;sz1)
P(:,ny_out)=[]
hold all;
figure(3);plot(P(1,:),P(2,:),'r*');axis ij  % check




3.- generating image with the complete contour only and 1 pixel thick contour only

Px=P(1,:);Py=P(2,:);
A2=zeros(sz1,sz2)
P=floor(P)
for k=1:1:numel(Px)
    A2(Py(k),Px(k))=255;
end
figure(4);imshow(A2)




4.- Selecting right hand side edge only

P2=[0;0]
for k=sz1:-1:1
    L1=A2(k,:)

    if sum(L1)&gt;0
        nL1=find(L1&gt;0)
        P2=[P2 [k;nL1(end)]];
    end
end
P2(:,1)=[]

figure(5);
imshow(A2);
hold all;

for k=1:1:length(P2)
    plot(P2(2,k),P2(1,k),'r*')  % check
end




The result can be improved refining the way pixels are selected, instead of

P2=[P2 [k;nL1(end)]];


using

P2=[P2 [k;nL1(2:end)]];


but this is assuming that the excluded left edge is a fairly straight up line, that may not be the case.

For more complicated shapes, to first find the single pixel thick contour
 have a look at my answer: https://uk.mathworks.com/matlabcentral/answers/350136-how-t-oget-straight-edge-in-binary-image?s_tid=srchtitle