文章目錄
- 1.圖像的非運算
- 2.圖像的與運算
- 3.圖像進行或操作
- 4.異或操作
- 5.添加水印
1.圖像的非運算
def FEI():
# 創建相同尺寸的圖片
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.圖像的與運算
def And():
# 創建相同尺寸的圖片
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.圖像進行或操作
def OR():
# 創建相同尺寸的圖片
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.異或操作
def Exotic():
# 創建相同尺寸的圖片
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.添加水印
import os
import cv2
import numpy as np
#讀取圖片
img=cv2.imread('images/lenna.png')
img=cv2.resize(src=img,dsize=(450,450))
#創建LOGO
logo=np.zeros(shape=(350,350,3),dtype=np.uint8)
#創建掩碼
mask=np.zeros(shape=(350,350),dtype=np.uint8)
#繪制圖形
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
#對mask求反
mask_not=cv2.bitwise_not(src=mask)
#選擇圖像lenna的位置添加logo
roi=img[0:350,0:350]
#roi與mask_not進行“與”操作
temp=cv2.bitwise_and(src1=roi,src2=roi,mask=mask_not)
#疊加
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')