def test_tf_get_all_colors_without_black(self): # 1) Get sample image as tf test_file_name: str = "lenna.png" test_image_fullpath: str = os.path.join( self.test_path, self.test_resource_folder_name, test_file_name ) tf_image = tf_images.decode_png(test_image_fullpath, 3) # 2) Calculate colors in image tf_image_colors = tf_images.tf_get_all_colors(tf_image, black_first=False) self.assertTrue( tf.math.reduce_all(tf.math.not_equal(tf_image_colors[0], [0, 0, 0])) )
def test_decode_image(self): # Expected Result sample_image_shape = [180, 180, 3] result = tf.constant(sample_image_shape) # 1) Get sample image as tf test_file_name: str = "sample.png" test_image_fullpath: str = os.path.join( self.test_path, self.test_resource_folder_name, test_file_name ) tf_image = tf_images.decode_png(test_image_fullpath, 3) # 2) Calculate shape self.assertTrue(tf.math.reduce_all(tf.math.equal(tf.shape(tf_image), result)))
def test_tf_img_to_minmax(self): # Expected Result threshold = 127 min_maxed_unique_values = (255, 0) number_of_black_color = 31760 # 1) Get sample image as tf test_file_name: str = "sample.png" test_image_fullpath: str = os.path.join( self.test_path, self.test_resource_folder_name, test_file_name ) grayscale_tf_image = tf_images.decode_png(test_image_fullpath, 1) # 2) Calculate min max min_maxed_grayscale_tf_image = tf_images.tf_img_to_minmax( grayscale_tf_image, threshold, (0, 255) ) # check unique reshaped_min_maxed_grayscale_tf_image = tf.reshape( min_maxed_grayscale_tf_image, (-1, 1) ) unique_min_max_values = gen_array_ops.unique_v2( reshaped_min_maxed_grayscale_tf_image, axis=[-2] )[0] self.assertTrue( tf.math.reduce_all( tf.math.equal( unique_min_max_values, tf.cast( tf.expand_dims(tf.constant(min_maxed_unique_values), axis=-1), tf.float32, ), ) ) ) self.assertTrue( tf.math.equal( tf.math.count_nonzero(min_maxed_grayscale_tf_image), number_of_black_color, ) )
def test_tf_get_all_colors(self): # Expected Result sample_image_colors = [ [0.0, 0.0, 0.0], [245.0, 245.0, 245.0], [255.0, 145.0, 77.0], [71.0, 71.0, 71.0], [72.0, 72.0, 72.0], ] result = tf.constant(sample_image_colors) # 1) Get sample image as tf test_file_name: str = "sample.png" test_image_fullpath: str = os.path.join( self.test_path, self.test_resource_folder_name, test_file_name ) tf_image = tf_images.decode_png(test_image_fullpath, 3) # 2) Calculate colors in image tf_image_colors = tf_images.tf_get_all_colors(tf_image) print(tf_image_colors) self.assertTrue(tf.math.reduce_all(tf.math.equal(tf_image_colors, result)))
def test_tf_generate_color_map(self): sample_indexes = [0.0, 1.0, 2.0, 3.0, 4.0] sample_image_colors = [ [0.0, 0.0, 0.0], [245.0, 245.0, 245.0], [255.0, 145.0, 77.0], [71.0, 71.0, 71.0], [72.0, 72.0, 72.0], ] result = tf.constant(sample_image_colors) # 1) Get sample image as tf test_file_name: str = "sample.png" test_image_fullpath: str = os.path.join( self.test_path, self.test_resource_folder_name, test_file_name ) tf_image = tf_images.decode_png(test_image_fullpath, 3) color_map = tf_images.tf_generate_color_map(tf_image) print(color_map) self.assertTrue(tf.math.reduce_all(tf.math.equal(color_map[0], sample_indexes))) self.assertTrue(tf.math.reduce_all(tf.math.equal(color_map[1], result)))
def test_tf_get_sorted_unique_color(self): sample_image_colors = [ [88.0, 18.0, 60.0], [176.0, 67.0, 79.0], [205.0, 96.0, 97.0], [178.0, 69.0, 80.0], [206.0, 100.0, 94.0], ] result = tf.constant(sample_image_colors) # 1) Get sample image as tf test_file_name: str = "lenna.png" test_image_fullpath: str = os.path.join( self.test_path, self.test_resource_folder_name, test_file_name ) tf_image = tf_images.decode_png(test_image_fullpath, 3) self.assertTrue( tf.math.reduce_all( tf.math.equal( tf_images.tf_get_sorted_unique_color(tf_image)[:5], result ) ) )
def test_tf_image_detach_with_id_color_list(self): # 1) Get sample image as tf test_file_name: str = "sample.png" test_image_fullpath: str = os.path.join( self.test_path, self.test_resource_folder_name, test_file_name ) tf_image = tf_images.decode_png(test_image_fullpath, 3) # 2) ID Color list # id_color_list = tf_images.tf_generate_random_color_map( # tf_image, shuffle_exclude_first=1, bin_size=30 # ) id_color_list = tf_images.tf_generate_color_map(tf_image) print(tf.shape(tf_image)) print(id_color_list) print( tf.shape( tf_images.tf_image_detach_with_id_color_list( tf_image, id_color_list=id_color_list, bin_num=30 ) ) )