How can I save each part (sector) of a circle in an individual image?
2 次查看(过去 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.
0 个评论
采纳的回答
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
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
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 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!