2014-09-25 279 views
1

我刚刚开始在Python中使用OpenCV,并试图做一些简单的事情。首先,我试图创建一个稳定的蓝色图像(或可能是红色,如果图像变成RGB,而不是BGR)。我期待从python-openCV出现蓝色(红色?)图像,但变黑了

我试过如下:

import numpy as np 
import cv2 

img1 = np.zeros((512,512,3), np.uint8) #Create black image 
img1[0,:,:] = 200 #Add intenstity to blue (red?) plane 
print img1 #Verify image array 
cv2.imshow("II",img1) 
cv2.waitKey(0) 
cv2.destroyAllWindows() 
for _ in range (1,5): 
    cv2.waitKey(1) 

但是,我得到的是一个黑色的图像。我相当肯定的阵列正确的,因为打印语句给我的以下内容:

[[[200 200 200] 
    [200 200 200] 
    [200 200 200] 
    ..., 
    [200 200 200] 
    [200 200 200] 
    [200 200 200]] 

[[ 0 0 0] 
    [ 0 0 0] 
    [ 0 0 0] 
    ..., 
    [ 0 0 0] 
    [ 0 0 0] 
    [ 0 0 0]] 

[[ 0 0 0] 
    [ 0 0 0] 
    [ 0 0 0] 
    ..., 
    [ 0 0 0] 
    [ 0 0 0] 
    [ 0 0 0]] 

    ..., 
[[ 0 0 0] 
    [ 0 0 0] 
    [ 0 0 0] 
    ..., 
    [ 0 0 0] 
    [ 0 0 0] 
    [ 0 0 0]] 

[[ 0 0 0] 
    [ 0 0 0] 
    [ 0 0 0] 
    ..., 
    [ 0 0 0] 
    [ 0 0 0] 
    [ 0 0 0]] 

[[ 0 0 0] 
    [ 0 0 0] 
    [ 0 0 0] 
    ..., 
    [ 0 0 0] 
    [ 0 0 0] 
    [ 0 0 0]]] 

是否作出这样的我看到的,而不是蓝色(或红色?)像一个黑色的意义吗?

回答

3

什么你”重新做的是改变颜色0th row。相反,您需要更改第一个或第零个通道的值。

img[:, :, 0] = 255 

这将改变所有的第一或第0信道〜255的这将使您蓝色图像,因为它是一个BGR图像的值。

+0

谢谢......我现在感觉有点傻。 – user1245262 2014-09-25 11:39:29

+0

难道我们都在某个点或其他? :) – Froyo 2014-09-25 12:45:08

3

您需要将颜色指定为元组!如果你想有一个RGB图像,因为在阵列非常指数是一个像素,你需要为B,G,R 3值(OpenCV中设定的像素为BGR

import numpy as np 
import cv2 

img1 = np.zeros((512,512,3), np.uint8) #Create black image 
img1[:,:] = (255,0,0) #Add intenstity to blue (red?) plane 
print img1 #Verify image array 
cv2.imshow("II",img1,) 
cv2.waitKey(0) 
cv2.destroyAllWindows() 
for _ in range (1,5): 
    cv2.waitKey(1) 

结果:

enter image description here

+0

请注意,opencv假设像素存储为BGR而不是RGB – remi 2014-09-25 08:55:24

+0

是的,谢谢提醒! – Kasramvd 2014-09-25 08:58:41

+0

谢谢,我还没有意识到我可以分配一个这样的数组维度。 – user1245262 2014-09-25 11:44:05

相关问题