# coding=UTF-8
import cv2 as cv
import numpy as np
def create_image():
img = np.zeros([400,400,3],np.uint8)
img[:,:,0] = 1*255 #img的第三个参数为0,蓝;为1,绿;为2,红
cv.imshow("new image",img)
create_image()
cv.waitKey()
cv.destroyAllWindows()
效果如下:
生成了一张400*400像素的蓝色图像。(这就能解释为什么叫BGR图像)
①np.zeros (shape, dtype, order)
shape: 可以是任意维数。
例如:
shape = [m , a, b] 表示生成m个ab的0矩阵;
shape = [a,b]表示生成1个ab的0矩阵;
shape = a或shape = [a] 表示生成1个1*a的0矩阵。
dtype: 可选参数,默认numpy.float64。
order: 可选参数,'C’代表行优先;'F’代表列优先。
np.ones()同理,只是生成全1矩阵。
②img[:,:,0] = 1*255
这边用到了python中的切片处理。
将所有400个矩阵的第1列变成了255,而按照BGR,第一列为B(blue),所以显示出的图像就是蓝色的。