Performs Gamma Correction on the input image. Also known as Power Law Transform. This function converts the input images at first to float representation, then transforms them pixelwise according to the equation `Out = gain * In**gamma`, and then converts the back to the original data type.
(image, gamma=1, gain=1)
| 1676 | |
| 1677 | @tf_export('image.adjust_gamma') |
| 1678 | def adjust_gamma(image, gamma=1, gain=1): |
| 1679 | """Performs Gamma Correction on the input image. |
| 1680 | |
| 1681 | Also known as Power Law Transform. This function converts the |
| 1682 | input images at first to float representation, then transforms them |
| 1683 | pixelwise according to the equation `Out = gain * In**gamma`, |
| 1684 | and then converts the back to the original data type. |
| 1685 | |
| 1686 | Args: |
| 1687 | image : RGB image or images to adjust. |
| 1688 | gamma : A scalar or tensor. Non negative real number. |
| 1689 | gain : A scalar or tensor. The constant multiplier. |
| 1690 | |
| 1691 | Returns: |
| 1692 | A Tensor. A Gamma-adjusted tensor of the same shape and type as `image`. |
| 1693 | Usage Example: |
| 1694 | ```python |
| 1695 | >> import tensorflow as tf |
| 1696 | >> x = tf.random.normal(shape=(256, 256, 3)) |
| 1697 | >> tf.image.adjust_gamma(x, 0.2) |
| 1698 | ``` |
| 1699 | Raises: |
| 1700 | ValueError: If gamma is negative. |
| 1701 | Notes: |
| 1702 | For gamma greater than 1, the histogram will shift towards left and |
| 1703 | the output image will be darker than the input image. |
| 1704 | For gamma less than 1, the histogram will shift towards right and |
| 1705 | the output image will be brighter than the input image. |
| 1706 | References: |
| 1707 | [1] http://en.wikipedia.org/wiki/Gamma_correction |
| 1708 | """ |
| 1709 | |
| 1710 | with ops.name_scope(None, 'adjust_gamma', [image, gamma, gain]) as name: |
| 1711 | image = ops.convert_to_tensor(image, name='image') |
| 1712 | # Remember original dtype to so we can convert back if needed |
| 1713 | orig_dtype = image.dtype |
| 1714 | |
| 1715 | if orig_dtype in [dtypes.float16, dtypes.float32]: |
| 1716 | flt_image = image |
| 1717 | else: |
| 1718 | flt_image = convert_image_dtype(image, dtypes.float32) |
| 1719 | |
| 1720 | assert_op = _assert(gamma >= 0, ValueError, |
| 1721 | 'Gamma should be a non-negative real number.') |
| 1722 | if assert_op: |
| 1723 | gamma = control_flow_ops.with_dependencies(assert_op, gamma) |
| 1724 | |
| 1725 | # According to the definition of gamma correction. |
| 1726 | adjusted_img = gain * flt_image**gamma |
| 1727 | |
| 1728 | return convert_image_dtype(adjusted_img, orig_dtype, saturate=True) |
| 1729 | |
| 1730 | |
| 1731 | @tf_export('image.convert_image_dtype') |
nothing calls this directly
no test coverage detected