-3

谁能帮我 ?我需要在 MATLAB 中制作一个充满黑色的图像,而不是我从以前的函数中获得的特定圆圈,这可能吗?谢谢~

4

2 回答 2

2

这是可以做到的:

%Center of the circle is (x,y) and radius is r.
x=250;
y=250;
r=50;


%this is your image
a=true(500,500);
for i=1:500
   for j=1:500
      if ((j-x)^2+(i-y)^2)<=r^2 %condition that (x,y) lies on the circle.
         a(i,j)=0;
      end
   end
end
imshow(a)

%%%%%%%%%%%%%%%%%% Alternate method %%%%%%%%%%%%%%%%%%%%%%
%vectorized way

x=250;
y=250;
r=50;


%this is your image
a=true(500,500);
[x_meshgrid,y_meshgrid]=meshgrid(1:500,1:500);
circle_test=(x_meshgrid-x).^2+(y_meshgrid-y).^2-r.^2;
b=circle_test<=0;
%desiredImage=yourImage.*b;
desiredImage=a.*b;
于 2013-04-14T22:32:56.813 回答
1

如果您只想渲染图像(之后不使用矩阵计算任何内容),您可以使用高级函数,例如rectangleviscircles在图像上绘制圆圈。在命令窗口中键入'doc rectangle'参数说明。例如:

N=50;
x=23; y=11;
rad = 13;

img = zeros(N);

figure(1)
image(im); colormap gray; axis xy; axis image
hold on; rectangle('Position', [x,y,rad,rad], 'Curvature', [1,1], 'facecolor', 'w', 'edgecolor', 'w'); hold off;
于 2013-04-14T21:56:13.553 回答