How to create a circle with a gradient inside
12 views (last 30 days)
Show older comments
Steven Bruce
on 3 Dec 2016
Commented: Image Analyst
on 3 Dec 2016
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
0 Comments
Accepted Answer
Image Analyst
on 3 Dec 2016
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');
2 Comments
Image Analyst
on 3 Dec 2016
I guess you could loop over columns in circlePixels and find the first and last white pixel.
for col = 1 : imageSizeX
thisColumn = circlePixels(:, col);
row1 = find(thisColumn, 1, 'first');
row2 = find(thisColumn, 1, 'last');
finalImage(row1:row2, col) = col; % Or whatever gray level you want.
end
though I don't know why you'd want to abandon one of the nicest features of MATLAB - using vectorized operations.
More Answers (1)
Tamir Suliman
on 3 Dec 2016
...'Color', [1 4 1]..
instead of using color white find the the rgb equivalent and use the option above with insertShape
where [1 4 1] is your RGB color matrix
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!