MATLAB Answers

How can I find pixel coordinates of a perimeter in a binary image?

92 views (last 30 days)
Milad
Milad on 1 Apr 2014
Commented: Cam S on 18 Jul 2019
Hi
I have a binary image which I have extracted the perimeter with bwperim() function. Now that I have the perimeter image I am trying to extract the coordinates of pixels in this perimeter.Here are the images:
The white perimeter illustrated in the second image is where I want to find the coordinates. Any Suggestions is appreciated.
Thanks

  3 Comments

mahesh pawar
mahesh pawar on 3 Mar 2017
Per = bwperim(img);
[r,c]=size(Per);
p=1;
z = nnz(Per);
cord = zeros(z,2);
for i=1:r
    for j=1:c
        if (Per(r,c)) == 1
             cord(p,1)=i;
             cord(p,2)=j;
             p=p+1;
               p=p+1;
        end
    end
end
Image Analyst
Image Analyst on 3 Mar 2017
mahesh, put this as an "Answer" below, not a comment up here. Also your entire code could be done in the single line
[rows, columns] = find(bwperim(img));
which is about the same at Nitin's accepted answer. However, neither that, nor your code will give the perimeter in a contiguous, connected way, like going clockwise around the region. It will give them on a column by column disconnected basis.

Sign in to comment.

Accepted Answer

More Answers (2)

Dishant Arora
Dishant Arora on 1 Apr 2014
If you have a single blob the above answer would suffice but in case you have multiple objects use bwboundaries. It gives you the coordinates of boundary pixels.
doc bwboundaries

  3 Comments

Milad
Milad on 1 Apr 2014
Thanks Dishant. At this time I am dealing with only one region(or blob as you called it:actually this is a converted aggregate image) per image but I might need to work with bwboundaries() too, as I may deal with multi-regions in the future. For this problem I couldn't get the matrix of pixel positions by his function. I used this code:
[x,y] = bwboundaries(bw,'noholes');
where,bw is the input image(second image illustrated in the main Question).which returns this result:
[471x2 double] [360x480 double]
in which the right one is the resolution of the input picture and I guess "double" is referred to it's class. I am not sure about the left one because it does not have the same dimension as the matrix derived from find() function. Any ideas?
Dishant Arora
Dishant Arora on 1 Apr 2014
First one is the co-ordinates of boundary pixels and second is a labelled image. What output do you get from find()?? Post your code.
Image Analyst
Image Analyst on 1 Apr 2014
I agree with Dishant that bwboundaries is what you want , not bwperim followed by find. The difference in sizes between find and bwperim is probably due to bwboundaries "cutting corners" as it makes a 45 degree path while perim will give you all the rows and columns of the "L". Also find probably does not "follow the boundary" like bwboundaries does. With find you are not gauranteed that the pixel at the kth index is next to the pixel at the (k+1)st index since find probably goes in column major order like most things in MATLAB.

Sign in to comment.


Arriana Nwodu
Arriana Nwodu on 13 Aug 2018
So which code is correct? I tried all the ways listed and it didn't work

  0 Comments

Sign in to comment.

Sign in to answer this question.