Alternate method for constructing circular region mask from center positions and radii
6 views (last 30 days)
Show older comments
I would like to be able to construct a mask for a given image full of different circular ROIs. I know the x,y coordinates for the centers and the radius length for each ROI. I know that the following expression can be utilized:
temp = ((xgrid-data(i,1)).^2 + (ygrid-data(i,2)).^2) <= (radius(i)).^2;
BW = BW+temp;
which will iterate through all ROIs and combine them into one full mask. I do not want to proceed in this particular way. I am wondering if a faster approach exists such that given the x,y coordinates and the radius for say 100 ROIs, can I instantaneously create the mask instead of iterating through each individual ROI?
3 Comments
Chetna Jain
on 8 Feb 2018
Can you share data file so that I can try computing and verify the difference in computational time.Also I can try finding out other options.
Answers (1)
Guillaume
on 8 Feb 2018
Edited: Guillaume
on 8 Feb 2018
%random matrix of centre and radius coordinates for demo:
xyr = [randi(150, 10, 2) + 50, randi(20, 10, 1)];
masksize = [200, 200];
[x, y] = meshgrid(1:masksize(2), 1:masksize(1)); %create pixel coordinates
xyr = permute(xyr, [3 2 1]); %move each circle along 3rd dimension
mask = any(hypot(x - xyr(1, 1, :), y - xyr(1, 2, :)) <= xyr(1, 3, :), 3); %require R2016b or later
imshow(mask)
If you're on a version earlier than R2016b (or don't like implicit expansion):
mask = any(bsxfun(@le, hypot(bsxfun(@minus, x, xyr(1, 1, :)), bsxfun(@minus, y, xyr(1, 2, :))), xyr(1, 3, :)), 3);
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!