Alternate method for constructing circular region mask from center positions and radii

6 visualizzazioni (ultimi 30 giorni)
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 Commenti
Serg Zhelezniak
Serg Zhelezniak il 8 Feb 2018
No, this function uses exactly the same logic as I've described above just in a slightly different format:
mask = mask | hypot(xx - xc(ii), yy - yc(ii)) <= radii(ii);
It will have nearly identical computational time. I'm looking for some alternate means of doing this.
Chetna Jain
Chetna Jain il 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.

Accedi per commentare.

Risposte (1)

Guillaume
Guillaume il 8 Feb 2018
Modificato: Guillaume il 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);

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by