图像融合
按照一定的比例将两张图片融合在一起
addWeighted()方法:
- 参数1第一张图片矩阵
- 参数2第一张图片矩阵的权重
- 参数3第二张图片矩阵
- 参数4第二张图片矩阵的权重
- 融合之后的偏移量
import cv2
import cv2 as cv
img = cv.imread("img/lena.jpg")
tony = cv.imread("img/tony.jpg", )
height, width = img.shape[0:2]
new_height = int(height * 1.5)
new_width = int(width * 2)
new_img = cv2.resize(img, (new_width, new_height))
dst = cv.addWeighted(new_img, 0.5, tony, 0.5, 0)
cv.imshow("dst", dst)
cv.waitKey(0)
cv.destroyAllWindows()
灰度处理
- 一张彩色图片通常是由BGR三个通道叠加而成
- 为了便于图像特征识别,我们通常会将一张彩色图片转成灰度图片来进行分析,当我们转成灰色图片之后,图片中边缘,轮廓特征仍然是能够清晰看到的,况且在这种情况下我们仅需要对单一通道进行分析,会简化很多操作
- 前面说的可以读取图片时以灰度的方式读取
import cv2
img = cv2.imread("img/lena.jpg", cv.IMREAD_GRAYSCALE)
- BGR转灰度图
import cv2
img = cv2.imread("img/lena.jpg", cv.IMREAD_COLOR)
dstImg = cv2.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow("dstImg", dstImg)
cv.waitKey(0)
颜色反转
灰度反转
- 灰度图中每一个像素点都是0~255组成,如果一个像素点为100,反转之后就是
255 - 100 = 155
import cv2 as cv
img = cv.imread("img/lena.jpg", cv.IMREAD_COLOR)
dstImg = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
height, width = dstImg.shape[0:2]
for row in range(height):
for col in range(width):
dstImg[row, col] = 255 - dstImg[row, col]
cv.imshow("dstImg", dstImg)
cv.waitKey(0)
彩色反转
- 一样的道理,彩色图片有BGR三个颜色通道,每一个颜色都取反
255 - B = B1 255 - G = G1 255 - R = R1
import cv2 as cv
img = cv.imread("img/lena.jpg", cv.IMREAD_COLOR)
height, width = img.shape[0:2]
for row in range(height):
for col in range(width):
b, g, r = img[row, col]
b1 = 255 - b
g1 = 255 - g
r1 = 255 - r
img[row, col] = b1, g1, r1
cv.imshow("img", img)
cv.waitKey(0)
马赛克效果
- 马赛克指现行广为使用的一种图像(视频)处理手段,此手段将影像特定区域的色阶细节劣化并造成色块打乱的效果,因为这种模糊看上去有一个个的小格子组成,便形象的称这种画面为马赛克。其目的通常是使之无法辨认。
import cv2
img = cv2.imread('./img/lena.jpg', cv2.IMREAD_COLOR)
img1 = img[180:250, 180:310]
height, width = img1.shape[0:2]
for row in range(height):
for col in range(width):
if row % 10 == 0 and col % 10 == 0:
b, g, r = img1[row, col]
for i in range(10):
for j in range(10):
img1[row + i, col + j] = b, g, r
cv2.imshow('img', img)
cv2.imwrite('msk_lena.jpg', img)
cv2.waitKey()
毛玻璃效果
import random
import cv2
import numpy as np
img = cv2.imread('./lena.jpg')
height, width = img.shape[0:2]
new_img = np.zeros_like(img, np.uint8)
offset = 6
for row in range(height):
for col in range(width):
index = int(random.random() * offset)
random_row = row + index if row + index < height else height - 1
random_col = col + index if col + index < width else width - 1
b, g, r = img[random_row, random_col]
new_img[row, col] = b, g, r
cv2.imshow('img', img)
cv2.imshow('new_img', new_img)
cv2.waitKey()
浮雕效果
- 浮雕效果公式:
new_gray = gray0-gray1+120 - 加120是为了增加灰度值
import cv2
import numpy as np
img = cv2.imread('./lena.jpg')
height, width = img.shape[0:2]
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
new_img = np.zeros_like(gray_img, np.uint8)
for row in range(height):
for col in range(width - 1):
gray0 = gray_img[row, col]
gray1 = gray_img[row, col + 1]
new_gray = int(gray0) - int(gray1) + 120
if new_gray > 255:
new_gray = 255
elif new_gray < 0:
new_gray = 0
new_img[row, col] = new_gray
cv2.imshow('img', img)
cv2.imshow('new_img', new_img)
cv2.waitKey()
|