diff --git a/image_method.py b/image_method.py new file mode 100644 index 0000000..3ffb860 --- /dev/null +++ b/image_method.py @@ -0,0 +1,92 @@ +import cv2 +import numpy as np + +"""功能一:""" +#1图像转换为灰度图像 +def grayscale(image): + return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) + +#1垂直镜像 +def mirror_vertical(image): + vertical = cv2.flip(image,0,dst=None) + return vertical + +#1图像放缩(!) +def change_size(image): + change_image = cv2.resize(image, (256, 256)) + return change_image + +#1旋转画布 +def rotate(image): + rows, cols, depth = image.shape + rotation = cv2.getRotationMatrix2D((cols / 2, rows / 2), 90, 1) + return rotation + +#1水平镜像 +def mirror_horizontal(image): + horizontal = cv2.flip(image,1,dst=None) + return horizontal + +#1保持横纵比例 +def keep_shape(image): + # 获取图像的高度和宽度 + height, width = image.shape[:2] + # 计算裁剪区域crop + target_aspect_ratio = 16 / 9 + if width / height > target_aspect_ratio: + new_width = int(height * target_aspect_ratio) + offset = (width - new_width) // 2 + crop = image[:, offset:offset + new_width] + else: + new_height = int(width / target_aspect_ratio) + offset = (height - new_height) // 2 + crop = image[offset:offset + new_height, :] + return crop + +#1还原原始图像 + + +"""功能二""" +#2高斯滤波模糊去噪 +def apply_blur(image): + blurred = cv2.GaussianBlur(image, (5, 5), 0) + return blurred + +#2肤色处理 +def skin(image): + # 将图像从BGR转换到HSV + hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) + # 定义肤色的HSV范围 + lower_skin = np.array([0, 20, 70], dtype=np.uint8) + upper_skin = np.array([20, 255, 255], dtype=np.uint8) + # 创建掩码 + mask = cv2.inRange(hsv_image, lower_skin, upper_skin) + # 应用掩码到原始图像 + skin_image = cv2.bitwise_and(image, image, mask=mask) + return skin_image + +#2腐蚀 +def center_erosion(image): + erode = cv2.imread(image, cv2.IMREAD_UNCHANGED) + # 使用一个5x5的交叉型结构元(核心在几何中心)对二值图片src进行腐蚀 + kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (5, 5)); + erosion = cv2.erode(erode,kernel) + return erosion + + +"""功能三""" +#边缘检测 +def detect_edges(image): + """使用Canny边缘检测算法检测图像的边缘""" + edges = cv2.Canny(image, 100, 200) + return edges + + + + +# 更多自定义处理方法... + +#识别边缘 + + +