一、通道合并
通道合并就是将三个通道的图像合并在一起构成一幅图:
"""
bgr = cv2.merge([b,g,r]) #b通道图像b,g通道图像g,r通道图像r 合并为bgr彩色图像
"""
import cv2
h = cv2.imread('huajiu.jpg')
b,g,r = cv2.split(h)
bgr = cv2.merge([b,g,r])
gbr = cv2.merge([g,b,r])
rgb = cv2.merge([r,g,b])
cv2.imshow('bgr',bgr)
cv2.imshow('gbr',gbr)
cv2.imshow('rgb',rgb)
cv2.waitKey()
cv2.destroyAllWindows()
运行:
二、获取图像属性
shape: 彩色图像返回行、列、通道数的数组;灰度图像返回行数、列数。
size: 返回图像像素值数目,即:“ 行 x 列 x 通道数 ” ,灰度图的通道数为1 。
dtype: 返回图像的数据类型
import cv2
h = cv2.imread('huajiu.jpg')
print('h图像属性:')
print('h.size',h.size)
print('h.dtype',h.dtype)
print('h.shape',h.shape)
运行如下:
三、图像加法
使用 +
import numpy as np
img1 = np.random.randint(0,256,size=[3,3],dtype=np.uint8)
img2 = np.random.randint(0,256,size=[3,3],dtype=np.uint8)
print('img1=\n',img1)
print('img2=\n',img2)
print('img1+img2=\n',img1+img2)
使用cv2.add()
#计算结果=cv2.add(像素值a,像素值b)
import numpy as np
import cv2
img1 = np.random.randint(0,256,size=[3,3],dtype=np.uint8)
img2 = np.random.randint(0,256,size=[3,3],dtype=np.uint8)
print('img1= \n',img1)
print('img2= \n',img2)
img3 = cv2.add(img1,img2)
print('img3= \n',img3)
两者对比:
import cv2
a = cv2.imread('huajiu.jpg')
print("huajiu",a)
b = a
c = b+a
d = cv2.add(a,b)
print('a= \n',a)
cv2.imshow('a',a)
cv2.imshow('b',b)
cv2.imshow('c',c)
cv2.imshow('d',d)
cv2.waitKey()
cv2.destroyAllWindows()