MATLAB: How to create a circle with a gradient inside

circlefaqfillgradientpolygon

Hi
I can create a white circle on a black background but how do I create a white circle with a gradient like in the image below? Here is the code I have so far:
if true
% code
width = 200;
I = zeros(width);
radius = 80;
centre = width/2;
final_image = insertShape(I,'FilledCircle',[centre,centre,radius],'Color', 'white', 'Opacity', 1.0);
imshow(final_image);
end
Steve

Best Answer

Here is the code:
% Initialization / clean-up code.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
% clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
% fontSize = 20;
% Create a gradient ramp along the x axis
rampImage = uint8(repmat([0:255], [256, 1]));
% Now, display it.


subplot(2,2, 1);
imshow(rampImage) ;
axis on;
title('Ramp Image');
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Create a logical image of a circle with specified
% diameter, center, and image size.
% First create the image.
imageSizeX = 256;
imageSizeY = 256;
[columnsInImage rowsInImage] = meshgrid(1:imageSizeX, 1:imageSizeY);
% Next create the circle in the image.
centerX = 128;
centerY = 128;
radius = 110;
circlePixels = (rowsInImage - centerY).^2 ...
+ (columnsInImage - centerX).^2 <= radius.^2;
% circlePixels is a 2D "logical" array.
% Now, display it.
subplot(2,2, 2);
imshow(circlePixels, []);
axis on;
title('Binary image of a circle');
% Apply the circle mask to the ramp image.
finalImage = rampImage; % Initialize;
finalImage(~circlePixels) = 0; % Mask
% Now, display it.
subplot(2,2, 3);
imshow(finalImage, []);
axis on;
title('Masked Image');