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.

219 lines
8.5 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
import albumentations as A
#图像处理类
class Image_enhancement:
def __init__(self,):
pass
"""===============================1.几何处理:=================================="""
# 1.1 图片旋转(可选择旋转角度)
def roate(self, frame=None,angle=30):
h, w = frame.shape[:2]
M = cv2.getRotationMatrix2D((w / 2, h / 2), angle, 1)
img_roate = cv2.warpAffine(frame, M, (w, h))
return img_roate
# 1.2 图片缩放(可选择缩放大小)
def resize(self, frame=None,w=224,h=224):
img_resized = cv2.resize(frame, (w, h)) # w,h
return img_resized
"""===============================2.颜色空间变化:=================================="""
# 2.1 图片的对比度、饱和度、亮度、色调变化
def ColorJitter(self, frame=None,brightness=0,hue=0,saturation=0,contrast=0):
transform = A.ColorJitter(brightness=float(brightness),hue=float(hue),saturation=float(saturation),contrast=float(contrast),p=1)
img_result = transform(image=np.array(frame))
img_color = img_result['image']
return img_color
# 2.2 灰度图转换
def ToGray(self, frame=None ):
# cvtColor的第一个参数是处理的图像第二个是RGB2GRAY
gray_img = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
return gray_img
# 2.3 直方图均衡化
def equalhist(self, frame=None):
# 直方图均衡化增强对比度:通过调整图像的灰度分布,增加图像的对比度,从而使图像更加清晰。
frame_lab = cv2.cvtColor(frame, cv2.COLOR_BGR2LAB)
frame_lab[:, :, 0] = cv2.equalizeHist(frame_lab[:, :, 0])
enhanced_frame = cv2.cvtColor(frame_lab, cv2.COLOR_LAB2BGR)
return enhanced_frame
"""===============================3.频率像素点操作:=================================="""
# 3.1 模糊
# 3.1.1 高斯滤波
def Gaussblur(self, frame=None,count=3):
transform = A.GaussianBlur(blur_limit=count, p=1)
img_result = transform(image=np.array(frame))['image']
return img_result
# 3.1.3 随机模糊
def Blur(self, frame=None, count=3):
transform = A.Blur(blur_limit=count, p=1)
img_result = transform(image=np.array(frame))['image']
return img_result
# 3.1.2 中值滤波
def Medianblur(self, frame=None, count=3):
transform = A.MedianBlur(blur_limit=count, p=1)
img_result = transform(image=np.array(frame))['image']
return img_result
# 3.2 锐化
# 3.2.1 使用sobel算子进行锐化
def sobel(self, frame=None):
img_gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
kernelx1 = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]], dtype=int)
kernely1 = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], dtype=int)
x1 = cv2.filter2D(img_gray, cv2.CV_16S, kernelx1)
y1 = cv2.filter2D(img_gray, cv2.CV_16S, kernely1)
absX1 = cv2.convertScaleAbs(x1)
absY1 = cv2.convertScaleAbs(y1)
img_result = cv2.addWeighted(absX1, 0.5, absY1, 0.5, 0)
return img_result
# 3.2.2 使用Prewitt算子进行锐化
def Prewitt(self, frame=None):
img_gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
kernelx1 = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]], dtype=int)
kernely1 = np.array([[-1, -1, 1], [0, 0, 0], [1, 1, 1]], dtype=int)
x1 = cv2.filter2D(img_gray, cv2.CV_16S, kernelx1)
y1 = cv2.filter2D(img_gray, cv2.CV_16S, kernely1)
absX1 = cv2.convertScaleAbs(x1)
absY1 = cv2.convertScaleAbs(y1)
img_result = cv2.addWeighted(absX1, 0.5, absY1, 0.5, 0)
return img_result
# 3.2.3 使用robert算子进行锐化
def robert(self, frame=None):
img_gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
kernelx1 = np.array([[-1, 0], [0, 1]], dtype=int)
kernely1 = np.array([[0, -1], [1, 0]], dtype=int)
x1 = cv2.filter2D(img_gray, cv2.CV_16S, kernelx1)
y1 = cv2.filter2D(img_gray, cv2.CV_16S, kernely1)
absX1 = cv2.convertScaleAbs(x1)
absY1 = cv2.convertScaleAbs(y1)
img_result = cv2.addWeighted(absX1, 0.5, absY1, 0.5, 0)
return img_result
# 3.3 添加噪声
# 3.3.1 添加高斯噪声
def add_gaussian_noise(self, frame=None, mean=0,sigma=30):
# 生成高斯噪声矩阵
row, col, ch = frame.shape
gaussian = np.random.randn(row, col, ch) * sigma + mean
gaussian = gaussian.reshape(row, col, ch)
img_result = frame + gaussian
# 转换数据类型为8位无符号整数类型
img_result = cv2.convertScaleAbs(img_result)
return img_result
# 3.3.2 添加椒盐噪声
def add_salt_and_pepper_noise(self, frame=None, percentage=0): # percentage [0,100]
# 确保百分比在 0 到 100 之间
if percentage < 0 or percentage > 100:
percentage=0
# 生成椒盐噪声矩阵
row, col, ch = frame.shape
noise = np.zeros((row, col, ch), np.uint8)
for i in range(row):
for j in range(col):
rand = np.random.randint(0, 100)
if rand < percentage:
noise[i][j] = [0, 0, 0]
elif rand > 100 - percentage:
noise[i][j] = [255, 255, 255]
else:
noise[i][j] = frame[i][j]
# 将椒盐噪声矩阵添加到原始图像中
img_result = cv2.add(frame, noise)
return img_result
# 3.3.3 添加均值噪声
def add_mean_noise(self, frame=None, mean=0,std_dev=30):
# 生成均值噪声矩阵
row, col, ch = frame.shape
noise = np.random.normal(mean, std_dev, (row, col, ch)).astype(np.uint8)
# 将均值噪声矩阵添加到原始图像中
img_result = cv2.add(frame, noise)
return img_result
# 3.4 边缘检测
def Canny(self, frame=None):
# 转为灰度图
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# 高斯滤波
gray = cv2.GaussianBlur(gray, (5, 5), 0)
# 边缘检测
canny_edged = cv2.Canny(gray, 30, 250)
return canny_edged
if __name__ == "__main__":
# 类实例化
img_enhance = Image_enhancement()
img_path = r'images/car_test.jpg'
img = cv2.imread(img_path)
# 测试1.1 图片旋转(可选择旋转角度)
img_roate = img_enhance.roate(img,45)
cv2.imwrite("out_imgs/out_1.jpg",img_roate)
# 测试1.2 图片缩放(可选择缩放大小)
img_resized = img_enhance.resize(img, w=224, h=224)
cv2.imwrite("out_imgs/out_2.jpg", img_resized)
# 测试2.1 图片的对比度、饱和度、亮度、色调变化
img_resized = img_enhance.ColorJitter(img, brightness=10,hue=10,saturation=10,contrast=10)
cv2.imwrite("out_imgs/out_3.jpg", img_resized)
# 测试2.2 灰度图转化
gray_img = img_enhance.ToGray(img)
cv2.imwrite("out_imgs/out_4.jpg", gray_img)
# 测试2.3 直方图均衡化
enhanced_frame = img_enhance.equalhist(img)
cv2.imwrite("out_imgs/out_5.jpg", enhanced_frame)
# 3.1 模糊
# 3.1.1 高斯滤波
img_gauss = img_enhance.Gaussblur(img,count=5)
cv2.imwrite("out_imgs/out_6.jpg", img_gauss)
# 3.1.2 随机模糊
img_Blur = img_enhance.Blur(img, count=5)
cv2.imwrite("out_imgs/out_7.jpg", img_Blur)
# 3.1.3 中值滤波
img_Medianblur = img_enhance.Medianblur(img, count=5)
cv2.imwrite("out_imgs/out_8.jpg", img_Medianblur)
# 3.2 锐化
# 3.2.1 使用sobel算子进行锐化
img_sobel = img_enhance.sobel(img)
cv2.imwrite("out_imgs/out_9.jpg", img_sobel)
# 3.2.2 使用Prewitt算子进行锐化
img_Prewitt = img_enhance.Prewitt(img)
cv2.imwrite("out_imgs/out_10.jpg", img_Prewitt)
# 3.2.3 使用robert算子进行锐化
img_robert = img_enhance.robert(img)
cv2.imwrite("out_imgs/out_11.jpg", img_robert)
# 3.3 添加噪声
# 3.3.1 添加高斯噪声
img_gaussian_noise = img_enhance.add_gaussian_noise(img,mean=0,sigma=30)
cv2.imwrite("out_imgs/out_12.jpg", img_gaussian_noise)
# 3.3.2 添加椒盐噪声
img_salt_and_pepper_noise = img_enhance.add_salt_and_pepper_noise(img, percentage=10)
cv2.imwrite("out_imgs/out_13.jpg", img_salt_and_pepper_noise)
# 3.3.3 添加均值噪声
img_mean_noise = img_enhance.add_mean_noise(img, mean=0, std_dev=30)
cv2.imwrite("out_imgs/out_14.jpg", img_mean_noise)
# 3.4 边缘检测
img_Canny = img_enhance.Canny(img)
cv2.imwrite("out_imgs/out_15.jpg", img_Canny)