Reputation: 947
Is there a proper way to convert a cvimg to tensor properly without causing any color distortions? I have done a comparison by storing 2 images. One was decoded using tensorflow and the other was done using openCV
Image generated using tensorflow jpeg encoder
file_reader = tf.read_file(file_name, input_name)
if file_name.endswith(".png"):
image_reader = tf.image.decode_png(
file_reader, channels=3, name="png_reader")
elif file_name.endswith(".gif"):
image_reader = tf.squeeze(
tf.image.decode_gif(file_reader, name="gif_reader"))
elif file_name.endswith(".bmp"):
image_reader = tf.image.decode_bmp(file_reader, name="bmp_reader")
else:
image_reader = tf.image.decode_jpeg(
file_reader, channels=3, name="jpeg_reader")
Image generated using cv to tensor convert.
image_reader = tf.convert_to_tensor(cvimg)
Am i missing some steps here during the cv conversion? Thanks!
Upvotes: 0
Views: 1575
Reputation: 51
Another way is:
image_reader=tf.reverse(cvimg,axis=[-1])
tf.reverse
will reverse the order that you specified dimension.because the cvimg(opencv image,[height,width,channel])
is BGR format,
so tf.reverse(cvimg,axis=[-1])
will reverse BGR(the last dimension) to RGB,
you don't have to use tf.convert_to_tensor(cvimg)
, because tensorfow will automatically convert from numpy array to tensor.
Upvotes: 0
Reputation: 27042
OpenCV loads images in the BGR format while Tensorflow uses the RGB format (as you can see the blue and red channels of your image are swapped).
Thus, if you want to read an image loaded using OpenCV (i suppose cvimg
) you just have to swap the color channels from BGR to RGB:
image_reader = tf.convert_to_tensor(cvimg)
b, g, r = tf.unstack (image_reader, axis=-1)
image_reader = tf.stack([r, g, b], axis=-1)
Upvotes: 2