共计 3208 个字符,预计需要花费 9 分钟才能阅读完成。
“””
Author:XiaoMa
date:2021/11/16
“””
import cv2
import numpy as np
import math
import matplotlib.pyplot as plt
img0 = cv2.imread(‘E:\From Zhihu\For the desk\cvfifteen1.jpg’)
img1 = cv2.cvtColor(img0, cv2.COLOR_BGR2GRAY)
h, w = img0.shape[:2]
print(h, w)
cv2.imshow(“W0”, img0)
cv2.imshow(“W1”, img1)
cv2.waitKey(delay = 0)
毛玻璃特效
img2 = np.zeros((h – 6, w – 6, 3), np.uint8) #生成的全零矩阵思考到了随机数范畴,变小了
for i in range(0, h – 6): #避免上面的随机数超出边缘
for j in range(0, w - 6):
index = int(np.random.random()*6) #0~6 的随机数
(b, g, r) = img0[i + index, j + index]
img2[i, j] = (b, g, r)
cv2.imshow(“W2”, img2)
cv2.waitKey(delay = 0)
浮雕特效 (须要对灰度图像进行操作)
img3 = np.zeros((h, w, 3), np.uint8)
for i in range(0, h):
for j in range(0, w - 2): #减 2 的成果和下面一样
grayP0 = int(img1[i, j])
grayP1 = int(img1[i, j + 2]) #取与前一个像素点相邻的点
newP = grayP0 - grayP1 + 150 #失去差值,加一个常数能够减少浮雕立体感
if newP > 255:
newP = 255
if newP < 0:
newP = 0
img3[i, j] = newP
cv2.imshow(“W3”, img3)
cv2.waitKey(delay = 0)
素描特效
img4 = 255 – img1 #对原灰度图像的像素点进行反转
blurred = cv2.GaussianBlur(img4, (21, 21), 0) #进行高斯含糊
inverted_blurred = 255 – blurred #反转
img4 = cv2.divide(img1, inverted_blurred, scale = 127.0) #灰度图像除以倒置的含糊图像失去铅笔素描画
cv2.imshow(“W4”, img4)
cv2.waitKey(delay = 0)
念旧特效
img5 = np.zeros((h, w, 3), np.uint8)
for i in range(0, h):
for j in range(0, w):
B = 0.272 * img0[i, j][2] + 0.534 * img0[i, j][1] + 0.131 * img0[i, j][0]
G = 0.349 * img0[i, j][2] + 0.686 * img0[i, j][1] + 0.168 * img0[i, j][0]
R = 0.393 * img0[i, j][2] + 0.769 * img0[i, j][1] + 0.189 * img0[i, j][0]
if B > 255:
B = 255
if G > 255:
G = 255
if R > 255:
R = 255
img5[i, j] = np.uint8((B, G, R))
cv2.imshow(“W5”, img5)
cv2.waitKey(delay = 0)
流年特效
img6 = np.zeros((h, w, 3), np.uint8)
for i in range(0, h):
for j in range(0, w):
B = math.sqrt(img0[i, j][0]) *14 # B 通道的数值开平方乘以参数 14
G = img0[i, j][1]
R = img0[i, j][2]
if B > 255:
B = 255
img6[i, j] = np.uint8((B, G, R))
cv2.imshow(“W6”, img6)
cv2.waitKey(delay = 0)
水波特效
img7 = np.zeros((h, w, 3), np.uint8)
wavelength = 20 #定义水波特效波长
amplitude = 30 #幅度
phase = math.pi / 4 #相位
centreX = 0.5 #水波中心点 X
centreY = 0.5 #水波中心点 Y
radius = min(h, w) / 2
icentreX = w*centreX #水波笼罩宽度
icentreY = h*centreY #水波笼罩高度
for i in range(0, h):
for j in range(0, w):
dx = j - icentreX
dy = i - icentreY
distance = dx * dx + dy * dy
if distance > radius * radius:
x = j
y = i
else:
# 计算水波区域
distance = math.sqrt(distance)
amount = amplitude * math.sin(distance / wavelength * 2 * math.pi - phase)
amount = amount * (radius - distance) / radius
amount = amount * wavelength / (distance + 0.0001)
x = j + dx * amount
y = i + dy * amount
# 边界判断
if x < 0:
x = 0
if x >= w - 1:
x = w - 2
if y < 0:
y = 0
if y >= h - 1:
y = h - 2
p = x - int(x)
q = y - int(y)
# 图像水波赋值
img7[i, j, :] = (1 - p) * (1 - q) * img0[int(y), int(x), :] + p * (1 - q) * img0[int(y), int(x), :]
+ (1 - p) * q * img0[int(y), int(x), :] + p * q * img0[int(y), int(x), :]
cv2.imshow(“W7”, img7)
cv2.waitKey(delay = 0)
卡通特效
num_bilateral = 7 #定义双边滤波的数目
for i in range(num_bilateral): #双边滤波解决
img_color = cv2.bilateralFilter(img0, d = 9, sigmaColor = 5, sigmaSpace = 3)
img_blur = cv2.medianBlur(img1, 7) # 中值滤波解决
img_edge = cv2.adaptiveThreshold(img_blur, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, blockSize = 5, C = 2) #金融期货边缘检测及自适应阈值化解决
img_edge = cv2.cvtColor(img_edge, cv2.COLOR_GRAY2RGB) #转换回彩色图像
img8 = cv2.bitwise_and(img0, img_edge) #图像的与运算
cv2.imshow(‘W8’, img8)
cv2.waitKey(delay = 0)
将所有图像保留到一张图中
plt.rcParams[‘font.family’] = ‘SimHei’
imgs = [img0, img1, img2, img3, img4, img5, img6, img7, img8]
titles = [‘ 原图 ’, ‘ 灰度图 ’, ‘ 毛玻璃特效 ’, ‘ 浮雕特效 ’, ‘ 素描特效 ’, ‘ 念旧特效 ’, ‘ 流年特效 ’, ‘ 水波特效 ’, ‘ 卡通特效 ’]
for i in range(9):
imgs[i] = cv2.cvtColor(imgs[i], cv2.COLOR_BGR2RGB)
plt.subplot(3, 3, i + 1)
plt.imshow(imgs[i])
plt.title(titles[i])
plt.xticks([])
plt.yticks([])
plt.suptitle(‘ 图像特效解决 ’)
plt.savefig(‘E:\From Zhihu\For the desk\cvfifteenresult.jpg’, dpi = 1080)
plt.show()