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

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 commentaires

Now we need an explanation what "copy onto another image" exactly means. Do you want to copy the data to the top left corner, resize it to cover the new picture completely or call imfreehand to mark a new area to copy the data into? Please post more details of what you are trying to achieve. We cannot guess this.
I would like to be able to maybe have a function and input a position as arguments and have the function return a new image with the first one on top of the second one in the spot I specify. I've never used MATLAB before so I've just been trying to guess and try everything, so I realize my code probably isn't what I need. My assignment says I can copy it by replacing the pixels in the bottom image with pixels in the top image.
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).
Thank you so much!!

Connectez-vous pour commenter.

 Réponse acceptée

Joseph Cheng
Joseph Cheng le 4 Avr 2014
Modifié(e) : Joseph Cheng le 4 Avr 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 commentaires

You don't need to use find() at all. Simply do
image2(maskimg) = Foreground(maskimg);
Ahh... Very true, I haven't had to do that in a while.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Images dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by