解决:Clipping input data to the valid range for imshow with RGB data

Clipping input data to the valid range for imshow with RGB data

今天在提取彩色图像RGB通道值合成单通道图像时,出现问题:

Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

先给出原因:

matplotlib.pyplot.imshow()函数在处理灰度图像时,自动将其值做归一化处理

而在处理彩色图像时则不会,而是将浮点值变换至[0,1],整数值变换到[0, 255]范围

上代码:

def slic_image(image_path, block_number, compactness, sigma):

    image = cv2.imread(image_path)
    image_depth = image.shape[2]

    if image_depth == 3:
        image_r, image_g, image_b = cv2.split(image)

        image_r = image_r.astype('float32')
        image_g = image_g.astype('float32')
        image_b = image_b.astype('float32')

        syn_image_g = syn_single_channel_image(sig_channel_image=image_g, channel_name="g")

        plt.imshow(syn_image_g)
        plt.show()



        
def syn_single_channel_image(sig_channel_image, channel_name,):

        image_height = sig_channel_image.shape[0]
        image_width = sig_channel_image.shape[1]

        b = np.empty(shape=(image_height, image_width), dtype="float32")
        g = np.empty(shape=(image_height, image_width), dtype="float32")
        r = np.empty(shape=(image_height, image_width), dtype="float32")
        b[:][:] = 0
        g[:][:] = 0
        r[:][:] = 0

        synthesis = [r, g, b]

        if channel_name == 'r':
            synthesis = [sig_channel_image, g, b]
        elif channel_name == 'g':
            synthesis = [r, sig_channel_image, r]
        elif channel_name == 'b':
            synthesis = [r, g, sig_channel_image]

        synthesis_image = cv2.merge(synthesis)

        return synthesis_image

      slic_image(image_path='test.png', block_number=30, compactness=10, sigma=5)

简单描述大概就是:将一张图像的三个通道信息分别抽取出来,与另外两个大小相等的0数组来合成单色图像。

这里我将抽取出的每个通道的信息都转为float32,原目的的为了更好的保留图像的信息(后来发现使用cv2.imread( )函数读取的图像,其像素值类型本就为uint8,完全没有必要这么做)

最后合成的结果自然是三个通道的数据类型全部是float32,在调用plt.imshow( )函数的时候全被调整到了[0, 1]范围内,最后导致生成了几乎全是绿色的图像:

image-20211215221449450

​ 原图与合成图像的显示对比

解决方法

​ 最终我将数组中的数据类型全部定义为uint8,这样就正常了。

结果:

image-20211215221728070

今天在提取彩色图像RGB通道值合成单通道图像时,出现问题:

Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

先给出原因:

matplotlib.pyplot.imshow()函数在处理灰度图像时,自动将其值做归一化处理

而在处理彩色图像时则不会,而是将浮点值变换至[0,1],整数值变换到[0, 255]范围

上代码:

def slic_image(image_path, block_number, compactness, sigma):

    image = cv2.imread(image_path)
    image_depth = image.shape[2]

    if image_depth == 3:
        image_r, image_g, image_b = cv2.split(image)

        image_r = image_r.astype('float32')
        image_g = image_g.astype('float32')
        image_b = image_b.astype('float32')

        syn_image_g = syn_single_channel_image(sig_channel_image=image_g, channel_name="g")

        plt.imshow(syn_image_g)
        plt.show()



        
def syn_single_channel_image(sig_channel_image, channel_name,):

        image_height = sig_channel_image.shape[0]
        image_width = sig_channel_image.shape[1]

        b = np.empty(shape=(image_height, image_width), dtype="float32")
        g = np.empty(shape=(image_height, image_width), dtype="float32")
        r = np.empty(shape=(image_height, image_width), dtype="float32")
        b[:][:] = 0
        g[:][:] = 0
        r[:][:] = 0

        synthesis = [r, g, b]

        if channel_name == 'r':
            synthesis = [sig_channel_image, g, b]
        elif channel_name == 'g':
            synthesis = [r, sig_channel_image, r]
        elif channel_name == 'b':
            synthesis = [r, g, sig_channel_image]

        synthesis_image = cv2.merge(synthesis)

        return synthesis_image

      slic_image(image_path='test.png', block_number=30, compactness=10, sigma=5)

简单描述大概就是:将一张图像的三个通道信息分别抽取出来,与另外两个大小相等的0数组来合成单色图像。

这里我将抽取出的每个通道的信息都转为float32,原目的的为了更好的保留图像的信息(后来发现使用cv2.imread( )函数读取的图像,其像素值类型本就为uint8,完全没有必要这么做)

最后合成的结果自然是三个通道的数据类型全部是float32,在调用plt.imshow( )函数的时候全被调整到了[0, 1]范围内,最后导致生成了几乎全是绿色的图像:

image-20211215221449450

​ 原图与合成图像的显示对比

解决方法

​ 最终我将数组中的数据类型全部定义为uint8,这样就正常了。

结果:

image-20211215221728070

物联沃分享整理
物联沃-IOTWORD物联网 » 解决:Clipping input data to the valid range for imshow with RGB data

发表评论