Crop a circle from an image and put it on another image

May you please help me how to crop (cut) a circle from an image and put it on another image?
I know that I can crop a circle from an image using following code:
I = imread('patricia.jpg');
imageSize = size(I);
ci = [250, 300, 100]; % center and radius of circle ([c_row, c_col, r])
[xx,yy] = ndgrid((1:imageSize(1))-ci(1),(1:imageSize(2))-ci(2));
mask = uint8((xx.^2 + yy.^2)<ci(3)^2);
croppedImage = uint8(zeros(size(I)));
croppedImage(:,:,1) = I(:,:,1).*mask;
croppedImage(:,:,2) = I(:,:,2).*mask;
croppedImage(:,:,3) = I(:,:,3).*mask;
imshow(croppedImage);

Answers (2)

jonas
jonas on 27 Oct 2018
Edited: jonas on 27 Oct 2018
You were almost there, and given the error message I think you could have figured it out :)
"Integers can only be combined with integers of the same class, or scalar doubles."
croppedImage(:,:,1) = I(:,:,1).*uint8(mask);
croppedImage(:,:,2) = I(:,:,2).*uint8(mask);
croppedImage(:,:,3) = I(:,:,3).*uint8(mask);
Here is a bonus method with inpolygon if you want to crop arbitrary shapes:
% Load image
RGB = imread('peppers.png');
%Convert to 2D to create mask
GRAY = rgb2gray(RGB);
imageSize = size(GRAY);
ci = [250, 300, 100]; % center and radius of circle ([c_row, c_col, r])
%Create circle
h = viscircles(ci(1:2),ci(3));
c = h.Children(1).XData(1:end-1);
r = h.Children(2).YData(1:end-1);
% Create grid for mask
[C,R] = meshgrid(1:imageSize(2),1:imageSize(1));
%Find pts inside of circle
mask = inpolygon(R,C,r,c);
%Crop
croppedImage = uint8(zeros(imageSize));
croppedImage(:,:,1) = RGB(:,:,1).*uint8(mask);
croppedImage(:,:,2) = RGB(:,:,2).*uint8(mask);
croppedImage(:,:,3) = RGB(:,:,3).*uint8(mask);
imshow(croppedImage);
Try this to copy a circular mask/roi region from image 1 and then paste it onto the same location of image 2:
% Read first image.
rgbImage1 = imread('peppers.png');
[rows, columns, numberOfColorChannels] = size(rgbImage1);
subplot(2, 2, 1);
imshow(rgbImage1);
axis('on', 'image');
title('rgbImage1', 'FontSize', 20);
% Get the second image - should be the same size as rgbImage1.
rgbImage2 = imread('coloredChips.png');
rgbImage2 = imresize(rgbImage2, [rows, columns]);
subplot(2, 2, 2);
imshow(rgbImage2);
axis('on', 'image');
title('rgbImage2', 'FontSize', 20);
% Make the circle mask.
imageSize = size(rgbImage1);
ci = [250, 300, 100]; % center and radius of circle ([c_row, c_col, r])
[xx,yy] = ndgrid((1:imageSize(1))-ci(1),(1:imageSize(2))-ci(2));
mask = (xx.^2 + yy.^2) < ci(3)^2;
subplot(2, 2, 3);
imshow(mask);
axis('on', 'image');
title('Mask', 'FontSize', 20);
% Get the individual color channels
% Extract the individual red, green, and blue color channels.
redChannel1 = rgbImage1(:, :, 1);
greenChannel1 = rgbImage1(:, :, 2);
blueChannel1 = rgbImage1(:, :, 3);
% For the second one, let's use the new imsplit().
% It's in R2018b. If you don't have it, use code like the above.
[redChannel2, greenChannel2, blueChannel2] = imsplit(rgbImage2);
% Paste the image from the circle region of rgbImage1 onto rgbImage2 in just the circle region.
redChannel2(mask) = redChannel1(mask);
greenChannel2(mask) = greenChannel1(mask);
blueChannel2(mask) = blueChannel1(mask);
% Recombine separate color channels into a single, true color RGB image.
rgbImage2 = cat(3, redChannel2, greenChannel2, blueChannel2);
% Display it.
subplot(2, 2, 4);
imshow(rgbImage2);
axis('on', 'image');
title('Image 1 pasted onto image 2 only within mask', 'FontSize', 20);

