两图像加减乘除
- 相加:图像混合、添加噪声
-
- img = cv.add(img1, img2)
- img = cv.addWeighted(img1, alpha, img2, beta, gamma)
- 相减:消除背景、影插法(比较差异、运动跟踪)
-
- img = cv.subtract(img1, img2)
- 相乘:掩膜(mask)
-
- img = cv.multiply(img1, img2)
- 相除:设备校正、比较差异
-
- img = cv.divide(img1, img2)
- 完整代码
相加:图像混合、添加噪声
img = cv.add(img1, img2)
img = cv.addWeighted(img1, alpha, img2, beta, gamma)
import matplotlib.pyplot as plt
import numpy as np
import cv2 as cv
def show(img):
if img.ndim == 2:
plt.imshow(img, cmap='gray')
else:
plt.imshow(cv.cvtColor(img, cv.COLOR_RGB2GRAY))
plt.show()
dog = cv.imread('dog.jpg', 0)
cat = cv.imread('cat.jpg', 0)
bg = cv.imread('background.jpg', 0)
mask = cv.imread('mask.jpg', 0)
cat_art = cv.imread('cat_art.jpg', 0)
cv.imwrite('cat.jpg', cv.resize(cat, (800, 800)))
cv.imwrite('background.jpg', cv.resize(bg, (800, 800)))
cv.imwrite('mask.jpg', cv.resize(mask, (800, 800)))
cv.imwrite('cat_art.jpg', cv.resize(cat_art, (800, 800)))
cv.imwrite('dog.jpg', cv.resize(dog, (800, 800)))
show(np.hstack([cat, cat_art, dog, bg, mask]))
img_add = cat + bg
show(img_add)
img_cvadd = cv.add(cat, bg)
show(img_cvadd)
img_cvadd_half = cv.add(cat * 0.5, bg * 0.5)
print(img_cvadd_half.dtype)
show(img_cvadd_half)
img_cvaddweight = cv.addWeighted(cat, 0.5, bg, 0.5, 0)
print(img_cvaddweight.dtype)
show(img_cvaddweight)
相减:消除背景、影插法(比较差异、运动跟踪)
img = cv.subtract(img1, img2)
img_sub = (img_cvadd_half - bg * 0.5)
img_sub2 = (img_cvadd_half - bg * 0.5) * 2
show(np.hstack([img_sub, img_sub2]))
相乘:掩膜(mask)
img = cv.multiply(img1, img2)
mask = mask / 255
img_mul = cat * mask
show(img_mul)
img_mul2 = cv.multiply(cat * 1.0, mask)
show(img_mul2)
相除:设备校正、比较差异
img = cv.divide(img1, img2)
img_div = cat / (np.float64(cat_art) + 1)
img_div2 = cv.divide(cat, cat_art + 1)
show(np.hstack([img_div, img_div2]))
完整代码
"""
相加:图像混合、添加噪声
img = cv.add(img1, img2)
img = cv.addWeighted(img1, alpha, img2, beta, gamma)
相减:消除背景、影插法(比较差异、运动跟踪)
img = cv.subtract(img1, img2)
相乘:掩膜(mask)
img = cv.multiply(img1, img2)
相除:设备校正、比较差异
img = cv.divide(img1, img2)
"""
import matplotlib.pyplot as plt
import numpy as np
import cv2 as cv
def show(img):
if img.ndim == 2:
plt.imshow(img, cmap='gray')
else:
plt.imshow(cv.cvtColor(img, cv.COLOR_RGB2GRAY))
plt.show()
dog = cv.imread('dog.jpg', 0)
cat = cv.imread('cat.jpg', 0)
bg = cv.imread('background.jpg', 0)
mask = cv.imread('mask.jpg', 0)
cat_art = cv.imread('cat_art.jpg', 0)
cv.imwrite('cat.jpg', cv.resize(cat, (800, 800)))
cv.imwrite('background.jpg', cv.resize(bg, (800, 800)))
cv.imwrite('mask.jpg', cv.resize(mask, (800, 800)))
cv.imwrite('cat_art.jpg', cv.resize(cat_art, (800, 800)))
cv.imwrite('dog.jpg', cv.resize(dog, (800, 800)))
show(np.hstack([cat, cat_art, dog, bg, mask]))
img_add = cat + bg
show(img_add)
img_cvadd = cv.add(cat, bg)
show(img_cvadd)
img_cvadd_half = cv.add(cat * 0.5, bg * 0.5)
print(img_cvadd_half.dtype)
show(img_cvadd_half)
img_cvaddweight = cv.addWeighted(cat, 0.5, bg, 0.5, 0)
print(img_cvaddweight.dtype)
show(img_cvaddweight)
img_sub = (img_cvadd_half - bg * 0.5)
img_sub2 = (img_cvadd_half - bg * 0.5) * 2
show(np.hstack([img_sub, img_sub2]))
mask = mask / 255
img_mul = cat * mask
show(img_mul)
img_mul2 = cv.multiply(cat * 1.0, mask)
show(img_mul2)
img_div = cat / (np.float64(cat_art) + 1)
show(img_div)
img_div2 = cv.divide(cat, cat_art + 1)
show(img_div2)
show(np.hstack([img_div, img_div2]))