Skip to content

RGB to Grayscale #1772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
/tensorflow_addons/image/tests/interpolate_spline_test.py
/tensorflow_addons/image/resampler_ops.py @autoih
/tensorflow_addons/image/tests/resampler_ops_test.py @autoih
/tensorflow_addons/image/rgb_to_grayscale_op.py @ghosalsattam
/tensorflow_addons/image/tests/rgb_to_grayscale_op_test.py @ghosalsattam
/tensorflow_addons/image/sparse_image_warp.py
/tensorflow_addons/image/tests/sparse_image_warp_test.py
/tensorflow_addons/image/transform_ops.py @mels630
Expand Down
1 change: 1 addition & 0 deletions tensorflow_addons/image/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from tensorflow_addons.image.cutout_ops import random_cutout
from tensorflow_addons.image.distort_image_ops import random_hsv_in_yiq
from tensorflow_addons.image.resampler_ops import resampler
from tensorflow_addons.image.rgb_to_grayscale_op import rgb_to_grayscale
from tensorflow_addons.image.transform_ops import rotate
from tensorflow_addons.image.transform_ops import shear_x
from tensorflow_addons.image.transform_ops import shear_y
Expand Down
45 changes: 45 additions & 0 deletions tensorflow_addons/image/rgb_to_grayscale_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"RGB to Grayscale op"

from tensorflow_addons.utils.types import FloatTensorLike
from tensorflow_addons.image import utils as img_utils
import tensorflow as tf

from typing import Optional


def rgb_to_grayscale(
image: FloatTensorLike, name: Optional[str] = None,
) -> FloatTensorLike:
"""Perform RGB to Grayscale conversion of image(s).

Args:
image: A 3-D `Tensor` of shape `[height, width, channels]`,
or a 4-D `Tensor` of shape `[batch_size, height, width, channels]`.
The number of channels must be equal to 3.
name: A name for this operation (optional).
"""

with tf.name_scope(name or "rgb_to_grayscale"):
image = tf.cast(image, tf.float32)
original_ndims = img_utils.get_ndims(image)
image = img_utils.to_4D_image(image)
channels = tf.shape(image)[3]
if channels != 3:
raise ValueError("The image must be in RGB format")
grayscale_output = tf.image.rgb_to_grayscale(image)
grayscale_output = img_utils.from_4D_image(grayscale_output, original_ndims)
return grayscale_output
89 changes: 89 additions & 0 deletions tensorflow_addons/image/tests/rgb_to_grayscale_op_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"rgb to grayscale op test"


import numpy as np
import tensorflow as tf
from tensorflow_addons.image.rgb_to_grayscale_op import rgb_to_grayscale
import pytest


@pytest.mark.usefixtures("maybe_run_functions_eagerly")
def test_rgb_to_grayscale_op():
img = tf.constant(
[
[
[
[0.0, 0.0, 0.0],
[2.0, 2.0, 2.0],
[4.0, 4.0, 4.0],
[6.0, 6.0, 6.0],
[8.0, 8.0, 8.0],
],
[
[10.0, 10.0, 10.0],
[12.0, 12.0, 12.0],
[14.0, 14.0, 14.0],
[16.0, 16.0, 16.0],
[18.0, 18.0, 18.0],
],
[
[20.0, 20.0, 20.0],
[22.0, 22.0, 22.0],
[24.0, 24.0, 24.0],
[26.0, 26.0, 26.0],
[28.0, 28.0, 28.0],
],
[
[30.0, 30.0, 30.0],
[32.0, 32.0, 32.0],
[34.0, 34.0, 34.0],
[36.0, 36.0, 36.0],
[38.0, 38.0, 38.0],
],
[
[40.0, 40.0, 40.0],
[42.0, 42.0, 42.0],
[44.0, 44.0, 44.0],
[46.0, 46.0, 46.0],
[48.0, 48.0, 48.0],
],
]
],
dtype=tf.float32,
)
r = np.arange(0, 50, 2)
count = 0
exp_plane = np.ones([5, 5, 1])
for i in range(5):
for j in range(5):
exp_plane[i][j][0] = (
0.2989 * r[count] + 0.5870 * r[count] + 0.1140 * r[count]
)
count += 1
grayscale_tfa = rgb_to_grayscale(img)
grayscale_tfa = grayscale_tfa.numpy()
grayscale_tfa = np.resize(grayscale_tfa, (5, 5))
exp_plane = np.resize(exp_plane, (5, 5))
np.testing.assert_allclose(grayscale_tfa, exp_plane, 0.06)


@pytest.mark.usefixtures("maybe_run_functions_eagerly")
def test_rgb_to_grayscale_op_random_image():
img = tf.random.uniform([2, 40, 40, 3], minval=0, maxval=255, dtype=tf.float32)
exp_plane = tf.image.rgb_to_grayscale(img)
grayscale_tfa = tf.image.rgb_to_grayscale(img)
np.testing.assert_allclose(grayscale_tfa, exp_plane, 0.06)