opencv学习笔记3

opencv学习笔记3

1 感兴趣区域(ROI)

img = cv2.imread('src\lena.tiff',0)
part_img = img[220:400,250:350]

2 通道的拆分

#索引的方式
b = img[:,:,0]
g = img[:,:,1]
r = img[:,:,2]
#函数的方式
b,g,r = cv2.split(img)

3 通道的合并

bgr = cv2.merge([b,g,r])
rgb = cv2.merge([r,g,b])

4 图像的属性

img.shape
img.size
img.dtype

5.图像的运算

#1加法
cv2.add(A,B)#A,B可为数字或者图像
A+B

6 灰度图像的各个位图像

import cv2
import numpy as np
lena = cv2.imread('src\lena.tiff',0)
cv2.imshow('lena',lena)
r,c = lena.shape
x = np.zeros((r,c,8),dtype=np.uint8)
for i in range(8):
    x[:,:,i] = 2**i
r = np.zeros((r,c,8),dtype=np.uint8)
for i in range(8):
    r[:,:,i] = cv2.bitwise_and(lena,x[:,:,i])
    mask = r[:,:,i]>0
    r[mask] = 255
    cv2.imshow(str(i),r[:,:,i])
cv2.waitKey()
cv2.destroyAllWindows()

7 图像按照异或运算实现加密和解密的过程

import cv2
import numpy as np
lena = cv2.imread('src\lena.tiff',0)
r,c = lena.shape
key = np.random.randint(0,255,size=(r,c),dtype=np.uint8)
encryption = cv2.bitwise_xor(lena,key)
decryption = cv2.bitwise_xor(encryption,key)
cv2.imshow('lena',lena)
cv2.imshow('key',key)
cv2.imshow('encryption',encryption)
cv2.imshow('decryption',decryption)
cv2.waitKey()
cv2.destroyAllWindows()

你可能感兴趣的:(opencv学习笔记,opencv,计算机视觉,python,cv)