Clear Filters
Clear Filters

Create an image (pixels) circle with slices (not from the center) and paint each slice a different color

2 views (last 30 days)
I am trying to create an image of a circle and divide the circle into known parts (start from the shell of the circle) and give each part (slice) a different color (or gray level).
I am having difficulty how to restrict each slice between two lines and the relevant arc of the circle that completes the slice.
please see the attached picture to understand what i mean.(sorry that the painting wasnot complete - just to help understand the question)
please refer only to the slices in red (ignore the blue one)
thank you very much for your time and help!.
here is the code i found to create the circle:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Create a logical image of a circle with specified
% diameter, center, and image size.
% First create the image.
imageSizeX = 640;
imageSizeY = 480;
[columnsInImage rowsInImage] = meshgrid(1:imageSizeX, 1:imageSizeY);
% Next create the circle in the image.
centerX = 320;
centerY = 240;
radius = 200;
circlePixels = (rowsInImage - centerY).^2 ...
+ (columnsInImage - centerX).^2 <= radius.^2;
% circlePixels is a 2D "logical" array.
% Now, display it.
%subplot(2, 1, 1);
imshow(circlePixels) ;
colormap([0 0 0; 1 1 1]);
title('Binary image of a circle', 'FontSize', fontSize);
new_im = im2double(circlePixels);

Answers (2)

Bjorn Gustavsson
Bjorn Gustavsson on 13 Aug 2021
(The background image looked rather nice if you ask me...) If you want to find those wedge-shaped regions I think inpolygon would be the easiest function to use. Perhaps something like this:
theta360 = linspace(0,1,3601)*2*pi;
idx18 = 1:200:3601;
theta18 = theta360(idx18);
R = 200;
[x,y] = meshgrid(-200:200);
Im = zeros(401);
for iSeg = 1:16,
rEdge = [r0;r_circ(idx18(iSeg+2):idx18(iSeg+3),:)];
inP = inpolygon(x(:),y(:),rEdge(:,1),rEdge(:,2));
Im(inP) = iSeg;
You might want to modify the angular resolution (in theta360), the number of points along the circle (idx18) and the values you assign to the pixels inside, as well as the image resolution etc.

Sign in to comment.

darova on 15 Aug 2021
Here is an example with using matrix
r0 = 100; % radius of a circle
t = linspace(0, 2*pi, 15)+pi/2; % 15 points on a circle
[x,y] = pol2cart(t,r0); % cartesian 15 points
[X,Y] = meshgrid(-20-r0:r0+20); % mesh for an image
ii = 14;
[T,R] = cart2pol(X-x(ii),Y-y(ii)); % 15 segments in polar
t1 = cart2pol(x-x(ii),y-y(ii)); % angles of each segment
I = X*0+1;
for i = [1:length(t1)-1]
cond = t1(i) < T & T < t1(i+1); % choose segment
I = I + cond*i;
cond = X.^2 + Y.^2 < r0^2;
I = I.*cond; % assing zero value outside circle
colormap gray
axis equal

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!