Draw a gradient circle within a circle

I want to code a gradient circle within a circle like shown in the diagram. Any help will be appreciated.

 採用された回答

Matt J
Matt J 2021 年 6 月 19 日
編集済み: Matt J 2021 年 6 月 19 日

1 投票

One way might be to use a patch with vertex interpolation, as in this example.
EDIT:
Here's how you might apply that to a circle:
t=linspace(0,360,500); t(end)=[];
x=cosd(t);
y=sind(t);
c=cosd(t);
patch(x,y,c); colormap(gray); axis equal; caxis([-0.7970 ,0.6373])

15 件のコメント

Sajjad Ahmad Khan
Sajjad Ahmad Khan 2021 年 6 月 19 日
I haven't found answer from the shared link
Matt J
Matt J 2021 年 6 月 19 日
You should see an example there with a gradient transition across triangles, similar to what you are trying to do with circles.
Scott MacKenzie
Scott MacKenzie 2021 年 6 月 19 日
編集済み: Scott MacKenzie 2021 年 6 月 19 日
@Matt J I tried -- without success -- to extend this example to circles. I think it's tricky because a circle doesn't have faces and vertices. When I tried to concoct a circle as a many-sided polygon, the color interpolation seemed to go a bit wonky:
Matt J
Matt J 2021 年 6 月 19 日
編集済み: Matt J 2021 年 6 月 19 日
@Scott MacKenzie See my edited answer
Scott MacKenzie
Scott MacKenzie 2021 年 6 月 19 日
@Matt JWow, excellent. Thanks for this
Sajjad Ahmad Khan
Sajjad Ahmad Khan 2021 年 6 月 19 日
Hi Matt, I want to add the gradient circle that you plotted inside the white circle in the following code. How can I do that?
% First create the image.
imageSizeX = 256;
imageSizeY = 256;
image=zeros(imageSizeX,imageSizeY);
[columnsInImage rowsInImage] = meshgrid(1:imageSizeX, 1:imageSizeY);
% Next create the circle in the image.
centerX = 128;
centerY = 128;
radius = 100;
circlePixels = (rowsInImage - centerY).^2 ...
+ (columnsInImage - centerX).^2 <= radius.^2;
% circlePixels is a 2D "logical" array.
% Now, display it.
image(circlePixels)=256;
imshow(image, [])
Matt J
Matt J 2021 年 6 月 19 日
編集済み: Matt J 2021 年 6 月 19 日
Doing this with images is much easier than with plots...
gradientCircle=columnsInImage.*circlePixels;
gradientCircle(circlePixels)=rescale(gradientCircle(circlePixels),0,255);
imshow(gradientCircle, [])
Sajjad Ahmad Khan
Sajjad Ahmad Khan 2021 年 6 月 19 日
I actually want an image of zeros of some height and width in which I have two circles, the outer circle should be white and inner most circle should have a gradient (0 to 255). See the first image that I had posted in a question. Thanks
Matt J
Matt J 2021 年 6 月 19 日
編集済み: Matt J 2021 年 6 月 19 日
Well, it's very easy to add a circle around the first.
halo=imdilate(circlePixels,strel('disk',10)) & ~circlePixels;
gradientCircle(halo)=255;
imshow(gradientCircle, []);shg
Sajjad Ahmad Khan
Sajjad Ahmad Khan 2021 年 6 月 19 日
@Matt J Thanks for your reply. This function is really helpful but I want to code such that I could change the dimensions of image and inner circles. I want some like this shown image. How can I modify my above mentioned code to get something like this image? Thanks
Matt J
Matt J 2021 年 6 月 19 日
Well, the radius is a parameter you do control, isn't it? You set it to 100 in your original code, but you could easily change it to something else.
Sajjad Ahmad Khan
Sajjad Ahmad Khan 2021 年 6 月 20 日
@Matt J yes I can control the radius of outer circle but how to control the radius of inner circle that you coded? Thank you!
Matt J
Matt J 2021 年 6 月 20 日
The inner circle is the one that you coded. The outer circle is coded by choosing the strel radius in strel('disk',10)
Sajjad Ahmad Khan
Sajjad Ahmad Khan 2021 年 6 月 20 日
@Matt J Thank you so much. I appreciate your help!
Matt J
Matt J 2021 年 6 月 20 日
You're quite welcome, but please Accept-click the answer if you consider the question resolved.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeGeometric Transformation and Image Registration についてさらに検索

質問済み:

2021 年 6 月 19 日

コメント済み:

2021 年 6 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by