5

This might be a simple question. I am just trying to do radon transform of an image and save it using functions in TensorFlow. But the result is not right. I know I can use plt.imsave() to save the image correctly, but I want to know how to do it in TensorFlow.

I am new to TensorFlow and thank you for your help.

This is the shepp-logan.jpg image I use. It is a grayscale image with size 64*64

This is the saved image

Here is my code.

from skimage.transform import radon,iradon
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np

sess = tf.Session()
img = plt.imread('shepp-logan.jpg')
theta = np.linspace(0., 180., max(img.shape), endpoint=False)
sinogram = radon(img, theta=theta, circle=True)
sinogram = tf.cast(sinogram, tf.uint8)
sinogram = tf.expand_dims(sinogram, -1)
sinogram = tf.image.encode_jpeg(sinogram, quality=100, format='grayscale')
writer = tf.write_file('test_sinogram.jpg', sinogram)
sess.run(writer)
f4.
  • 3,814
  • 1
  • 23
  • 30
ddxxdds
  • 373
  • 5
  • 13
  • Do you want to specifical save it or just view it and save in a tf-format? – Lau Jun 23 '18 at 20:58
  • @Lau It would be best if I can save it specifically. But how do I view it? – ddxxdds Jun 23 '18 at 21:34
  • You can view it with tensorbard (tf.summary.image). Its late, I'll write you tomorow a detailed answer ;) – Lau Jun 23 '18 at 21:51
  • please include a functional example next time, you were just missing two lines and some proper indentation ... https://stackoverflow.com/help/mcve – f4. Jun 24 '18 at 00:09

1 Answers1

8

The problem is that the function radon returns values way too high for tensorflow. Tensorflow wants values between 0 and 255 (uint8) per channel.

I didn't look why that is, but I did a quick test after looking at the values in sinogram and decided to divide by np.max(sinogram) and the result looks much closer to what you expect I believe :)

from skimage.transform import radon,iradon
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np

sess = tf.Session()
img = plt.imread('shepp-logan.jpg')
theta = np.linspace(0., 180., max(img.shape), endpoint=False)
sinogram = radon(img, theta=theta, circle=True)

# scaling the values here
sinogram = 255*sinogram/np.max(sinogram)

sinogram = tf.cast(sinogram, tf.uint8)
sinogram = tf.expand_dims(sinogram, -1)
sinogram = tf.image.encode_jpeg(sinogram, quality=100, format='grayscale')
writer = tf.write_file('test_sinogram.jpg', sinogram)
sess.run(writer)

As for tensorboard, which I recommend you use, you have to use tf.summary.image: https://www.tensorflow.org/api_docs/python/tf/summary/image

And here is a guide for tensorboard: https://www.tensorflow.org/programmers_guide/summaries_and_tensorboard

f4.
  • 3,814
  • 1
  • 23
  • 30