Basic operations in opencv: and or not, XOR and adding watermark (implemented in Python)
編輯:Python
List of articles
1. Non operation of image
2. And operation of image
3. To perform or operate on images
4. Exclusive or operation
5. Add watermark
1. Non operation of image
def FEI():
# Create a picture of the same size
npimg = np.ones(shape=(450,450), dtype=np.uint8)
npimg[100:300,100:300]=255
dst=cv2.bitwise_not(src=npimg)
cv2.imshow('npimg',npimg)
cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
2. And operation of image
def And():
# Create a picture of the same size
npimg1 = np.ones(shape=(450,450), dtype=np.uint8)
npimg1[100:300,100:300]=255
npimg2 = np.ones(shape=(450, 450), dtype=np.uint8)
npimg2[50:150, 50:150] = 255
dst=cv2.bitwise_and(src1=npimg1,src2=npimg2)
cv2.imshow('npimg1', npimg1)
cv2.imshow('npimg2', npimg2)
cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
3. To perform or operate on images
def OR():
# Create a picture of the same size
npimg1 = np.ones(shape=(450, 450), dtype=np.uint8)
npimg1[100:300, 100:300] = 255
npimg2 = np.ones(shape=(450, 450), dtype=np.uint8)
npimg2[50:150, 50:150] = 255
dst=cv2.bitwise_or(src1=npimg1,src2=npimg2)
cv2.imshow('npimg1', npimg1)
cv2.imshow('npimg2', npimg2)
cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
4. Exclusive or operation
def Exotic():
# Create a picture of the same size
npimg1 = np.ones(shape=(450, 450), dtype=np.uint8)
npimg1[100:300, 100:300] = 255
npimg2 = np.ones(shape=(450, 450), dtype=np.uint8)
npimg2[50:150, 50:150] = 255
dst=cv2.bitwise_xor(src1=npimg1,src2=npimg2)
cv2.imshow('npimg1', npimg1)
cv2.imshow('npimg2', npimg2)
cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
5. Add watermark
import os
import cv2
import numpy as np
# Read the picture
img=cv2.imread('images/lenna.png')
img=cv2.resize(src=img,dsize=(450,450))
# establish LOGO
logo=np.zeros(shape=(350,350,3),dtype=np.uint8)
# Create a mask
mask=np.zeros(shape=(350,350),dtype=np.uint8)
# Drawing graphics
logo[100:300,100:300]=[0,255,0]
logo[50:250,50:250]=[0,0,255]
mask[100:300,100:300]=255
mask[50:250,50:250]=255
# Yes mask Inverse
mask_not=cv2.bitwise_not(src=mask)
# Choose an image lenna Add logo
roi=img[0:350,0:350]
#roi And mask_not Conduct “ And ” operation
temp=cv2.bitwise_and(src1=roi,src2=roi,mask=mask_not)
# superposition
dst=cv2.add(src1=temp,src2=logo)
img[0:350,0:350]=dst
cv2.imshow('img',img)
# cv2.imshow('logo',logo)
# cv2.imshow('mask',mask)
# cv2.imshow('mask_not',mask_not)
# cv2.imshow('temp',temp)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
print('Pycharm')