Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- # Example foreground
- fg = np.ones((160, 160, 4), dtype=np.uint8)
- fg[10,10] = [0,0,0,0]
- fg[20,20] = [0,0,0,1]
- fg[30,30] = [0,0,1,0]
- fg[40,40] = [0,1,0,0]
- fg[50,50] = [1,0,0,0]
- fg[60,10] = [0,0,0,0]
- fg[10,70] = [0,0,0,0]
- # Example background
- bg = np.random.randint(0, 255, (1440, 1920, 4))
- # Find coordinates of all pixels with (0,0,0,0)
- fg_zero_pos = np.where((fg == (0,0,0,0)).all(axis=2))
- # Count them
- fg_zero_count = len(fg_zero_pos[0])
- # Create random replacement coordinates for each element of
- r = np.random.randint(0, bg.shape[0]-1, fg_zero_count)
- c = np.random.randint(0, bg.shape[1]-1, fg_zero_count)
- bg_rand_pos = (r,c)
- # Perform replacement
- fg[fg_zero_pos] = bg[bg_rand_pos]
- print(fg[fg_zero_pos])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement