Guest User

Untitled

a guest
Sep 15th, 2020
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.70 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import math
  4.  
  5.  
  6. # Stolen from https://stackoverflow.com/questions/40895785/using-opencv-to-overlay-transparent-image-onto-another-image
  7. def apply_overlay(background, foreground):
  8.     # normalize alpha channels from 0-255 to 0-1
  9.     alpha_background = background[:,:,3] / 255.0
  10.     alpha_foreground = foreground[:,:,3] / 255.0
  11.  
  12.     # set adjusted colors
  13.     for color in range(0, 3):
  14.         background[:,:,color] = alpha_foreground * foreground[:,:,color] + \
  15.             alpha_background * background[:,:,color] * (1 - alpha_foreground)
  16.  
  17.     # set adjusted alpha and denormalize back to 0-255
  18.     background[:,:,3] = (1 - (1 - alpha_foreground) * (1 - alpha_background)) * 255
  19.     return background
  20.  
  21.  
  22. # Generates a camo overlay for an image. Changing this function will change how the overlay is generated.
  23. # The algorithm below "blocks" the image into "super pixels" of equal size. It then selects a random subset
  24. # of the super pixels to be replaced with "replace_block". If "replace_block" is not set, it defaults to black
  25. # with 25% opacity, as per recommendation in (https://wearethene.ws/notable/139468)
  26. def generate_overlay(dims, blocks, percent_overlaid=0.23, replace_block=None):
  27.     if replace_block is None:
  28.         replace_block = [0, 0, 0, 64]
  29.    
  30.     def blockify_dimension( dim_val, blocks ):
  31.         if dim_val < blocks:
  32.             blocks = dim_val
  33.         split = np.array_split(range(dim_val), blocks)
  34.         return split
  35.  
  36.     def generate_mask( rows, cols ):
  37.         num_blocks = rows * cols
  38.         overlaid_blocks = math.ceil( num_blocks * percent_overlaid )
  39.         mask = np.arange(rows*cols)
  40.         mask = np.where(mask<overlaid_blocks, 1, 0)
  41.         np.random.shuffle(mask)
  42.         mask = mask.reshape(rows,cols)
  43.         return mask
  44.    
  45.     d_row = blockify_dimension( dims[0], blocks )
  46.     d_col = blockify_dimension( dims[1], blocks )
  47.  
  48.     masked_image = np.zeros(dims, np.uint8)
  49.  
  50.     mask = generate_mask(len(d_row), len(d_col))
  51.     for row_idx, row in enumerate(d_row):
  52.         for col_idx, col in enumerate(d_col):
  53.             if mask[row_idx][col_idx] == 1:
  54.                 for r in row:
  55.                     for c in col:
  56.                         masked_image[r][c] = replace_block
  57.     return masked_image
  58.  
  59.  
  60.  
  61. # Driver function. This first reads the input image, ensures it's in RGBA mode,
  62. # generates an overlay, applies the overlay, then writes the new image.
  63. def camo_image(input_fn, output_fn, blocks=20, replace_block=None):
  64.     img = cv2.imread(input_fn)
  65.     img = cv2.cvtColor(img, cv2.COLOR_RGB2RGBA)
  66.     overlay = generate_overlay( img.shape, blocks)
  67.     overlaid = apply_overlay(img, overlay)
  68.     cv2.imwrite(output_fn, overlaid)
Add Comment
Please, Sign In to add comment