Skip to content Skip to sidebar Skip to footer

Filling A Blank Image With Another With Opencv

I'm using python with cv2 library. I have a small image that I want to fill some blank space around. Say the blank image is as following: (each x means a pixel, [255,255,255]) x x

Solution 1:

Use numpy slicing.

tmp = np.zeros((1024,768,3),np.uint_8)

img[y1:y2, x1:x2] = tmp[y3:y4, x3:x4] # given that (y2-y1) = (y4 - y3) and same for x

Or fill it with some color

img[y1:y2, x1:x2] = (255, 255, 255)

Solution 2:

Change image to an array first and then do replacing.

Try this:

import cv2
import numpy as np
tmp=np.zeros((1024,768,3),np.uint_8)
image= .. #image captured from camera
src = np.array(image)

tmp[144:, 197:] = src

Make sure number of elements are right first.

Solution 3:

If you want to replace some big parts like rectangles, then use ROI to replace (as other answers already explained). But if you are dealing with randomly distributed pixels or complex shapes then you can try this.

1) Create a mask binary image, MaskImage, for the part to be replaced as true rest is set as false.

2) Result = MasterImageAND (NOT(MaskImage)) + PickerImageANDMaskImage.

PS: I haven't used python as I don't know Python and it pretty easy expression. Good Luck

Solution 4:

In short you can do like that :

Sizetaille= new_image.size();   // New_image is the image to be inserted
Mat  white_roi( white_image, Rect( x, y, taille.width, taille.height ) );
                                  // white_roi is a subimage of white_image//          that start from the point (x,y)// and have the same dimension as a new_image
new_image.copyTo( white_roi );    // You copy the new_image into the white_roi,//          this in the original image

This is c++ code, you have to adapt to python.

Post a Comment for "Filling A Blank Image With Another With Opencv"