In this post we will cover the topic of how to convert an image to a Tensor in TensorFlow. To convert an image to a tensor in TensorFlow we use tf.convert_to_tensor(). This function accepts a Python object of various types. In this post we will read the input image using Pillow and OpenCV and convert the image to a Tensor. The image is in [H, W, C] format, where H, W and C are the height, width and number of channels of the image. And the shape of the output tensor is also in [H, W, C] format. TensorFlow is a very rich library to work on Computer Vision or Image Processing related tasks.
Contents:
Prerequisites:
- Python
- TensorFlow
- Pillow
- and/ or OpenCV
Syntax:
tf.convert_to_tensor(value, dtype=None, dtype_hint=None, name=None)
- value: An object to be converted to Tensor.
- dtype: Data type of the returned tensor. It's optional and if it is missing, the type is inferred from the type of value.
- dtype_hint: Data type for the returned tensor, used when dtype is None. Its also an optional argument.
- name: Name to use if a new Tensor is created. It's also an optional argument.
Steps to Convert an Image to Tensor:
To convert an image to a Tensor in tensor flow, we can follow the below given steps:
- Import the required libraries. Here required libraries are TensorFlow, Pillow and OpenCV.
- Read the input image. Here we are taking a PIL image as an input image. The image is in [H, W, C] format, where H, W and C are the height, width and number of channels of the image.
- Convert the image to tensor using tf.convert_to_tensor().
- Print the tensor values.
Input Image:
image.jpg |
Example 1:
The Python3 code below shows how to convert a PIL image to Tensor using tf.convert_to_tensor(). The value is our PIL image and we are not providing other optional arguments. So using default dtype=None, dtype_hint=None, name=None.
# Import required librariesimport tensorflow as tffrom PIL import Image# Read a PIL imageimg = Image.open('image.jpg')print(img)# Convert the PIL image to Tensorimg_to_tensor = tf.convert_to_tensor(img)# print the converted Torch tensorprint(img_to_tensor)print("dtype of tensor:",img_to_tensor.dtype)
Output:
tf.Tensor( [[[218 225 235] [216 225 234] [216 225 232] ... [215 224 231] [215 224 231] [215 224 231]] [[217 224 234] [216 225 234] [216 225 232] ... [215 224 231] [215 224 231] [215 224 231]] [[217 224 234] [216 225 234] [216 225 232] ... [215 224 231] [215 224 231] [215 224 231]] ... [[126 120 96] [159 153 129] [153 147 123] ... [185 178 162] [184 177 161] [182 175 159]] [[112 105 79] [127 119 96] [139 131 108] ... [185 178 162] [183 176 158] [182 175 157]] [[153 146 120] [112 104 81] [102 94 71] ... [184 177 161] [183 176 158] [182 175 157]]], shape=(450, 650, 3), dtype=uint8) dtype of tensor: <dtype: 'uint8'>
Notice that the data type of the output tensor is uint8 and the values are in range [0,255]. The shape of the image tensor is [450, 650, 3].
Example 2:
In this example we read an RGB image using OpenCV. We convert it to torch tensor using tf.convert_to_tensor().
The value is our RGB image and we are providing the optional argument dtype=tf.float32. Using other arguments as default , dtype_hint=None, name=None.
# Import required librariesimport tensorflow as tfimport cv2# Read the imageimage = cv2.imread('lion.jpg')# Convert BGR image to RGB imageimage = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)# Convert the image to Tensorimg_to_tensor = tf.convert_to_tensor(img, dtype=tf.float32)# print the converted Torch tensorprint(img_to_tensor)print("dtype of tensor:",img_to_tensor.dtype)
Output:
tf.Tensor( [[[218. 225. 235.] [216. 225. 234.] [216. 225. 232.] ... [215. 224. 231.] [215. 224. 231.] [215. 224. 231.]] [[217. 224. 234.] [216. 225. 234.] [216. 225. 232.] ... [215. 224. 231.] [215. 224. 231.] [215. 224. 231.]] [[217. 224. 234.] [216. 225. 234.] [216. 225. 232.] ... [215. 224. 231.] [215. 224. 231.] [215. 224. 231.]] ... [[126. 120. 96.] [159. 153. 129.] [153. 147. 123.] ... [185. 178. 162.] [184. 177. 161.] [182. 175. 159.]] [[112. 105. 79.] [127. 119. 96.] [139. 131. 108.] ... [185. 178. 162.] [183. 176. 158.] [182. 175. 157.]] [[153. 146. 120.] [112. 104. 81.] [102. 94. 71.] ... [184. 177. 161.] [183. 176. 158.] [182. 175. 157.]]], shape=(450, 650, 3), dtype=float32) dtype of tensor: <dtype: 'float32'>
Notice that the data type of the output tensor is tf.float32 and the values are in range [0, 255]. The shape of the image tensor is [450, 650, 3].
References:
Useful Resources:
Next Post: How to Convert an Image to a Tensor in TensorFlow
Previous Post: How to Draw Rectangle on An Image using OpenCV in Python
Comments
Post a Comment