How can I save each part (sector) of a circle in an individual image?
    10 views (last 30 days)
  
       Show older comments
    
    dziri halima
 on 23 Oct 2019
  
    
    
    
    
    Commented: Gustavo Liñán Cembrano
      
 on 16 Dec 2019
            
I create a circle in my image.  Then I subdivide the circle into 6 parts with this code.
x0=centroidsX;
y0=centroidsY;
r=10;
teta=-pi:0.01:pi;
x=r*cos(teta)+x0
y=r*sin(teta)+y0
plot(x,y)
hold on
scatter(x0,y0,'or')
axis square 
%----------------------------------------
% divide your circle to n sectors
n=6
tet=linspace(-pi,pi,n+1)
xi=r*cos(tet)+x0
yi=r*sin(tet)+y0
for k=1:numel(xi)
plot([x0 xi(k)],[y0 yi(k)],'black')
hold on
end
My need is to save each sector of the circle into an individual image.
0 Comments
Accepted Answer
  Gustavo Liñán Cembrano
      
 on 23 Oct 2019
        
      Edited: Gustavo Liñán Cembrano
      
 on 23 Oct 2019
  
      Hi, assuming your image variable is Im; I've created this small code, which provides a ThisSector image (B&W) and ThisSectorIDs (indexes to sector positions) which you can use to select part of your image to crop (or just multiply the image by the sector) to obtain black everywhere instead where the sector is one. Hope it helps. The imshow() etc. is just for you to see the produced sectors. The code allows you to interactively draw the circle around the area of interest. If you already have the circle defined, just comment the section where the circle is created interactively
[Nrows,Ncols,Ncolors]=size(Im);
hFig=imshow(Im);
%% Comment this section if x0,y0,r are already selected
hCircle=drawcircle;
x0=hCircle.Center(1);
y0=hCircle.Center(2);
r=hCircle.Radius;
%% if you already have x0,y0,r, then use
hCircle=drawcircle('Center',[x0,y0],'Radius',r);
%%
CircleMask=createMask(hCircle);
n=6;
tet=linspace(-pi,pi,n+1);
xi=r*cos(tet)+x0;
yi=r*sin(tet)+y0;
for k=1:numel(xi)-1
  if (k<0.5*(numel(xi)))
    CornerX1=xi(k);
    CornerY1=1;
    CornerX2=xi(k+1);
    CornerY2=1;
  else
    CornerX1=xi(k);
    CornerY1=Nrows;
    CornerX2=xi(k+1);
    CornerY2=Nrows;
  end
  hPoly=drawpolygon('Position',[x0 y0; xi(k) yi(k); CornerX1 CornerY1; CornerX2 CornerY2; xi(k+1) yi(k+1);x0 y0]);
  ThisMask=createMask(hPoly);
  ThisSector=(ThisMask & CircleMask);
  ThisSectorIDs=find(ThisSector);
  figure(k);
  imshow(ThisSector);
end
3 Comments
  Image Analyst
      
      
 on 14 Dec 2019
				Assuming your image is grayscale:
  ThisSector=(ThisMask & CircleMask);
  graySector = Im .* ThisSector;
  figure(k);
  imshow(graySector, []);
  axes('on', 'image');
  Gustavo Liñán Cembrano
      
 on 16 Dec 2019
				Add the following to mu previous Code Inside the for loop
ThisSectorIm=Im.*double(ThisSector); SectorName=strcat('Sector_',num2str(k),'.jpg'); imwrite(ThisSectorIm,SectorName);
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

