在MATLAB中,有没有办法用黑色填充图像而不是特定的圆圈?

时间:2013-04-14 20:45:34

标签: matlab image-processing

任何人都可以帮助我吗?我需要在MATLAB中制作一个用黑色填充的图像,而不是我以前的函数中的特定圆圈,这可能吗? 谢谢〜

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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;