Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are images actually saved with skimage (Python)

I am currently applying the Zhang-Suen thinning algorithm to hone down on some filaments I would like to later track. This requires me to output a grayscale image in order to identify objects using OpenCV.

import matplotlib
import matplotlib.pyplot as plt
import skimage.io as io
"load image data"
Img_Original =  io.imread( './data/test1.bmp')      # Gray image, rgb images   need pre-conversion

"Convert gray images to binary images using Otsu's method"
from skimage.filter import threshold_otsu
Otsu_Threshold = threshold_otsu(Img_Original)   
BW_Original = Img_Original < Otsu_Threshold    # must set object region as 1, background region as 0 !

#...
"Apply the algorithm on images"
BW_Skeleton = zhangSuen(BW_Original)
# BW_Skeleton = BW_Original
"Display the results"
fig, ax = plt.subplots(1, 2)
ax1, ax2 = ax.ravel()
ax1.imshow(BW_Original, cmap=plt.cm.gray)
ax1.set_title('Original binary image')
ax1.axis('off')
ax2.imshow(BW_Skeleton, cmap=plt.cm.gray)
ax2.set_title('Skeleton of the image')
ax2.axis('off')
plt.show()

The image plotted using matplotlib is exactly what I want (black and white). When I use either skimage or cv2 to write the output image to a file path, I get a similar image in blue and red. My only problem is that I can't convert this blue/red image to grayscale! So in essence, my output image is useless. Forgive me if this is a trivial question, but is there a protocol for writing images to file paths? What should I be looking out for in terms of image types (ie. bytes, colour/grayscale, format) when I use these tools? Thanks in advance!

like image 940
batlike Avatar asked Jun 19 '15 14:06

batlike


1 Answers

You can set the color map of the saved data so that when you open the image in OpenCV it will be in grey scale.

Here is some sample data as an example:

data = np.random.rand(256,256)

You can save the data directly:

plt.imsave('test.png', data, cmap = plt.cm.gray)

or save the entire figure:

plt.savefig('test2.png')
like image 54
Molly Avatar answered Sep 20 '22 01:09

Molly