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.x 使用PIL而不是imagio库读取图像时图像显示不正确_Python 3.x_Image_Image Processing_Python Imaging Library_Python Imageio - Fatal编程技术网

Python 3.x 使用PIL而不是imagio库读取图像时图像显示不正确

Python 3.x 使用PIL而不是imagio库读取图像时图像显示不正确,python-3.x,image,image-processing,python-imaging-library,python-imageio,Python 3.x,Image,Image Processing,Python Imaging Library,Python Imageio,我有一个将图像从RGB空间转换为HSI空间的代码: import numpy as np import imageio from matplotlib import pyplot as plt def RGB_TO_HSI(img): with np.errstate(divide='ignore', invalid='ignore'): rgb = np.float32(img) / 255 # Separate color channels

我有一个将图像从RGB空间转换为HSI空间的代码:

import numpy as np
import imageio
from matplotlib import pyplot as plt


def RGB_TO_HSI(img):
    with np.errstate(divide='ignore', invalid='ignore'):

        rgb = np.float32(img) / 255

        # Separate color channels
        red = rgb[:, :, 0]
        green = rgb[:, :, 1]
        blue = rgb[:, :, 2]

        # Calculate Intensity
        def calc_intensity(red, green, blue):
            intensity = (red + green + blue + 0.001) / 3
            return intensity

        # Calculate Saturation
        def calc_saturation(red, green, blue):
            minimum = np.minimum(np.minimum(red, green), blue)
            saturation = 1 - (minimum / calc_intensity(red, green, blue))
            return saturation

        # Calculate Hue
        def calc_hue(red, green, blue):
            hue = np.copy(red)  # Basically have our hue = red for now; we only need its size/dimensions

            for i in range(0, blue.shape[0]):
                for j in range(0, blue.shape[1]):

                    if blue[i][j] <= green[i][j]:
                        hue[i][j] = np.arccos(0.5 * ((red[i][j] - green[i][j]) + (red[i][j] - blue[i][j])) / (np.sqrt((red[i][j] - green[i][j]) ** 2 + (red[i][j] - blue[i][j]) * (green[i][j] - blue[i][j]))))
                    else:
                        hue[i][j] = 2 * np.pi - np.arccos(0.5 * ((red[i][j] - green[i][j]) + (red[i][j] - blue[i][j])) / (np.sqrt((red[i][j] - green[i][j]) ** 2 + (red[i][j] - blue[i][j]) * (green[i][j] - blue[i][j]))))
            return hue

    # Merge channels into picture and return image
    hsi = np.zeros(img.shape)  # instead of having 3 channels, one for each color (RGB), here we have a channel; for "hue", another for "saturation" and another for "intensity"
    hsi[:, :, 0], hsi[:, :, 1], hsi[:, :, 2] = calc_hue(red, green, blue), calc_saturation(red, green, blue), calc_intensity(red, green, blue)
    return hsi

cat = imageio.imread("Path...")
hsi_cat = RGB_TO_HSI(cat)
plt.imshow(hsi_cat)
plt.show()

这个结果是一个黑色的图像,我不太清楚为什么

函数
Image.fromarray
仅支持有限的典型范围。如果您只想将数据正确地渲染为图像,那么最好将HSI转换回RGB值,然后将其输出。大概

cat_-hsi=RGB_至_-hsi(cat)
结果=修改(cat_hsi)#我想您希望对hsi而不是rgb进行操作
结果\u rgb=HSI\u至\u rgb(结果)
img=Image.fromarray(结果\u rgb,模式='rgb')
img.show()

此外,我对代码做了一些修改,使其在我的机器上运行

  • 确保将带有形状(X,Y,3)的RGB数组传递给函数
  • calc\u hue
    函数中显式处理可能的零除法情况
将numpy导入为np
从numba import njit开始,代码速度明显加快
从matplotlib导入pyplot作为plt
从PIL导入图像
def RGB_至_HSI(img):
rgb=np.32(img)/255
红色=rgb[:,:,0]
绿色=rgb[:,:,1]
蓝色=rgb[:,:,2]
def钙浓度(红色、绿色、蓝色):
强度=(红色+绿色+蓝色+0.001)/3
回归强度
def钙饱和度(红色、绿色、蓝色):
最小值=np最小值(np最小值(红色、绿色)、蓝色)
饱和度=1-(最小/计算强度(红、绿、蓝))
回波饱和
@njit#使用numba加速
def calc_色调(红色、绿色、蓝色):
色调=np.复制(红色)
对于范围(0,蓝色。形状[0])中的i:
对于范围(0,蓝色形状[1])中的j:
分母=np.sqrt(
(红色[i][j]-绿色[i][j])**2+(红色[i][j]-蓝色[i][j])*(绿色[i][j]-蓝色[i][j])
)
如果abs(分母)<1e-10:#妥善处理可能的零除法情况
色调[i][j]=np.nan
其他:

如果说blue[i][j]哇,numba真的很神奇,它让代码更快了!然而,当使用PIL而不是imageio打开图像时,会出现黑色图像的情况;这方面的代码在问题的最后一部分。嗨,我用
PIL
更新了答案。一个可能的问题是,在处理png文件时,数据的格式是RGBA而不是RGB,因此我必须确保我只为函数提供RGB通道。我认为当使用“image.fromarray()”将hsi_cat从数组转换为图像时,问题会出现。即使仅确保RGB通道,如果将“plt.imshow(hsi_cat)”替换为“hsi_cat=Image.fromarray(hsi_cat)”,则“hsi_cat.show()”会得到黑色图像。我认为不支持色调饱和度强度的数据格式。我猜要显示图像,你必须将其转换为a。如果我想显示图像,我会将所有内容转换回RGB
cat_p = Image.open("Path...")  # Using PIL now
his_cat_p = RGB_TO_HSI(cat_p)  # Passing the JPEG image into the function
his_cat_p = Image.fromarray(his_cat_p)  # Converting the result back from an array (return hsi), into JPEG format
his_cat_p.show()  # Black image appears