How to crop circle from an image?
57 次查看(过去 30 天)
显示 更早的评论
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));
0 个评论
采纳的回答
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
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
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
2020-11-27
I'd just say that it came from the MATLAB Central Answers forum and give the link.
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
2023-3-3
@Pauline Audurier, try this:
% 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 Exchange 中查找有关 3-D Volumetric Image Processing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!