How to create a circle with a gradient inside
11 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Image Analyst
2016-12-3
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 个评论
Image Analyst
2016-12-3
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.
更多回答(1 个)
Tamir Suliman
2016-12-3
...'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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!