Home > Mobile >  How to insert in a specific position of the background image the foreground object?
How to insert in a specific position of the background image the foreground object?

Time:04-13

My goal is to insert the foreground object at a specific position in the background image (for example, in the center part, in the lower left part, etc.) using Python.

Get the foreground (surrounding pixels set to zero):

# Read the image
img = cv2.imread('/content/foreground.jpg')/255.0

foreground = img.copy()
foreground[foreground>=0.9]=0
plt.axis('off')
plt.imshow(foreground)
plt.show()

Create a mask for this foreground object:

def getForegroundMask(foreground):
    mask_new = foreground.copy()[:,:,0]
    mask_new[mask_new>0] = 1
    return mask_new

mask_new = getForegroundMask(foreground)
plt.imshow(mask_new)
plt.axis('off')
plt.show()

Get the background:

background = cv2.imread('/content/background.png')/255.0

plt.imshow(background)
plt.axis('off')
plt.show()

Compose the object with the background:

def compose(foreground, mask, background):
    # resize background
    background = transform.resize(background, foreground.shape[:2])
    
    # Subtract the foreground area from the background
    background = background*(1 - mask.reshape(foreground.shape[0], foreground.shape[1], 1))
    
    # Finally, add the foreground
    composed_image = background   foreground
    
    return composed_image

The result, the foreground object takes the full size of the background image, but what I want is to customize and insert this object in different parts and in different sizes. How could I do it?

enter image description here

CodePudding user response:

For this, you can use slicing. Let's say you want to put your foreground at (x, y) coordinates on the background.

h, w = foreground.shape[:2]

# Subtract the foreground area from the background
background[y:y h, x:x w] *= (1 - mask.reshape(foreground.shape[0], foreground.shape[1], 1))

background[y:y h, x:x w]  = foreground

return background

Here, make sure that y h and x w do not exceed background dimentions.

CodePudding user response:

Rahul Kedia's answer forms the base of this solution. However, you do not need to subtract a mask region from the background to later add the foreground.

You can overlay it by just mentioning the location of your interest. In the following example I am placing the foreground image from the origin.

background = cv2.imread(background_image_path, 1)
foreground = cv2.imread(foreground_image_path, 1)

f_ht, f_wd, _ = foreground.shape
background[0:f_ht, 0:f_wd] = foreground

CodePudding user response:

Straight forward less than 25 lines. We don't used matplotlib library.

#!/usr/bin/python3.9.2
 
import cv2
import numpy as np

img1 = cv2.imread('ralph.jpg')
overlay_img1 = np.ones(img1.shape,np.uint8)*255

img2 = cv2.imread('mainlogo.png')
rows,cols,channels = img2.shape
overlay_img1[450:rows 450, 450:cols 450 ] = img2

img2gray = cv2.cvtColor(overlay_img1,cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray,220,55,cv2.THRESH_BINARY_INV)
mask_inv = cv2.bitwise_not(mask)
temp1 = cv2.bitwise_and(img1,img1,mask = mask_inv)
temp2 = cv2.bitwise_and(overlay_img1,overlay_img1, mask = mask)

result = cv2.add(temp1,temp2)
cv2.imshow("Result",result)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output overlay: enter image description here

  • Related