Hi,
To create a grayscale gradient from the center to the edge of your circle, you'll adjust the pixel values based on their distance from the center. Instead of setting the inside of the circle to a uniform value (like 1 for inside and 0 for outside), you'll calculate a gradient value that decreases from the center to the edge.
You can refer to the following code snippet to achieve a grayscale gradient effect:
Z = zeros(99); % create square matrix of zeroes
origin = [round((size(Z,2)-1)/2+1), round((size(Z,1)-1)/2+1)]; % "center" of the matrix
radius = round(sqrt(numel(Z)/(2*pi))); % radius for a circle that fills half the area of the matrix
[xx,yy] = meshgrid((1:size(Z,2))-origin(1),(1:size(Z,1))-origin(2)); % create x and y grid
distanceFromCenter = sqrt(xx.^2 + yy.^2); % calculate distance from center for each point
% Instead of setting inside to 1, calculate gradient value
for i = 1:size(Z,1)
for j = 1:size(Z,2)
if distanceFromCenter(i,j) <= radius
Z(i, j) = 1 - (distanceFromCenter(i,j) / radius); % normalize and invert to get gradient
end
end
end
imshow(Z);
In above code, each pixel's value inside the circle is calculated based on its distance from the center, normalized by the radius. This means pixels at the center of the circle will have a value close to 1 (lighter) and decrease linearly to 0 (darker) as you move towards the edge of the circle, creating a nice gradient effect.
Hope it helps!