https://blog.csdn.net/Eastmount/article/details/99566969
一.图像素描特效
图像素描特效会将图像的边界都凸显出来,
通过边缘检测及阈值化处理能实现该功能
import cv2
import numpy as np
#读取原始图像
img = cv2.imread("C:/Users/31035/Desktop/yifei/01.jpg")
#图像灰度处理
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#高斯滤波降噪
gaussian = cv2.GaussianBlur(gray, (3,3), 0)
#Canny算子
canny = cv2.Canny(gaussian, 5, 150)
#阈值化处理
ret, result = cv2.threshold(canny, 127, 255, cv2.THRESH_BINARY_INV)
#显示图像
cv2.imshow('src', img)
cv2.imshow('result', result)
cv2.waitKey()
cv2.destroyAllWindows()
二.图像怀旧特效
怀旧特效是将图像的RGB三个分量分别按照一定比例进行处理的结果
#获取图像行和列
rows, cols = img.shape[:2]
#新建目标图像
dst = np.zeros((rows, cols, 3), dtype="uint8")
# dst = np.zeros_like(img)
#图像怀旧特效
for i in range(rows):
for j in range(cols):
B = 0.272*img[i,j][2] + 0.534*img[i,j][1] + 0.131*img[i,j][0]
G = 0.349*img[i,j][2] + 0.686*img[i,j][1] + 0.168*img[i,j][0]
R = 0.393*img[i,j][2] + 0.769*img[i,j][1] + 0.189*img[i,j][0]
if B>255:
B = 255
if G>255:
G = 255
if R>255:
R = 255
dst[i,j] = np.uint8((B, G, R))
#显示图像
cv2.imshow('src', img)
cv2.imshow('dst', dst)
cv2.waitKey()
cv2.destroyAllWindows()
三.图像光照特效
import math
#获取图像行和列
rows, cols = img.shape[:2]
#设置中心点
centerX = rows / 2
centerY = cols / 2
# print centerX, centerY
#光照范围由半径调节
radius = min(centerX, centerY) / 4
# print radius
#设置光照强度
strength = 200
#新建目标图像
dst = np.zeros((rows, cols, 3), dtype="uint8")
#图像光照特效
for i in range(rows):
for j in range(cols):
#计算当前点到光照中心距离(平面坐标系中两点之间的距离)
distance = math.pow((centerY-j), 2) + math.pow((centerX-i), 2)
#获取原始图像
B = img[i,j][0]
G = img[i,j][1]
R = img[i,j][2]
if (distance < radius * radius):
#按照距离大小计算增强的光照值
result = (int)(strength*( 1.0 - math.sqrt(distance) / radius ))
B = img[i,j][0] + result
G = img[i,j][1] + result
R = img[i,j][2] + result
#判断边界 防止越界
B = min(255, max(0, B))
G = min(255, max(0, G))
R = min(255, max(0, R))
dst[i,j] = np.uint8((B, G, R))
else:
dst[i,j] = np.uint8((B, G, R))
#显示图像
cv2.imshow('src', img)
cv2.imshow('dst', dst)
cv2.waitKey()
cv2.destroyAllWindows()
四.图像流年特效
将原始图像的蓝色(B)通道的像素值开根号,再乘以一个权重参数
五.图像滤镜特效
滤镜通常需要同通道、图层等联合使用,才能取得最佳艺术效果