How can I save each part (sector) of a circle in an individual image?

조회 수: 4 (최근 30일)
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.

채택된 답변

Gustavo Liñán Cembrano
Gustavo Liñán Cembrano 2019년 10월 23일
편집: Gustavo Liñán Cembrano 2019년 10월 23일
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
Image Analyst
Image Analyst 2019년 12월 14일
Assuming your image is grayscale:
ThisSector=(ThisMask & CircleMask);
graySector = Im .* ThisSector;
figure(k);
imshow(graySector, []);
axes('on', 'image');
Gustavo Liñán Cembrano
Gustavo Liñán Cembrano 2019년 12월 16일
Add the following to mu previous Code Inside the for loop
ThisSectorIm=Im.*double(ThisSector); SectorName=strcat('Sector_',num2str(k),'.jpg'); imwrite(ThisSectorIm,SectorName);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

태그

Community Treasure Hunt

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

Start Hunting!

Translated by