pyplot.imsave()可以正确保存图像,但是cv2.imwrite()可以将相同图像保存为黑色 [英] pyplot.imsave() saves image correctly but cv2.imwrite() saved the same image as black

查看:133
本文介绍了pyplot.imsave()可以正确保存图像,但是cv2.imwrite()可以将相同图像保存为黑色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

from scipy.misc import imread
from matplotlib import pyplot

import cv2
from cv2 import cv

from SRM import SRM ## Module for Statistical Regional Segmentation

im = imread("lena.png") 
im2 = cv2.imread("lena.png")
print type(im), type(im2), im.shape, im2.shape 
## Prints <type 'numpy.ndarray'> <type 'numpy.ndarray'> (120, 120, 3) (120, 120, 3)

srm = SRM(im, 256)
segmented = srm.run()

srm2 = SRM(im2, 256)
segmented2 = srm2.run()

pic = segmented/256
pic2 = segmented2/256

pyplot.imshow(pic)
pyplot.imsave("onePic.jpg", pic)

pic = pic.astype('uint8')
cv2.imwrite("onePic2.jpg", pic2)

pyplot.show()

onePic.jpg 提供正确的分割图像,而 onePic2.jpg 提供完整的黑色图像.使用 pic = pic.astype('uint8')将数据类型转换为 uint8 并没有帮助.我仍然给出黑色图像!

onePic.jpg gives the correct segmented image but onePic2.jpg gives a complete black image. Converting the datatype to uint8 using pic = pic.astype('uint8') did not help. I still gives a black image!

onePic.jpg使用 pyplot.imsave():

onePic.jpg using pyplot.imsave():

onePic2.jpg使用 cv2.imwrite():

onePic2.jpg using cv2.imwrite():

请帮助!

推荐答案

在将 pic 转换为 uint8 之前,需要将其乘以255以获取正确的范围.

Before converting pic to uint8, you need to multiply it by 255 to get the correct range.

这篇关于pyplot.imsave()可以正确保存图像,但是cv2.imwrite()可以将相同图像保存为黑色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