How to create a circle with a gradient inside

13 Ansichten (letzte 30 Tage)
Steven Bruce
Steven Bruce am 3 Dez. 2016
Kommentiert: Image Analyst am 3 Dez. 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

Akzeptierte Antwort

Image Analyst
Image Analyst am 3 Dez. 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 Kommentare
Steven Bruce
Steven Bruce am 3 Dez. 2016
OK thanks. I'll accept that as the answer although I didn't want to use masks. Is there no way to do it manually using a for loop or something, plotting etc?
Image Analyst
Image Analyst am 3 Dez. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Tamir Suliman
Tamir Suliman am 3 Dez. 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
  1 Kommentar
Steven Bruce
Steven Bruce am 3 Dez. 2016
Wouldn't that just give you a different solid colour? I want it to go from dark to white like in the picture.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Image Processing Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by