Create circular masks randomly
7 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I need to create few mesh. The steps are highlighted below.
Let I be the image. Draw an initial curve whose center (a,b) is the center of the image and the radius r, is the small one of the halves of the length and width of the image. Draw the semi-diameters of the circle for every center angle one degree and randomly select 20 points on the every semi diameter. Randomly select one point from the 20 points on every semi diameter. This gives us a set of 360 points. By this set of points we can create a circular mesh. Repeat step 3 20 times to get 20 mesh or 20 masks. I have implemented the above in my following code:
clc;
clear all;
close all;
I = imread('brain.jpg');
I = double(I(:,:,1));
imagesc(I,[0 255]); colormap(gray); axis on;
%getting the centre and radius of the circular masks
xcen = round(size(I,1)/2);
ycen = round(size(I,2)/2);
%choosing the smaller of the length or breadth of the image for the raidus
if size(I,1)>=size(I,2)
radius = round(size(I,2)./4);
else
radius = round(size(I,1)./4);
end
%constructing the angles 0 to 360 degree
angle = 1:1:360;
X = xcen.*ones(size(angle,2),20);
Y = ycen.*ones(size(angle,2),20);
%Generate the 20 points on each semi diameter for every angle
for i = 1 : size(angle,2)
k=1;
if (angle(i)>=1 & angle(i)<=89) | (angle(i)>=271 & angle(i)<=360)
for j = 0 : (radius.*cos((angle(i)*pi)./180)/19) :...
radius.*cos((angle(i)*pi)./180)
X(i,k) = X(i,k)+j; %generate the X coordinates
k=k+1;
end
elseif (angle(i)==90 | angle(i)==270 )
else
for j = 0 : (radius.*cos((angle(i)*pi)./180)/19) : ...
(radius.*cos((angle(i)*pi)./180))
X(i,k) = X(i,k)+j; %generate the X coordinates
k=k+1;
end
end
end
for i = 1 : size(angle,2)
k=1;
if (angle(i)>=1 & angle(i)<=179)
for j = 0 : (radius.*sin((angle(i)*pi)./180))/19 :...
radius.*sin((angle(i)*pi)./180)
Y(i,k) = Y(i,k)+j; %generate the Y coordinates
k=k+1;
end
elseif (angle(i)==180 | angle(i)==360)
else
for j = 0 : (radius.*sin((angle(i)*pi)./180))/19 :...
(radius.*sin((angle(i)*pi)./180))
Y(i,k) = Y(i,k)+j; %generate the Y coordinates
k=k+1;
end
end
end
Having got the set of 360x20, X and Y coordinates, I can randomly select any one of them and construct the masks/mesh individually. Hence I will get my 20 mesh/masks.However my code is very inefficient as I have used so many loops.
%In my code plotting the X and Y coordinates gives me the 20 circular mesh
plot(X,Y,'-r');
Is my code correct? Can it be made more efficient?
0 Commenti
Risposte (0)
Vedere anche
Categorie
Scopri di più su Geographic Plots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!