How to crop circle from an image?

58 ビュー (過去 30 日間)
Hardik Poudel
Hardik Poudel 2020 年 7 月 19 日
コメント済み: DGM 2024 年 4 月 11 日
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));

採用された回答

Image Analyst
Image Analyst 2020 年 7 月 19 日
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 件のコメント
Image Analyst
Image Analyst 2024 年 4 月 11 日
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?

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

その他の回答 (2 件)

Thi Ng
Thi Ng 2020 年 11 月 27 日
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 件のコメント
Image Analyst
Image Analyst 2020 年 11 月 27 日
I'd just say that it came from the MATLAB Central Answers forum and give the link.
Thi Ng
Thi Ng 2020 年 11 月 27 日
Thanks!

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


Pauline Audurier
Pauline Audurier 2023 年 3 月 2 日
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 件のコメント
Image Analyst
Image Analyst 2023 年 3 月 3 日
% 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);

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

カテゴリ

Help Center および File Exchange3-D Volumetric Image Processing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by