diff --git a/filter_methods.py b/filter_methods.py new file mode 100644 index 0000000..174a384 --- /dev/null +++ b/filter_methods.py @@ -0,0 +1,63 @@ +import cv2 +import matplotlib.pyplot as plt +import ImageFilter as filter +import numpy as np +#浮雕 +def relief(img, Degree): # 参数为原图像和浮雕图像程度 + gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) + h, w = gray.shape[0:2] + # 定义空白图像,存放图像浮雕处理之后的图片 + img1 = np.zeros((h, w), dtype=gray.dtype) + # 通过对原始图像进行遍历,通过浮雕公式修改像素值,然后进行浮雕处理 + for i in range(h): + for j in range(w - 1): + # 前一个像素值 + a = gray[i, j] + # 后一个像素值 + b = gray[i, j + 1] + # 新的像素值,防止像素溢出 + img1[i, j] = min(max((int(a) - int(b) + Degree), 0), 255) + return img1 + +#素描 +def Nostalgia(img): + gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) + #通过高斯滤波过滤噪声 + gaussian = cv2.GaussianBlur(gray, (3,3), 0) + #通过canny算法提取图像轮轮廓 + canny = cv2.Canny(gaussian, 50, 140) + #对轮廓图像进行反二进制阈值化处理 + ret, result = cv2.threshold(canny, 90, 255, cv2.THRESH_BINARY_INV) + return result + +#水墨画 +def stylization(img): + result = cv2.stylization(img, sigma_s=60, sigma_r=0.6) + return result + +#连环画 + +#美颜磨皮 +def beauty_filter(image): + # 转换为灰度图 + gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) + # 使用Bilateral Filter双边滤波平滑皮肤,同时保留边缘细节 + bilateral = cv2.bilateralFilter(image, 9, 75, 75) + # 使用Gaussian Blur进一步模糊,达到柔化效果 + blur = cv2.GaussianBlur(bilateral, (23, 23), 30) + # 计算原图与模糊后的图像的差异 + diff = image - blur + # 将差异图与原图融合,控制美颜程度 + result = image + diff * 0.5 + # 确保像素值不超过255或低于0 + result = np.clip(result, 0, 255).astype(np.uint8) + return result + +#button3:图像转换为灰度图像 +def grayscale(image): + global display_img,current_img + return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) + + + +