Blend image1 and image2 using 'factor'. Factor can be above 0.0. A value of 0.0 means only image1 is used. A value of 1.0 means only image2 is used. A value between 0.0 and 1.0 means we linearly interpolate the pixel values between the two images. A value greater than 1.0 "extrapolates" t
(image1, image2, factor)
| 97 | |
| 98 | |
| 99 | def blend(image1, image2, factor): |
| 100 | """Blend image1 and image2 using 'factor'. |
| 101 | Factor can be above 0.0. A value of 0.0 means only image1 is used. |
| 102 | A value of 1.0 means only image2 is used. A value between 0.0 and |
| 103 | 1.0 means we linearly interpolate the pixel values between the two |
| 104 | images. A value greater than 1.0 "extrapolates" the difference |
| 105 | between the two pixel values, and we clip the results to values |
| 106 | between 0 and 255. |
| 107 | Args: |
| 108 | image1: An image Tensor of type uint8. |
| 109 | image2: An image Tensor of type uint8. |
| 110 | factor: A floating point value above 0.0. |
| 111 | Returns: |
| 112 | A blended image Tensor of type uint8. |
| 113 | """ |
| 114 | if factor == 0.0: |
| 115 | return tf.convert_to_tensor(image1) |
| 116 | if factor == 1.0: |
| 117 | return tf.convert_to_tensor(image2) |
| 118 | |
| 119 | image1 = tf.to_float(image1) |
| 120 | image2 = tf.to_float(image2) |
| 121 | |
| 122 | difference = image2 - image1 |
| 123 | scaled = factor * difference |
| 124 | |
| 125 | # Do addition in float. |
| 126 | temp = tf.to_float(image1) + scaled |
| 127 | |
| 128 | # Interpolate |
| 129 | if factor > 0.0 and factor < 1.0: |
| 130 | # Interpolation means we always stay within 0 and 255. |
| 131 | return tf.cast(temp, tf.uint8) |
| 132 | |
| 133 | # Extrapolate: |
| 134 | # |
| 135 | # We need to clip and then cast. |
| 136 | return tf.cast(tf.clip_by_value(temp, 0.0, 255.0), tf.uint8) |
| 137 | |
| 138 | |
| 139 | def cutout(image, pad_size, replace=0): |
no outgoing calls
no test coverage detected