为什么skimage.imread()不为我的bmp返回RGB值?

时间:2014-04-30 01:20:10

标签: python rgb bmp

我正在尝试从bmp图像切片块以用于图像关联,但是,当我从skimage.imread()返回的数组中取一个平面时,而不是获取红色平面或绿色平面,我得到奇怪的颜色,好像原始数据是hsl。

我尝试使用PIL将图像转换为RGB,但颜色变得更糟......

谁能告诉我发生了什么?

我确定我的问题需要更多信息,所以请告诉我需要添加的内容,谢谢!

编辑:

from skimage import data
full=data.imread("Cam_1.bmp")
green_template = full[144:194,297:347,1]    #Both give me a sort of reddish square
red_template = full[145:195,252:302,0]

FWIW如果skimage.match_template会拍摄彩色图像,我就不会有这个问题......是否有相关库可以做颜色?

以下是我正在使用的图片:enter image description here

当我显示使用上面代码制作的小作物时,会出现以下结果:enter image description here

在使用PIL打开后使用full = numpy.array(image)会产生相同的结果。

1 个答案:

答案 0 :(得分:1)

好的,我想出来了(在朋友的帮助下)。

我不完全理解为什么我的照片显示得如此奇怪,但我确实知道为什么他们没有显示为红色和绿色的飞机。

green_template = full[144:194,297:347,1]
red_template = full[145:195,252:302,0]

这些从最终的子阵列中取出一个切片,分别是g和r值。如果我想正确显示它们,我应该做的是使用green_template和red_template作为相应的g和r值创建一个新图像,并在其他地方创建零,例如使它回到一个形状(宽度,高度,3)的数组。 例如:

import Image
import numpy as np

im = Image.open('Cam_1.bmp')
#im.show()
r,g,b = im.split()

y = Image.fromarray(np.zeros(im.size[0]*im.size[1]).reshape(im.size[1],im.size[0]).astype('float')).convert("L")

red = Image.merge("RGB",(r,y,y))

green = Image.merge("RGB",(y,g,y))

如果我使用原始图像执行此操作,则会产生以下图像:

enter image description here

然而,我最初的问题是skimage的match_template只采用2D数组。所以,事实证明,我一直都有正确的数组,我只是没有意识到它们是正确的,因为显示它们会导致你在问题中的图像中看到的奇怪颜色。如果有人知道为什么python在显示2D图像时会做出奇怪的事情,我想知道。否则,我已经解决了我的问题。感谢任何试图提供帮助的人,无论你是张贴还是只是自己尝试过的东西!

编辑 - 请求的图像呈现代码:

def locate_squares(im):
    r,g,b = im.split()
    red = np.array(r)
    green = np.array(g)
    green_template = green[144:194,297:347]  #,144:194]
    gRadius = (green_template.shape[0]/2, green_template.shape[1]/2)
    red_template = red[145:195,252:302]    #,145:195]
    rRadius = (red_template.shape[0]/2, red_template.shape[1]/2)
    #print red_template

    plt.figure(1)
    plt.subplot(1,2,1)
    plt.imshow(green_template)
    plt.subplot(1,2,2)
    plt.imshow(red_template)

    plt.figure(2)
    plt.subplot(1,2,1)
    plt.imshow(green)
    plt.subplot(1,2,2)
    plt.imshow(red)

    plt.show()
相关问题