How do I copy pixels from one image onto another image?

16 views (last 30 days)
I'm new to MATLAB and have been trying to figure out how to copy a picture of myself onto another image. I've written code that allows me to drag my mouse to select pixels of an image, and then I can copy what I've selected in a matrix form, but I don't know what to do next or even if I'm going about this the right way. Any help would be greatly appreciated!!!
Also, could something like this work to remove the background of an image?
Foreground_image = imread('usbuddha.jpg');
Foreground_image = uint8(Foreground_image); %RGB values 0Black-255White
figure; fId = imagesc(Foreground_image); axis image; %Let me look at the foreground image
title('Drag mouse to select pixels for image'); %Tell me to draw defined space
A = imfreehand(); %Lets me freehand draw to select the certain pixels I want
maskImg = A.createMask;
  4 Comments
Image Analyst
Image Analyst on 5 Apr 2014
For what it's worth, see my latest demo attached below. It lets you draw a freehand, irregularly shaped region in one image and paste it onto another image (which must be of the same size).

Sign in to comment.

Accepted Answer

Joseph Cheng
Joseph Cheng on 4 Apr 2014
Edited: Joseph Cheng on 4 Apr 2014
What you have should be fine so far. After your maskimg = A.createMask you can use the find() function to find when maskimg~=0; Using find() will give you the index of the Region Of Interest (ROI). Now that you have the pixel locations of the mask you can do a substitution of the Forground to Background.
Background(index) = Foreground(index);
imagesc(Background);
*note this only works with images that are of the same size. To get user selected insertion point you may want to Shrink the mask just the ROI area and then do a substitution in a similar way.
  2 Comments
Image Analyst
Image Analyst on 4 Apr 2014
You don't need to use find() at all. Simply do
image2(maskimg) = Foreground(maskimg);
Joseph Cheng
Joseph Cheng on 4 Apr 2014
Ahh... Very true, I haven't had to do that in a while.

Sign in to comment.

More Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!