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 task is to find the pixels on the side of the pattern shown by the arrows

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?

up vote 1 down vote accepted

this is John BG jgb2012@sky.com

I found a way to detect the right hand side edge with MATLAB:

1.- acquiring image

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

enter image description here

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

enter image description here

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

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

enter image description here

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)

enter image description here

4.- Selecting right hand side edge only

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

    if sum(L1)>0
        nL1=find(L1>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

enter image description here

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

if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?

To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link

thanks in advance for time and attention

John BG

jgb2012@sky.com

If I interpret your arrows correctly:

This looks pretty straightforward: Just take every image row separately. Start from right until you hit a transition of value. Those positions are your border.

  • I will try and then get back.. – Prashant Jun 11 at 16:03

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.