2

I am trying to replace objects which I found using a mask with the original images pixels. I have a mask that shows black where the object is not detected and white if detected. I am then using the image in a where statement

image[np.where((image2 == [255,255,255].any(axis = 2)) 

I am stuck here and I have no idea how to change found white values to what the original image is (to use alongside other masks). I have tried image.shape and this did not work.

Thanks.

mDumple
  • 45
  • 2
  • 7

3 Answers3

3

Make a copy of the mask and then draw the original image over the white pixels of the mask from the white pixel coordinates. You can also check mask == 255 to compare element-wise. You don't need np.where because you can index arrays via the boolean mask created by mask == 255.

out = mask.copy()
out[mask == 255] = original_image[mask == 255]
sapht
  • 2,789
  • 18
  • 16
2

You can use bitwise operations. Try this:

replaced_image = cv2.bitwise_and(original_image,original_image,mask = your_mask)

Example Visit https://docs.opencv.org/3.3.0/d0/d86/tutorial_py_image_arithmetics.html to learn more about bitwise operations

janu777
  • 1,940
  • 11
  • 26
0
import os
import cv2
from netpbmfile import imread
 
img_dir = '.'
mask_dir = '.'
new_bg = 'image.png'

def get_foreground(fg_image_name, mask_name, bg_image_name):
    fg_image = cv2.imread(fg_image_name)
    mask = imread(mask_name)
    mask_inverse = (1-mask)
    bg_image = cv2.imread(bg_image_name)
    bg_image = cv2.resize(bg_image, (fg_image.shape[1], fg_image.shape[0]))
    foregound = cv2.bitwise_and(fg_image, fg_image, mask=mask)
    background = cv2.bitwise_and(bg_image, bg_image, mask=mask_inverse)
    composite = foregound + background
    
    return composite


image_fg = get_foreground(os.path.join(img_dir, "NP1_0.jpg"), os.path.join(mask_dir, "NP1_0_mask.pbm"), new_bg)
cv2.imwrite("foreground.jpg", image_fg)
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 27 '22 at 05:20