我刚开始使用OpenCV和Python,我正在尝试做一些简单的事情。首先,我尝试创建一个纯蓝色图像(或者可能是红色,如果图像变成RGB,而不是BGR)。
我尝试了以下内容:
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)
但是,我得到的是黑色图像。我相当确定数组是正确的,因为print语句给了我以下内容:
[[[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]]]
我看到黑色而不是蓝色(或红色?)图像是否有意义?
答案 0 :(得分: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)
结果:
答案 1 :(得分:3)
您正在做的是改变0th row
的颜色。相反,您需要更改第一个或第0个通道的值。
img[:, :, 0] = 255
这会将第一个或第0个通道的所有值更改为255,这样可以获得蓝色图像,因为它是BGR图像。