Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. import cv2
  2. import glob
  3. import os
  4. import random
  5. import numpy as np
  6.  
  7.  
  8. def create_composite_image(_fg_image , _bg_image):
  9. """
  10. Creates composite images from a given set of foreground and background images
  11. :param _fg_image: path to foreground images folder
  12. :param _bg_image: path to background images folder
  13. :return:
  14. """
  15. flag = 0
  16. for plate in glob.glob(_bg_image):
  17. composite_folder_name = os.path.splitext(plate)[0] + "_composite"
  18. mask_folder_name = os.path.splitext(plate)[0] + "_mask"
  19.  
  20. if not os.path.exists(composite_folder_name):
  21. os.makedirs(composite_folder_name)
  22.  
  23. if not os.path.exists(mask_folder_name):
  24. os.makedirs(mask_folder_name)
  25.  
  26. for food in glob.glob(_fg_image):
  27. plate_base_image = cv2.imread(plate, cv2.IMREAD_UNCHANGED)
  28. plate_base_image = cv2.resize(plate_base_image, (640, 480))
  29. h_1, w_1 = plate_base_image.shape[:2]
  30.  
  31. random_size = random.randint(1, 100)
  32. food_image = cv2.imread(food, cv2.IMREAD_UNCHANGED)
  33. food_image = cv2.resize(food_image, (200 + random_size, 200 + random_size))
  34.  
  35. h1, w1 = food_image.shape[:2]
  36. b, g, r, a = cv2.split(food_image)
  37.  
  38. x = int((w_1 / 2) - (w1 / 2))
  39. y = int((h_1 / 2) - (h1 / 2))
  40.  
  41. print(food_image.shape, plate_base_image.shape)
  42.  
  43. cropped_bg_image = plate_base_image[y:y + h1, x:x + w1]
  44. inv_a = cv2.bitwise_not(a)
  45.  
  46. image1_out = cv2.bitwise_and(cropped_bg_image, cropped_bg_image, mask=inv_a)
  47. image2_out = cv2.bitwise_and(food_image, food_image, mask=a)
  48. image_cropped = cv2.bitwise_or(image1_out, image2_out)
  49.  
  50. segmented_mask = get_mask(a)
  51.  
  52. plate_base_image[y:y + h1, x:x + w1] = image_cropped
  53.  
  54. cv2.imwrite(composite_folder_name + "/" + str(flag) + "_" + os.path.basename(food), plate_base_image)
  55. cv2.imwrite(mask_folder_name + "/" + str(flag) + "_" + os.path.basename(food), segmented_mask)
  56.  
  57. print(composite_folder_name + "/" + str(flag) + "_" + os.path.basename(food))
  58.  
  59. flag += 1
  60.  
  61. def get_mask(_in):
  62.  
  63. bg = np.zeros((480,640))
  64. h_1, w_1 =bg.shape[:2]
  65.  
  66. #b, g, r, a = cv2.split(_in)
  67. h1 , w1 = _in.shape[:2]
  68.  
  69. x = int((w_1 / 2) - (w1 / 2))
  70. y = int((h_1 / 2) - (h1 / 2))
  71.  
  72. bg[y:y + h1, x:x + w1] = _in
  73.  
  74. return bg
  75.  
  76.  
  77. if __name__ == "__main__":
  78.  
  79. BG_FOLDER_PATH = "/home/anirudh/HBRS/HomeLab/Dataset/dataset_green_segmentation/bg_png/*.png"
  80. FG_FOLDERS_PATH = "/home/anirudh/HBRS/HomeLab/Dataset/dataset_green_segmentation/fg/*.png"
  81.  
  82. create_composite_image(FG_FOLDERS_PATH,BG_FOLDER_PATH)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement