tf.image.sobel_edges
Stay organized with collections
Save and categorize content based on your preferences.
Returns a tensor holding Sobel edge maps.
tf.image.sobel_edges(
image
)
Example usage:
For general usage, image
would be loaded from a file as below:
image_bytes = tf.io.read_file(path_to_image_file)
image = tf.image.decode_image(image_bytes)
image = tf.cast(image, tf.float32)
image = tf.expand_dims(image, 0)
But for demo purposes, we are using randomly generated values for image
:
image = tf.random.uniform(
maxval=255, shape=[1, 28, 28, 3], dtype=tf.float32)
sobel = tf.image.sobel_edges(image)
sobel_y = np.asarray(sobel[0, :, :, :, 0]) # sobel in y-direction
sobel_x = np.asarray(sobel[0, :, :, :, 1]) # sobel in x-direction
For displaying the sobel results, PIL's Image Module can be used:
# Display edge maps for the first channel (at index 0)
Image.fromarray(sobel_y[..., 0] / 4 + 0.5).show()
Image.fromarray(sobel_x[..., 0] / 4 + 0.5).show()
Args |
image
|
Image tensor with shape [batch_size, h, w, d] and type float32 or
float64. The image(s) must be 2x2 or larger.
|
Returns |
Tensor holding edge maps for each channel. Returns a tensor with shape
[batch_size, h, w, d, 2] where the last two dimensions hold [[dy[0], dx[0]],
[dy[1], dx[1]], ..., [dy[d-1], dx[d-1]]] calculated using the Sobel filter.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2023-03-23 UTC.
[null,null,["Last updated 2023-03-23 UTC."],[],[],null,["# tf.image.sobel_edges\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.11.1/tensorflow/python/ops/image_ops_impl.py#L4658-L4724) |\n\nReturns a tensor holding Sobel edge maps.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.image.sobel_edges`](https://www.tensorflow.org/api_docs/python/tf/image/sobel_edges)\n\n\u003cbr /\u003e\n\n tf.image.sobel_edges(\n image\n )\n\n#### Example usage:\n\nFor general usage, `image` would be loaded from a file as below: \n\n image_bytes = tf.io.read_file(path_to_image_file)\n image = tf.image.decode_image(image_bytes)\n image = tf.cast(image, tf.float32)\n image = tf.expand_dims(image, 0)\n\nBut for demo purposes, we are using randomly generated values for `image`: \n\n image = tf.random.uniform(\n maxval=255, shape=[1, 28, 28, 3], dtype=tf.float32)\n sobel = tf.image.sobel_edges(image)\n sobel_y = np.asarray(sobel[0, :, :, :, 0]) # sobel in y-direction\n sobel_x = np.asarray(sobel[0, :, :, :, 1]) # sobel in x-direction\n\nFor displaying the sobel results, PIL's [Image Module](https://pillow.readthedocs.io/en/stable/reference/Image.html) can be used: \n\n # Display edge maps for the first channel (at index 0)\n Image.fromarray(sobel_y[..., 0] / 4 + 0.5).show()\n Image.fromarray(sobel_x[..., 0] / 4 + 0.5).show()\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------|------------------------------------------------------------------------------------------------------------------|\n| `image` | Image tensor with shape \\[batch_size, h, w, d\\] and type float32 or float64. The image(s) must be 2x2 or larger. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| Tensor holding edge maps for each channel. Returns a tensor with shape \\[batch_size, h, w, d, 2\\] where the last two dimensions hold \\[\\[dy\\[0\\], dx\\[0\\]\\], \\[dy\\[1\\], dx\\[1\\]\\], ..., \\[dy\\[d-1\\], dx\\[d-1\\]\\]\\] calculated using the Sobel filter. ||\n\n\u003cbr /\u003e"]]