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()