8 Comments

Thanks for this solution. I am trying to repurposing this code. The change I want to make is paste the masked regions on random locations of the same sized image and save it. Looking to generate 50 such images images( my mask radius is small, so its feasible). Thanks for the help!
You're welcome. I'm glad you were able to repurpose it for your needs. 🙂
@Image Analyst thanks!!!
Btw, how do I update the location of where the mask is placed? Thankyou!!
Change the values in the vector ci to change the size and location of the circle.
Oh, in your last announcement you didn't actually ask anything. I'm sure you can just make a loop to loop 50 times and place circles onto the mask, right? Try it, I have confidence in you. In case you just can't figure it out, I did it for you (depriving you of all the fun) for 10 circles at random locations:
% Demo by Image Analyst
% Initialization Steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
% Read first image.
rgbImage1 = imread('peppers.png');
[rows, columns, numberOfColorChannels] = size(rgbImage1);
subplot(2, 2, 1);
imshow(rgbImage1);
axis('on', 'image');
title('rgbImage1', 'FontSize', 20);
% Get the second image - should be the same size as rgbImage1.
rgbImage2 = imread('coloredChips.png');
rgbImage2 = imresize(rgbImage2, [rows, columns]);
subplot(2, 2, 2);
imshow(rgbImage2);
axis('on', 'image');
title('rgbImage2', 'FontSize', 20);
% Make a mask of all the circles
numCircles = 10; % Whatever you want.
mask = false(rows, columns);
radius = 30;
for k = 1 : numCircles
% Get the center.
xCenter = round(radius + (columns - 2 * radius) * rand(1));
yCenter = round(radius + (rows - 2 * radius) * rand(1));
% Make the circle mask.
[xx, yy] = ndgrid((1:rows)-yCenter, (1:columns)-xCenter);
thisMask = (xx.^2 + yy.^2) < radius ^ 2;
% Combine this mask with the master mask.
mask = mask | thisMask;
end
subplot(2, 2, 3);
imshow(mask);
axis('on', 'image');
caption = sprintf('Mask with %d circles', numCircles);
title(caption, 'FontSize', 20);
% Get the individual color channels
% Extract the individual red, green, and blue color channels.
redChannel1 = rgbImage1(:, :, 1);
greenChannel1 = rgbImage1(:, :, 2);
blueChannel1 = rgbImage1(:, :, 3);
% For the second one, let's use the new imsplit().
% It's in R2018b. If you don't have it, use code like the above.
[redChannel2, greenChannel2, blueChannel2] = imsplit(rgbImage2);
% Paste the image from the circle region of rgbImage1 onto rgbImage2 in just the circle region.
redChannel2(mask) = redChannel1(mask);
greenChannel2(mask) = greenChannel1(mask);
blueChannel2(mask) = blueChannel1(mask);
% Recombine separate color channels into a single, true color RGB image.
rgbImage2 = cat(3, redChannel2, greenChannel2, blueChannel2);
% Display it.
subplot(2, 2, 4);
imshow(rgbImage2);
axis('on', 'image');
title('Image 1 pasted onto image 2 only within mask', 'FontSize', 20);
@Image Analyst Perfect, thank you!!!
I have two scenarios.
Scenario one is what you have helped me with the above code.
The second scenario will be: Obtain one circular image from image one and paste that circle on a different location in image 2, instead of the same location.
The above code is pasting the circle image on the second image in the same location as the first one from which it has been extracted. Can I randomize the location in in the second image on which it gets pasted. I can then loop it to generate 50 different images with circles in different locations. Thanks!
Sure, try it! Just get a coordinate of one circle in the input and another coordinate in the output image. Just do one circle at a time instead of all 50 at once. Start a new question if you still need help.
Got it. Thanks for the tip, was able to do it. Very much appreciate the help.

Sign in to comment.

Categories

Asked:

on 27 Oct 2018

Commented:

on 18 Nov 2022

Community Treasure Hunt

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

Start Hunting!