3

The input images are loaded:

target_img = spi.imread(target_img, mode="RGB").astype(float)/256
source_img = spi.imread(source_img, mode="RGB").astype(float)/256
target_mask = spi.imread(target_mask, mode="RGB").astype(float)
source_mask = spi.imread(source_mask, mode="RGB").astype(float)

Some variables are set:

target_mask_list = []
source_mask_list = []
mask_list = []
i = 0
matched_piece = None

I extract the masked region of the target and source images, using their mask images:

for color in list(color_list):
    print(color)
    #Get Target Mask Regions
    target_color_mask = extract_mask(target_mask, color)
    masked_target = np.where(target_color_mask[...,None], target_img, 0)
    #imsave(output_name, image_mask)
    target_mask_list.append(masked_target)
    #Get Source Mask Regions
    source_color_mask = extract_mask(source_mask, color)
    masked_source = np.where(source_color_mask[...,None], source_img, 0)
    source_mask_list.append(masked_source)

Currently the "non selected" regions are either all black or all white depending on if I use a 0 or a 1 with np.where. The "non selected" regions have zero opacity, and as a result I cannot properly layer these images.

Then I run the mask through a color transfer function:

    # Perform Color Transfer
    matched_mask = match_color(masked_target, masked_source, mode='pca', eps=1e-5)
    mask_list.append(matched_mask)
    if i == 0: 
        matched_piece = np.where(target_color_mask[...,None], matched_mask, 0)
    else: 
        matched_piece = matched_piece + np.where(target_color_mask[...,None], matched_mask, 0)
    i = i + 1  

The resulting saved image is completely black:

masked = matched_piece
imsave(output_name, masked)

When I use GIMP to make target_img or source_img have transparent parts, I can successfully run these images through the color transfer function so that the color is transferred between the non transparent regions. I can then manually crop the resulting previous transparent sections out, so that I have the colored output image.

How can I make my code work more like the manual GIMP steps?

Edit:

The extract_mask function returns a boolean array from:

   def extract_mask(image, color_list):
       mask = None
       if color == 'green':
           mask = np.all(image == (0,255,0), axis=-1).astype(int)   
       elif color == 'black': 
           mask = np.all(image == (0,0,0), axis=-1).astype(int)
       elif color == 'white':
           mask = np.all(image == (255,255,255), axis=-1).astype(int)
       elif color == 'red':
           mask = np.all(image == (255,0,0), axis=-1).astype(int)
       elif color == 'blue':
           mask = np.all(image == (0,0,255), axis=-1).astype(int)
       elif color == 'yellow':
           mask = np.all(image == (255,255,0), axis=-1).astype(int)
       elif color == 'grey':
           mask = np.all(image == (128,128,128), axis=-1).astype(int)
       elif color == 'lightblue':
           mask = np.all(image == (0,255,255), axis=-1).astype(int)
       elif color == 'purple':
           mask = np.all(image == (255,0,255), axis=-1).astype(int)
       else: 
           print "Color not recognized"
       return mask 
ProGamerGov
  • 870
  • 1
  • 10
  • 23

0 Answers0