How to create a circle with a gradient inside

조회 수: 11 (최근 30일)
Steven Bruce
Steven Bruce 2016년 12월 3일
댓글: Image Analyst 2016년 12월 3일
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

채택된 답변

Image Analyst
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
Steven Bruce
Steven Bruce 2016년 12월 3일
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 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
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
  댓글 수: 1
Steven Bruce
Steven Bruce 2016년 12월 3일
Wouldn't that just give you a different solid colour? I want it to go from dark to white like in the picture.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by