Humty
Humty

Reputation: 1361

Make a circle inside a matrix in Matlab

I want to make a circle inside a matrix. For example; Make a matrix of some dimension, let's say ones(200,200) and then select its circle's x and y co-ordinates and change the color of these selected pixels and then show the image using imshow(img). As showing in the picture. Is it possible?

enter image description here

OR

Can I change this ploting code to picture for using the circle functionality?

radius = 5; 
centerX = 20;
centerY = 30;
viscircles([centerX, centerY], radius);
axis square;

Upvotes: 0

Views: 4177

Answers (1)

Suever
Suever

Reputation: 65430

You can use meshgrid to create a grid of x and y coordinates and then use the equation of the circle to check whether each x/y pair is within the circle or not. This will yield a logical result which can be displayed as an image

[x,y] = meshgrid(1:200, 1:200);
isinside = (x - centerX).^2 + (y - centerY).^2 <= radius^2;

imshow(isinside);

If you simply want the outline of the circle, you can apply a convolution to the resulting binary mask to decrease it's size and then subtract out the circle to yield only the outline

shrunk = ~conv2(double(~isinside), ones(3), 'same');
outline = isinside - shrunk;

imshow(outline)

If you have the Image Processing Toolbox, you can use bwperim to yield the binary outline

outline = bwperim(isinside);
imshow(outline);

enter image description here

Update

If you want to change the colors shown above, you can either invert outline and isinside before displaying

isinside = ~isinside;
outline = ~outline;

imshow(isinside)
imshow(outline)

Or you can invert the colormap

imshow(isinside)
colormap(gca, flipud(gray))

enter image description here

Upvotes: 1

Related Questions