def test_tf_rgb_lab(img, tf_v1=True): # raw tensor raw_input = tf.image.convert_image_dtype(img, dtype=tf.float32) raw_input.set_shape([None, None, 3]) # convert to lab-space image {L, a, b} lab = Conv_img.rgb_to_lab(raw_input) L_chan, a_chan, b_chan = Conv_img.preprocess_lab(lab) lab = Conv_img.deprocess_lab(L_chan, a_chan, b_chan) # get back the RGB image (tensor) true_image = Conv_img.lab_to_rgb(lab) true_image = tf.image.convert_image_dtype(true_image, dtype=tf.uint8, saturate=True) # get image array from tensor if tf_v1: # for tf v1.x init_op = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init_op) image = true_image.eval() else: # for tf v2.0 tf.compat.v1.disable_eager_execution() init_op = tf.compat.v1.global_variables_initializer() with tf.compat.v1.Session() as sess: sess.run(init_op) image = true_image.numpy() # save/show image plt.imshow(image) plt.imsave('output.jpg', image) plt.show()
def _augment( img, label_img, resize=None, # Resize the image to some size e.g. [256, 256] scale=1, # Scale image e.g. 1 / 255. hue_delta=0, # Adjust the hue of an RGB image by random factor horizontal_flip=False, # Random left right flip, vertical_flip=False, rot=False, brightness=None, contrast=None, saturation=None, gamma=None, width_shift_range=0, # Randomly translate the image horizontally height_shift_range=0, shape=(256, 256, 3), cspace='RGB'): # Randomly translate the image vertically if hue_delta: img = tf.image.random_hue(img, hue_delta) img, label_img = flip_img(horizontal_flip, vertical_flip, img, label_img) img, label_img = shift_img(img, label_img, width_shift_range, height_shift_range) img, label_img = rot_img(rot, img, label_img) if (brightness is not None): img = tf.image.random_brightness(img, brightness) if (contrast is not None): img = tf.image.random_contrast(img, contrast[0], contrast[1]) if (saturation is not None): img = tf.image.random_saturation(img, saturation[0], saturation[1]) #if (gamma is not None): # gamma_prob = tf.random_uniform([], 0.0, 1.0) # img = tf.cond(tf.less(gamma_prob, 0.5), # lambda: (tf.image.adjust_gamma(img, gamma)), # lambda: (img)) img = tf.clip_by_value(img, 0.0, 1.0) label_img = tf.to_float(label_img) * scale img = tf.to_float(img) * scale if (cspace == 'RGB'): img = img elif (cspace == 'HSV'): img = tf.image.rgb_to_hsv(img) elif (cspace == 'RGB-HSV'): img = tf.concat([img, tf.image.rgb_to_hsv(img)], -1) elif (cspace == 'LAB'): img_lab = Conv_img.rgb_to_lab(img) L, a, b = Conv_img.preprocess_lab(img_lab) img = tf.stack([L, a, b], axis=2) elif (cspace == 'RGB-LAB'): img_lab = Conv_img.rgb_to_lab(img) L, a, b = Conv_img.preprocess_lab(img_lab) img_lab = tf.stack([L, a, b], axis=2) img = tf.concat([img, img_lab], -1) elif (cspace == 'RGB-HSV-LAB'): img_rgb_hsv = tf.concat([img, tf.image.rgb_to_hsv(img)], -1) img_lab = Conv_img.rgb_to_lab(img) L, a, b = Conv_img.preprocess_lab(img_lab) img_lab = tf.stack([L, a, b], axis=2) img = tf.concat([img_rgb_hsv, img_lab], -1) elif (cspace == 'RGB-HSV-L'): img_rgb_hsv = tf.concat([img, tf.image.rgb_to_hsv(img)], -1) img_lab = Conv_img.rgb_to_lab(img) L, a, b = Conv_img.preprocess_lab(img_lab) img = tf.concat([img_rgb_hsv, tf.stack([L], axis=2)], -1) elif (cspace == 'RGB-SV-LAB'): img_hsv = tf.image.rgb_to_hsv(img) H, S, V = tf.unstack(img_hsv, axis=2) img_sv = tf.stack([S, V], axis=2) img_rgb_sv = tf.concat([img, img_sv], -1) img_lab = Conv_img.rgb_to_lab(img) L, a, b = Conv_img.preprocess_lab(img_lab) img_lab = tf.stack([L, a, b], axis=2) img = tf.concat([img_rgb_sv, img_lab], -1) return img, label_img
def _augment(img, label_img, cspace='RGB', augment=True): # Randomly translate the image vertically seed = np.random.randint(2**32 - 1, dtype=np.int64) # print('Seed = {}'.format(seed)) if (augment): img, label_img = jd.transform_img(img, label_img, flip_left_right=True, flip_up_down=True, crop=0.75, rot90=True, brightness=None, blur=False, contrast=None, hue=None, gamma=None, saturation=None, noise=None, size=(256, 256, 3)) #img = tf.image.random_hue(img, 0.1) #img, label_img = flip_img(True, img, label_img) #img, label_img = shift_img(img, label_img, 0.1, # 0.1) if (cspace == 'RGB'): img = img elif (cspace == 'HSV'): img = tf.image.rgb_to_hsv(img) elif (cspace == 'RGB-HSV'): img = tf.concat([img, tf.image.rgb_to_hsv(img)], -1) elif (cspace == 'LAB'): img_lab = Conv_img.rgb_to_lab(img) L, a, b = Conv_img.preprocess_lab(img_lab) img = tf.stack([L, a, b], axis=2) elif (cspace == 'RGB-LAB'): img_lab = Conv_img.rgb_to_lab(img) L, a, b = Conv_img.preprocess_lab(img_lab) img_lab = tf.stack([L, a, b], axis=2) img = tf.concat([img, img_lab], -1) elif (cspace == 'RGB-HSV-LAB'): img_rgb_hsv = tf.concat([img, tf.image.rgb_to_hsv(img)], -1) img_lab = Conv_img.rgb_to_lab(img) L, a, b = Conv_img.preprocess_lab(img_lab) img_lab = tf.stack([L, a, b], axis=2) img = tf.concat([img_rgb_hsv, img_lab], -1) elif (cspace == 'RGB-HSV-L'): img_rgb_hsv = tf.concat([img, tf.image.rgb_to_hsv(img)], -1) img_lab = Conv_img.rgb_to_lab(img) L, a, b = Conv_img.preprocess_lab(img_lab) img = tf.concat([img_rgb_hsv, tf.stack([L], axis=2)], -1) elif (cspace == 'RGB-SV-LAB'): img_hsv = tf.image.rgb_to_hsv(img) H, S, V = tf.unstack(img_hsv, axis=2) img_sv = tf.stack([S, V], axis=2) img_rgb_sv = tf.concat([img, img_sv], -1) img_lab = Conv_img.rgb_to_lab(img) L, a, b = Conv_img.preprocess_lab(img_lab) img_lab = tf.stack([L, a, b], axis=2) img = tf.concat([img_rgb_sv, img_lab], -1) return img, label_img
parser = argparse.ArgumentParser() parser.add_argument('--im', required=False, dest='im', type=str, default='data/umn.jpg', help='image path') args = parser.parse_args() img = misc.imread(args.im) # raw tensor raw_input = tf.image.convert_image_dtype(img, dtype=tf.float32) raw_input.set_shape([None, None, 3]) # convert to lab-space image {L, a, b} lab = Conv_img.rgb_to_lab(raw_input) L_chan, a_chan, b_chan = Conv_img.preprocess_lab(lab) lab = Conv_img.deprocess_lab(L_chan, a_chan, b_chan) # get back the RGB image true_image = Conv_img.lab_to_rgb(lab) true_image = tf.image.convert_image_dtype(true_image, dtype=tf.uint8, saturate=True) init_op = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init_op) # here is the image Tensor :) image = true_image.eval() conv_img = Image.fromarray(image, 'RGB')