文章目录
图像的加法运算
python
# -*- coding: utf-8 -*-
import cv2
import numpy as np
img = cv2.imread('E://pic//4.jpg')
# 图的加法运算就是矩阵的加法运算
# 因此,加法运算的两张图必须是相同的形状
# print(img.shape)
# (600, 480, 3)
img2 = np.ones((600, 480, 3), np.uint8) * 100
cv2.imshow('origin', img)
res = cv2.add(img, img2)
cv2.imshow('result', res)
key = cv2.waitKey(0) & 0xff
if key == ord('q'):
cv2.destroyAllWindows()
图像的减法运算
python
# -*- coding: utf-8 -*-
import cv2
import numpy as np
img = cv2.imread('E://pic//4.jpg')
# 图的加法运算就是矩阵的加法运算
# 因此,加法运算的两张图必须是相同的形状
# print(img.shape)
# (600, 480, 3)
img2 = np.ones((600, 480, 3), np.uint8) * 100
cv2.imshow('origin', img)
res = cv2.add(img, img2)
cv2.imshow('result', res)
img3 = cv2.subtract(res, img2)
cv2.imshow('origin2', img3)
key = cv2.waitKey(0) & 0xff
if key == ord('q'):
cv2.destroyAllWindows()
python
# -*- coding: utf-8 -*-
import cv2
import numpy as np
img = cv2.imread('E://pic//4.jpg')
# 图的加法运算就是矩阵的加法运算
# 因此,加法运算的两张图必须是相同的形状
# print(img.shape)
# (600, 480, 3)
img2 = np.ones((600, 480, 3), np.uint8) * 100
cv2.imshow('origin', img)
res = cv2.add(img, img2)
cv2.imshow('result', res)
img3 = cv2.subtract(res, img2)
cv2.imshow('origin2', img3)
img3 = cv2.subtract(img, img2)
cv2.imshow('origin3', img3)
key = cv2.waitKey(0) & 0xff
if key == ord('q'):
cv2.destroyAllWindows()
可以看出,给一张图做加法运算,它可以变亮一些,做减法运算,可以变暗一些
图像的乘除运算
无非是让图片亮得更快一些和亮得更慢一些
图像的融合
python
# -*- coding: utf-8 -*-
import cv2
import numpy as np
cv2.namedWindow('img', cv2.WINDOW_NORMAL)
back = cv2.imread('E://pic//Nurburgring_1920x1080.jpg')
superman = cv2.imread('E://pic//Man_Of_Steel_superman_superhero_____f_1920x1080.jpg')
# 只有两张图片的属性一样才可以融合
# print(back.shape)
# print(superman.shape)
# (1080, 1920, 3)
# (1080, 1920, 3)
res = cv2.addWeighted(superman, 0.7, back, 0.3, 0)
cv2.imshow('img', res)
key = cv2.waitKey(0) & 0xff
if key == ord('q'):
cv2.destroyAllWindows()
OpenCV位运算-非操作
python
# -*- coding: utf-8 -*-
import cv2
import numpy as np
img = np.zeros((200, 200), np.uint8)
img[50:150, 50:150] = 255
cv2.imshow('img', img)
key = cv2.waitKey(0) & 0xff
if key == ord('q'):
cv2.destroyAllWindows()
python
# -*- coding: utf-8 -*-
import cv2
import numpy as np
img = np.zeros((200, 200), np.uint8)
img[50:150, 50:150] = 255
new_img = cv2.bitwise_not(img)
cv2.imshow('img', img)
cv2.imshow('new_img', new_img)
key = cv2.waitKey(0) & 0xff
if key == ord('q'):
cv2.destroyAllWindows()
OpenCV位操作-与运算
python
# -*- coding: utf-8 -*-
import cv2
import numpy as np
img = np.zeros((200, 200), np.uint8)
img2 = np.zeros((200, 200), np.uint8)
img[20:120, 20:120] = 255
img2[80:180, 80:180] = 255
# new_img = cv2.bitwise_not(img)
cv2.imshow('img', img)
cv2.imshow('img2', img2)
key = cv2.waitKey(0) & 0xff
if key == ord('q'):
cv2.destroyAllWindows()
python
# -*- coding: utf-8 -*-
import cv2
import numpy as np
img = np.zeros((200, 200), np.uint8)
img2 = np.zeros((200, 200), np.uint8)
img[20:120, 20:120] = 255
img2[80:180, 80:180] = 255
# new_img = cv2.bitwise_not(img)
new_img = cv2.bitwise_and(img, img2)
cv2.imshow('img', img)
cv2.imshow('img2', img2)
cv2.imshow('new_img', new_img)
key = cv2.waitKey(0) & 0xff
if key == ord('q'):
cv2.destroyAllWindows()
OpenCV位操作-或与异或
python
# -*- coding: utf-8 -*-
import cv2
import numpy as np
img = np.zeros((200, 200), np.uint8)
img2 = np.zeros((200, 200), np.uint8)
img[20:120, 20:120] = 255
img2[80:180, 80:180] = 255
# new_img = cv2.bitwise_not(img)
# new_img = cv2.bitwise_and(img, img2)
new_img = cv2.bitwise_or(img, img2)
new_img2 = cv2.bitwise_xor(img, img2)
cv2.imshow('img', img)
cv2.imshow('img2', img2)
cv2.imshow('new_img', new_img)
cv2.imshow('new_img2', new_img2)
key = cv2.waitKey(0) & 0xff
if key == ord('q'):
cv2.destroyAllWindows()
为图像添加水印
python
# -*- coding: utf-8 -*-
import cv2
import numpy as np
#1. 引入一幅图片
#2. 要有一个LOGO,需要自己创建
#3. 计算图片在什么地方添加,在添加的地方变成黑色
#4. 利用add,将logo 与 图处叠加到一起
cv2.namedWindow('img', cv2.WINDOW_NORMAL)
superman = cv2.imread('E://pic//Man_Of_Steel_superman_superhero_____f_1920x1080.jpg')
# 创建水印
logo = np.zeros((200, 200, 3), np.uint8)
mask = np.zeros((200, 200), np.uint8)
# 绘制水印
logo[20:120, 20:120] = [0, 0, 255]
logo[80:180, 80:180] = [255, 255, 0]
mask[20:120, 20:120] = 255
mask[80:180, 80:180] = 255
# 对mask按位取反
m = cv2.bitwise_not(mask)
# 选择添加logo的位置
roi = superman[0:200, 0: 200]
# 和m进行按位与操作
# 因为roi是三通道,而m是单通道,不能直接相与
tmp = cv2.bitwise_and(roi, roi, mask=m)
dst = cv2.add(tmp, logo)
superman[0:200, 0:200] = dst
cv2.imshow('dst', dst)
cv2.imshow('tmp', tmp)
cv2.imshow('mask', mask)
cv2.imshow('logo', logo)
cv2.imshow('img', superman)
cv2.imshow('m', m)
key = cv2.waitKey(0) & 0xff
if key == ord('q'):
cv2.destroyAllWindows()
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!