How to crop circle from an image?

58 views (last 30 days)
Hardik Poudel
Hardik Poudel on 19 Jul 2020
Commented: DGM on 11 Apr 2024
Here I have have defined a circle using viscircles. Now i want to get the image inside the circle. How can I do that? I have tried it with impolygon but I have to crop the image for all the image in folder and turns out its really slow. Is there any way to do that?
im = imread('flower.jpg');
imshow(im);
[x,y] = getpts;
j = viscircles([x(1),y(2)], sqrt((x(1)-x(2))^2+(y(1)-y(2))^2));

Accepted Answer

Image Analyst
Image Analyst on 19 Jul 2020
To process a sequence of images, see the code in the FAQ:
To crop the circle from the image, use drawcircle():
% Demo to have the user click and draw a circle over an image, then blacken outside the circle and crop out the circular portion into a new image.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing
fontSize = 15;
% Get image.
originalImage = imread('peppers.png');
[rows, columns, numberOfColorChannels] = size(originalImage)
subplot(2, 2, 1);
imshow(originalImage);
axis('on', 'image');
title('Original Image', 'FontSize', fontSize);
% Maximize the window to make it easier to draw.
g = gcf;
g.WindowState = 'maximized'
% Ask user to draw a circle:
uiwait(helpdlg('Please click and drag out a circle.'));
h.Radius = 0;
while h.Radius == 0
h = drawcircle('Color','k','FaceAlpha',0.4)
if h.Radius == 0
uiwait(helpdlg('You double-clicked. You need to single click, then drag, then single click again.'));
end
end
% Get coordinates of the circle.
angles = linspace(0, 2*pi, 10000);
x = cos(angles) * h.Radius + h.Center(1);
y = sin(angles) * h.Radius + h.Center(2);
% Show circle over image.
subplot(2, 2, 2);
imshow(originalImage);
axis('on', 'image');
hold on;
plot(x, y, 'r-', 'LineWidth', 2);
title('Original image with circle mask overlaid', 'FontSize', fontSize);
% Get a mask of the circle
mask = poly2mask(x, y, rows, columns);
subplot(2, 2, 3);
imshow(mask);
axis('on', 'image');
title('Circle Mask', 'FontSize', fontSize);
% Mask the image with the circle.
if numberOfColorChannels == 1
maskedImage = originalImage; % Initialize with the entire image.
maskedImage(~circleImage) = 0; % Zero image outside the circle mask.
else
% Mask the image.
maskedImage = bsxfun(@times, originalImage, cast(mask, class(originalImage)));
end
% Crop the image to the bounding box.
props = regionprops(mask, 'BoundingBox');
maskedImage = imcrop(maskedImage, props.BoundingBox);
% Display it in the lower right plot.
subplot(2, 2, 4);
imshow(maskedImage, []);
% Change imshow to image() if you don't have the Image Processing Toolbox.
title('Image masked with the circle', 'FontSize', fontSize);
fprintf('Done running %s.m ...\n', mfilename);
  9 Comments
Image Analyst
Image Analyst on 11 Apr 2024
First, why?
Second, what does that mean? Does that mean if you display the image with something like imshow() you want the background to be transparent, like to show your computer desktop and other applications where the background is?

Sign in to comment.

More Answers (2)

Thi Ng
Thi Ng on 27 Nov 2020
Hi,
Thanks for the solution. Just a general question, how would I cite this if I am using the code to do data analysis for a paper in academia?
Thanks,
Thi
  2 Comments
Image Analyst
Image Analyst on 27 Nov 2020
I'd just say that it came from the MATLAB Central Answers forum and give the link.

Sign in to comment.


Pauline Audurier
Pauline Audurier on 2 Mar 2023
Hi,
Thank you for hte code, it's working well.
I wonder if there is a way to chose the outside color of the mask.
For exemple, in my case, I'ld like the outside color in grey and not in black (my images are colored).
Thank you,
Pauline
  1 Comment
Image Analyst
Image Analyst on 3 Mar 2023
% Demo to have the user click and draw a circle over an image, then blacken outside the circle and crop out the circular portion into a new image.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing
fontSize = 15;
% Get image.
originalImage = imread('peppers.png');
[rows, columns, numberOfColorChannels] = size(originalImage)
subplot(2, 2, 1);
imshow(originalImage);
impixelinfo; % let user mouse around and see (x,y) and RGB value.
axis('on', 'image');
title('Original Image', 'FontSize', fontSize);
% Maximize the window to make it easier to draw.
g = gcf;
g.WindowState = 'maximized'
% Ask user to draw a circle:
uiwait(helpdlg('Please click and drag out a circle.'));
h.Radius = 0;
while h.Radius == 0
h = drawcircle('Color','k','FaceAlpha',0.4)
if h.Radius == 0
uiwait(helpdlg('You double-clicked. You need to single click, then drag, then single click again.'));
end
end
% Get coordinates of the circle.
angles = linspace(0, 2*pi, 10000);
x = cos(angles) * h.Radius + h.Center(1);
y = sin(angles) * h.Radius + h.Center(2);
% Show circle over image.
subplot(2, 2, 2);
imshow(originalImage);
axis('on', 'image');
hold on;
plot(x, y, 'r-', 'LineWidth', 2);
title('Original image with circle mask overlaid', 'FontSize', fontSize);
% Get a mask of the circle
mask = poly2mask(x, y, rows, columns);
subplot(2, 2, 3);
imshow(mask);
axis('on', 'image');
title('Circle Mask', 'FontSize', fontSize);
% Mask the image with the circle.
if numberOfColorChannels == 1
maskedImage = originalImage; % Initialize with the entire image.
maskedImage(~circleImage) = 150; % Zero image outside the circle mask.
else
% Mask the image.
darkGray = [100, 100, 100];
[r, g, b] = imsplit(originalImage);
r(~mask) = darkGray(1);
g(~mask) = darkGray(2);
b(~mask) = darkGray(3);
maskedImage = cat(3, r, g, b);
end
% Crop the image to the bounding box.
props = regionprops(mask, 'BoundingBox');
maskedImage = imcrop(maskedImage, props.BoundingBox);
% Display it in the lower right plot.
subplot(2, 2, 4);
imshow(maskedImage, []);
impixelinfo; % let user mouse around and see (x,y) and RGB value.
% Change imshow to image() if you don't have the Image Processing Toolbox.
title('Image masked with the circle', 'FontSize', fontSize);
fprintf('Done running %s.m ...\n', mfilename);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!