Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow_addons/image/compose_ops.py: 21%

19 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-03 07:57 +0000

1# Copyright 2019 The TensorFlow Authors. All Rights Reserved. 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14# ============================================================================== 

15"""Compose Ops""" 

16 

17import tensorflow as tf 

18 

19from tensorflow_addons.utils.types import TensorLike, Number 

20 

21 

22def blend(image1: TensorLike, image2: TensorLike, factor: Number) -> tf.Tensor: 

23 """Blend `image1` and `image2` using `factor`. 

24 

25 Factor can be above 0.0. A value of 0.0 means only `image1` is used. 

26 A value of 1.0 means only `image2` is used. A value between 0.0 and 

27 1.0 means we linearly interpolate the pixel values between the two 

28 images. A value greater than 1.0 "extrapolates" the difference 

29 between the two pixel values, and we clip the results to values 

30 between 0 and 255. 

31 

32 Args: 

33 image1: An image Tensor of shape 

34 `(num_rows, num_columns, num_channels)` (HWC), or 

35 `(num_rows, num_columns)` (HW), or 

36 `(num_channels, num_rows, num_columns)` (CHW). 

37 image2: An image Tensor of shape 

38 `(num_rows, num_columns, num_channels)` (HWC), or 

39 `(num_rows, num_columns)` (HW), or 

40 `(num_channels, num_rows, num_columns)`. 

41 factor: A floating point value or Tensor of type `tf.float32` above 0.0. 

42 

43 Returns: 

44 A blended image Tensor of `tf.float32`. 

45 

46 """ 

47 with tf.name_scope("blend"): 

48 

49 if factor == 0.0: 

50 return tf.convert_to_tensor(image1) 

51 if factor == 1.0: 

52 return tf.convert_to_tensor(image2) 

53 

54 image1 = tf.cast(image1, dtype=tf.dtypes.float32) 

55 image2 = tf.cast(image2, dtype=tf.dtypes.float32) 

56 

57 difference = image2 - image1 

58 scaled = factor * difference 

59 

60 # Do addition in float. 

61 temp = image1 + scaled 

62 

63 # Interpolate 

64 if factor > 0.0 and factor < 1.0: 

65 # Interpolation means we always stay within 0 and 255. 

66 temp = tf.round(temp) 

67 return temp 

68 

69 # Extrapolate: 

70 # 

71 # We need to clip and then cast. 

72 temp = tf.round(tf.clip_by_value(temp, 0.0, 255.0)) 

73 return temp