How can I save each part (sector) of a circle in an individual image?
1 回表示 (過去 30 日間)
古いコメントを表示
dziri halima
2019 年 10 月 23 日
コメント済み: Gustavo Liñán Cembrano
2019 年 12 月 16 日
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/244186/image.jpeg)
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!