标题opencv 图片BGR分层处理
教程有些东西没有写得很明细,下面是我学习过程的一些补充。
直接上代码:
import cv2 as cv
import numpy as np
import random
import time
#创建黑色窗口 长 500 宽 500 3层色
img = np.zeros([500,500,3],np.uint8)
#创建白色窗口 长 500 宽 500 3层色
img_w = np.zeros([500,500,3],np.uint8)
img_w[:,:,:] = np.zeros([500,500,3],np.uint8)+[255,255,255]
#创建白色窗口 长 500 宽 500 3层色 注意 np.zeros 改成 np.ones
img_w1 = np.ones([500,500,3],np.uint8)
img_w1[:,:,:] = np.ones([500,500,3],np.uint8)*255
#创建蓝色窗口 长 500 宽 500 3层色 注意第二行没有 3 因为是 BGR 排序 B蓝排在第一层 0
img_b = np.zeros([500,500,3],np.uint8)
img_b[:,:,0] = np.zeros([500,500],np.uint8)+255# 0 层 所以是单层,3不能再出现。下面G,R层是一样的道理
#创建绿色窗口 长 500 宽 500 3层色
img_g = np.zeros([500,500,3],np.uint8)
#创建红色窗口 长 500 宽 500 3层色
img_r = np.zeros([500,500,3],np.uint8)
img_r[:,:,2] = np.zeros([500,500],np.uint8)+255
while True:
x = random.randint(1,255)
y = random.randint(1,255)
z = random.randint(1,255)
img_w_b = np.ones([500,500],np.uint8)*x # 如果写成np.ones([500,500],3)结果是一样的
img_g[:,:,1] = np.zeros([500,500],np.uint8)+y
cv.imshow('img_w_b',img_w_b)
cv.imshow('img',img)
cv.imshow('img_w',img_w)
cv.imshow('img_w1',img_w1)
cv.imshow('img_b',img_b)
cv.imshow('img_g',img_g)
cv.imshow('img_r',img_r)
time.sleep(1)
if cv.waitKey(1) == ord('q'):
break
如有不足请补充,如有不对的请更正。