title: Changing a part of an image
body: <p>How can I paste back a part of my picture after I changed it in MATLAB? I cropped the plates off this image of a car and now I want to put it back automatically. The plates are still at the same coordinates as they were initially, but the background is all black.</p>
  
  <p>The car I want to paste on:</p>
  
  <p><a href="https://i.stack.imgur.com/eoX1e.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eoX1e.jpg" alt="Car"></a></p>
  
  <p>And this is what I want to paste:</p>
  
  <p><a href="https://i.stack.imgur.com/WX9vX.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WX9vX.jpg" alt="to paste"></a></p>
  
  <p>Here is my code, I want to change the part where I need to draw by hand the plate. </p>
  
  <pre><code>fontSize = 20;
  format compact;
  
  baseFileName1 = 'blurr_plate.jpg';
  baseFileName2 = 'car2.jpg';
  
  sourceImage = imread(baseFileName1);
  subplot(1, 2, 1);
  imshow(sourceImage);
  axis on;
  caption = sprintf('Source image, %s', baseFileName1);
  title(caption, 'FontSize', fontSize, 'Interpreter', 'none');
  
  % Enlarge figure to full screen.
  set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
  % Give a name to the title bar.
  set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
  
  targetImage = imread(baseFileName2);
  subplot(1, 2, 2);
  imshow(targetImage);
  axis on; 
  caption = sprintf('Target image, %s, original', baseFileName2);
  title(caption, 'FontSize', fontSize, 'Interpreter', 'none');
  
  % Ask user to draw freehand mask.
  message = sprintf('In the LEFT IMAGE...\nLeft click and hold to begin drawing.\nSimply lift the mouse button to finish');
  subplot(1, 2, 1);
  uiwait(msgbox(message));
  hFH = imfreehand(); % Actual line of code to do the drawing.
  % Create a binary image ("mask") from the ROI object.
  mask = hFH.createMask();
  xy = hFH.getPosition;
  
  % Paste it onto the target image.
  targetImage(mask) = sourceImage(mask);
  
  % Display new image.
  subplot(1, 2, 2); % Switch active axes to right hand axes.
  imshow(targetImage);
  imwrite(targetImage, 'car_new.jpg')
  axis on;
  caption = sprintf('Target image, %s, after paste', baseFileName2);
  title(caption, 'FontSize', fontSize, 'Interpreter', 'none');
  </code></pre>