Ali P
Ali P

Reputation: 519

Getting a list of pixels coordinates from a circular or oddly shape blob - Matlab

I am new to image processing and I am trying to obtain a list of pixel coordinates that are found within a circular/oval/oddly shape blob.

The only way that I can think of doing it is using a bounding box but unfortunately the bounding box does go over the area.

Anyone has a better idea?

Thanks enter image description here

Upvotes: 0

Views: 833

Answers (1)

rayryeng
rayryeng

Reputation: 104555

Just use find to obtain the pixel coordinates. Assuming your image is binary and stored in im, do:

[r,c] = find(im);

r and c will be the rows and columns of every pixel that is white. This assumes that the object is fully closed - one caveat I'd like to mention. If there are holes in the interior of the object, consider using imfill to fill in these holes, then combine it with find:

bw = imfill(im, 'holes');
[r,c] = find(bw);

If you have more than one object, use regionprops and specify the PixelList attribute:

s = regionprops(im, 'PixelList');

This will return a N element structure where each structure contains a PixelList field that contains the (x,y) coordinates of each unique object. In your case, this will be a M x 2 matrix where the first column are the x or column coordinates and the second column are the y or row coordinates.

To access an object's pixel coordinate list, simply do:

coords = s(idx).PixelList;

idx is the object you want to access.

Upvotes: 2

Related Questions