Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.5:PIL Image.fromarray生成无意义图像_Python_Image_Matplotlib_Python Imaging Library - Fatal编程技术网

Python 3.5:PIL Image.fromarray生成无意义图像

Python 3.5:PIL Image.fromarray生成无意义图像,python,image,matplotlib,python-imaging-library,Python,Image,Matplotlib,Python Imaging Library,我有一个RGB图像。导入此图像时,我使用matplotlib.color将其转换为HSV,并将生成的数组保存在dict中。当我要显示此图像时,我使用image.fromarray和模式='HSV'。我不确定我做错了什么,但当图像显示时,我会弄得一团糟(见下面的代码)。感谢您的帮助。下面的代码片段大致描述了为处理任何给定的导入图像集而发生的情况 RGB至HSV代码: from skimage import io import matplotlib.colors as mpclr import g

我有一个RGB图像。导入此图像时,我使用
matplotlib.color
将其转换为HSV,并将生成的数组保存在dict中。当我要显示此图像时,我使用
image.fromarray
模式='HSV'
。我不确定我做错了什么,但当图像显示时,我会弄得一团糟(见下面的代码)。感谢您的帮助。下面的代码片段大致描述了为处理任何给定的导入图像集而发生的情况

RGB至HSV代码:

from skimage import io 
import matplotlib.colors as mpclr
import glob
import os 
from PIL import Image, ImageOps

types = ("\*.tif", "\*.jpg", "\*.ppm")
imagePath = []

def importAllImgs(folderPath):

    for ext in types:
        imagePath.extend(glob.glob(folderPath + ext))


    im_coll = io.ImageCollection(imagePath, conserve_memory = True)
    im_array = []    

    for i in range(len(im_coll)):
        #CONVERSION HAPPENS HERE
        image = im_coll[i]
        fltImg = np.around((np.array(image)/255.0), decimals = 2)
        imgHSV = mpclr.rgb_to_hsv(fltImg)

        im_array.append(imgHSV)  

    return im_array, imagePath
数据存储:

def organizeAllData(self, imgArrList, imgPathList):

    self.allImages = dict()            
    self.imageKeys = imgPathList

    for i in range(len(imgPathList)):

        self.allImages[imgPathList[i]] = {'H': imgArrList[i][:, :, 0],
                                            'S': imgArrList[i][:, :, 1],
                                            'V': imgArrList[i][:, :, 2]}


    self.hsvValues = []
    self.labelValues = [] 

    return self.allImages
用于显示图像的阵列的构造:

def getImage(self, imageOfInterest):

    H = self.allImages[imageOfInterest]['H'][:,:]
    S = self.allImages[imageOfInterest]['S'][:,:]
    V = self.allImages[imageOfInterest]['V'][:,:]

    imgArray = np.dstack((H,S,V))

    return imgArray
显示图像:

    preImArray = halThrThsnd.getImage(self.imagePaths[self.imageIndex])
    self.preIm = Image.fromarray(preImArray, 'HSV')
最后,生成的图像:


根据用户sascha的评论(见下面的问题),我决定对用于HSV转换的库进行规范化。一旦我这样做了,我得到了正常的图像没有问题。事实证明,根据用于图像转换的库,您将获得不同的HSV值范围。某些库将生成从0到1的范围。其他的将产生从0到255的范围


Tl;dr:在所有流程中使用相同的库,获得了良好的图像

根据用户sascha的评论(见下面的问题),我决定规范化用于HSV转换的库。一旦我这样做了,我得到了正常的图像没有问题。事实证明,根据用于图像转换的库,您将获得不同的HSV值范围。某些库将生成从0到1的范围。其他的将产生从0到255的范围


Tl;dr:在所有流程中使用相同的库,获得了良好的图像

为什么不在scikit映像中执行所有映像内容(因为您已经导入了它)?它更加专业化,所以出错的可能性更小。根据颜色尺寸和颜色格式,为所有这些操作(例如内部浮点或无符号整数)保留正确的类型可能会很麻烦。当然,它还可以进行颜色转换以及输出当前图像。为什么不在scikit图像中执行所有图像操作(因为您已经导入了它)?它更加专业化,所以出错的可能性更小。根据颜色尺寸和颜色格式,为所有这些操作(例如内部浮点或无符号整数)保留正确的类型可能会很麻烦。当然,它还可以进行颜色转换以及输出当前图像。