How to copy a cropped image onto the original one, given the coordinates of the center of the crop
Question: I'm cropping an image like this:

    
    
    self.rst = self.img_color[self.param_a_y:self.param_b_y,
                            self.param_a_x:self.param_b_x:, ]
    

How do I copy this image back to the original one. The data I have available
are the coordinates of the original image, which makes the center of the crop.

Seems like there's no`copy_to()` function for python


Answer: I failed myself getting copy_to() working a few days ago, but came up with a
difeerent solution: You can uses masks for this task.

I have an example at hand which shows how to create a mask from a defined
colour range using inrange. With that mask, you create two partial images
(=masks), one for the old content and one for the new content, the not used
area in both images is back. Finally, a simple bitwise_or combines both
images.

This works for arbitrary shapes, so you can easily adapt this to rectangular
ROIs.

    
    
    import cv2
    import numpy as np
    img = cv2.imread('image.png')
    
    rows,cols,bands = img.shape 
    print rows,cols,bands
    
    # Create image with new colour for replacement
    new_colour_image= np.zeros((rows,cols,3), np.uint8)
    new_colour_image[:,:]= (255,0,0) 
    
    # Define range of color to be exchanged (in this case only one single color, but could be range of colours)
    lower_limit = np.array([0,0,0])
    upper_limit = np.array([0,0,0])
    
    # Generate mask for the pixels to be exchanged
    new_colour_mask = cv2.inRange(img, lower_limit, upper_limit)
    
    # Generate mask for the pixels to be kept
    old_image_mask=cv2.bitwise_not(new_colour_mask)
    
    # Part of the image which is kept
    img2= cv2.bitwise_and(img,img, old_image_mask)
    
    # Part of the image which is replaced
    new_colour_image=cv2.bitwise_and(new_colour_image,new_colour_image, new_colour_mask)
    
    #Combination of the two parts
    result=cv2.bitwise_or(img2, new_colour_image)
    
    cv2.imshow('image',img)
    cv2.imshow('mask',new_colour_mask)
    cv2.imshow('r',result)
    cv2.waitKey(0)
    

