14
14
# ==============================================================================
15
15
"""Color operations.
16
16
equalize: Equalizes image histogram
17
+ sharpness: Sharpen image
17
18
"""
18
19
19
20
import tensorflow as tf
20
21
21
- from tensorflow_addons .utils .types import TensorLike
22
+ from tensorflow_addons .utils .types import TensorLike , Number
22
23
from tensorflow_addons .image .utils import to_4D_image , from_4D_image
24
+ from tensorflow_addons .image .compose_ops import blend
23
25
24
26
from typing import Optional
25
27
from functools import partial
@@ -84,7 +86,7 @@ def equalize(
84
86
(num_images, num_rows, num_columns, num_channels) (NHWC), or
85
87
(num_images, num_channels, num_rows, num_columns) (NCHW), or
86
88
(num_rows, num_columns, num_channels) (HWC), or
87
- (num_channels, num_rows, num_columns) (HWC ), or
89
+ (num_channels, num_rows, num_columns) (CHW ), or
88
90
(num_rows, num_columns) (HW). The rank must be statically known (the
89
91
shape is not `TensorShape(None)`).
90
92
data_format: Either 'channels_first' or 'channels_last'
@@ -98,3 +100,55 @@ def equalize(
98
100
fn = partial (equalize_image , data_format = data_format )
99
101
image = tf .map_fn (fn , image )
100
102
return from_4D_image (image , image_dims )
103
+
104
+
105
+ def sharpness_image (image : TensorLike , factor : Number ) -> tf .Tensor :
106
+ """Implements Sharpness function from PIL using TF ops."""
107
+ orig_image = image
108
+ image_dtype = image .dtype
109
+ # Make image 4D for conv operation.
110
+ image = tf .expand_dims (image , 0 )
111
+ # SMOOTH PIL Kernel.
112
+ image = tf .cast (image , tf .float32 )
113
+ kernel = (
114
+ tf .constant (
115
+ [[1 , 1 , 1 ], [1 , 5 , 1 ], [1 , 1 , 1 ]], dtype = tf .float32 , shape = [3 , 3 , 1 , 1 ]
116
+ )
117
+ / 13.0
118
+ )
119
+ # Tile across channel dimension.
120
+ kernel = tf .tile (kernel , [1 , 1 , 3 , 1 ])
121
+ strides = [1 , 1 , 1 , 1 ]
122
+ degenerate = tf .nn .depthwise_conv2d (
123
+ image , kernel , strides , padding = "VALID" , dilations = [1 , 1 ]
124
+ )
125
+ degenerate = tf .clip_by_value (degenerate , 0.0 , 255.0 )
126
+ degenerate = tf .squeeze (tf .cast (degenerate , image_dtype ), [0 ])
127
+
128
+ # For the borders of the resulting image, fill in the values of the
129
+ # original image.
130
+ mask = tf .ones_like (degenerate )
131
+ padded_mask = tf .pad (mask , [[1 , 1 ], [1 , 1 ], [0 , 0 ]])
132
+ padded_degenerate = tf .pad (degenerate , [[1 , 1 ], [1 , 1 ], [0 , 0 ]])
133
+ result = tf .where (tf .equal (padded_mask , 1 ), padded_degenerate , orig_image )
134
+ # Blend the final result.
135
+ blended = blend (result , orig_image , factor )
136
+ return tf .cast (blended , image_dtype )
137
+
138
+
139
+ def sharpness (image : TensorLike , factor : Number ) -> tf .Tensor :
140
+ """Change sharpness of image(s)
141
+
142
+ Args:
143
+ images: A tensor of shape
144
+ (num_images, num_rows, num_columns, num_channels) (NHWC), or
145
+ (num_rows, num_columns, num_channels) (HWC)
146
+ factor: A floating point value or Tensor above 0.0.
147
+ Returns:
148
+ Image(s) with the same type and shape as `images`, sharper.
149
+ """
150
+ image_dims = tf .rank (image )
151
+ image = to_4D_image (image )
152
+ fn = partial (sharpness_image , factor = factor )
153
+ image = tf .map_fn (fn , image )
154
+ return from_4D_image (image , image_dims )
0 commit comments