python保存输出图像_如何用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!

解决方案

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')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值