You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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
# 更多自定义处理方法...
#识别边缘