python写一段opencv利用调整亮度、饱和度、色调、对比度、随机平移、旋转、缩放随机选择其中一种或多种来进行图像增强的代码
时间: 2023-09-12 19:05:09 浏览: 216
### 回答1:
可以使用以下代码来实现:import cv2# 读取图像
img = cv2.imread('image.jpg')# 随机选择一种或多种操作来增强图像
choice = np.random.randint(1, 7)
if choice == 1:
img = cv2.addWeighted(img, 1.2, np.zeros(img.shape, img.dtype), 0, 10) # 调整亮度
elif choice == 2:
img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
img[:, :, 1] = img[:, :, 1] * 1.2 # 饱和度
img[:, :, 2] = img[:, :, 2] * 1.2 # 色调
img = cv2.cvtColor(img, cv2.COLOR_HSV2BGR)
elif choice == 3:
img = cv2.addWeighted(img, 1.2, np.zeros(img.shape, img.dtype), 0, -25) # 对比度
elif choice == 4:
x = np.random.randint(-20, 20)
y = np.random.randint(-20, 20)
M = np.float32([[1, 0, x], [0, 1, y]]) # 随机平移
img = cv2.warpAffine(img, M, (img.shape[1], img.shape[0]))
elif choice == 5:
angle = np.random.randint(-30, 30)
M = cv2.getRotationMatrix2D((img.shape[1] / 2, img.shape[0] / 2), angle, 1) # 旋转
img = cv2.warpAffine(img, M, (img.shape[1], img.shape[0]))
elif choice == 6:
scale = np.random.uniform(0.7, 1.3)
M = cv2.getRotationMatrix2D((img.shape[1] / 2, img.shape[0] / 2), 0, scale) # 缩放
img = cv2.warpAffine(img, M, (img.shape[1], img.shape[0]))# 保存图像
cv2.imwrite('enhanced_image.jpg', img)
### 回答2:
import cv2
import random
def adjust_brightness(image, value):
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)
v = cv2.add(v, value)
v = cv2.add(v, np.where(v > 255, -255, 0))
final_hsv = cv2.merge((h, s, v))
enhanced = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)
return enhanced
def adjust_saturation(image, value):
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)
s = cv2.add(s, value)
s = cv2.add(s, np.where(s > 255, -255, 0))
final_hsv = cv2.merge((h, s, v))
enhanced = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)
return enhanced
def adjust_hue(image, value):
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)
h = cv2.add(h, value)
h = cv2.add(h, np.where(h > 180, -180, 0))
final_hsv = cv2.merge((h, s, v))
enhanced = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)
return enhanced
def adjust_contrast(image, value):
alpha = float(127 + value) / 127
enhanced = cv2.addWeighted(image, alpha, image, 0, 0)
return enhanced
def random_translation(image, max_shift):
rows, cols, _ = image.shape
dx = random.randint(-max_shift, max_shift)
dy = random.randint(-max_shift, max_shift)
M = np.float32([[1, 0, dx], [0, 1, dy]])
translated = cv2.warpAffine(image, M, (cols, rows))
return translated
def random_rotation(image, max_angle):
rows, cols, _ = image.shape
angle = random.uniform(-max_angle, max_angle)
M = cv2.getRotationMatrix2D((cols/2, rows/2), angle, 1)
rotated = cv2.warpAffine(image, M, (cols, rows))
return rotated
def random_scaling(image, min_scale, max_scale):
scale = random.uniform(min_scale, max_scale)
scaled = cv2.resize(image, None, fx=scale, fy=scale)
return scaled
def random_image_enhancement(image):
operation = random.choice(["brightness", "saturation", "hue", "contrast"])
if operation == "brightness":
value = random.randint(-50, 50)
enhanced = adjust_brightness(image, value)
elif operation == "saturation":
value = random.randint(-50, 50)
enhanced = adjust_saturation(image, value)
elif operation == "hue":
value = random.randint(-50, 50)
enhanced = adjust_hue(image, value)
elif operation == "contrast":
value = random.randint(-50, 50)
enhanced = adjust_contrast(image, value)
else:
enhanced = image
return enhanced
def random_image_transform(image):
operation = random.choice(["translation", "rotation", "scaling"])
if operation == "translation":
max_shift = min(image.shape[0], image.shape[1]) // 10
transformed = random_translation(image, max_shift)
elif operation == "rotation":
max_angle = 20
transformed = random_rotation(image, max_angle)
elif operation == "scaling":
min_scale = 0.5
max_scale = 1.5
transformed = random_scaling(image, min_scale, max_scale)
else:
transformed = image
return transformed
# 定义图像路径
image_path = "image.jpg"
# 读入图像
image = cv2.imread(image_path)
# 进行随机图像增强
enhanced_image = random_image_enhancement(image)
# 进行随机图像变换
transformed_image = random_image_transform(enhanced_image)
# 保存增强后的图像
cv2.imwrite("enhanced_image.jpg", transformed_image)
以上代码实现了利用OpenCV对图像进行随机亮度、饱和度、色调、对比度调整、随机平移、旋转、缩放等图像增强操作的功能。首先,我们定义了一系列函数来实现不同的图像增强操作,包括调整亮度、饱和度、色调、对比度、随机平移、旋转和缩放等。然后,我们使用random_image_enhancement函数随机选择一种增强操作对图像进行增强,接着再使用random_image_transform函数随机选择一种图像变换操作对增强后的图像进行变换。最后,将增强后的图像保存下来。
### 回答3:
要使用Python编写OpenCV实现图像增强的代码,可以使用以下示例代码:
```python
import cv2
import numpy as np
def random_brightness(img, brightness_range):
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
brightness = np.random.uniform(brightness_range[0], brightness_range[1])
img_hsv[:,:,2] = img_hsv[:,:,2] * brightness
img_hsv[:,:,2] = np.clip(img_hsv[:,:,2], 0, 255)
img = cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR)
return img
def random_saturation(img, saturation_range):
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
saturation = np.random.uniform(saturation_range[0], saturation_range[1])
img_hsv[:,:,1] = img_hsv[:,:,1] * saturation
img_hsv[:,:,1] = np.clip(img_hsv[:,:,1], 0, 255)
img = cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR)
return img
def random_hue(img, hue_range):
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
hue = np.random.uniform(hue_range[0], hue_range[1])
img_hsv[:,:,0] = img_hsv[:,:,0] + hue
img_hsv[:,:,0] = np.clip(img_hsv[:,:,0], 0, 179)
img = cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR)
return img
def random_contrast(img, contrast_range):
contrast = np.random.uniform(contrast_range[0], contrast_range[1])
img = cv2.convertScaleAbs(img, alpha=contrast, beta=0)
return img
def random_translate(img, translate_range):
translate_x = int(np.random.uniform(translate_range[0], translate_range[1])*img.shape[1])
translate_y = int(np.random.uniform(translate_range[0], translate_range[1])*img.shape[0])
translation_matrix = np.float32([[1,0,translate_x],[0,1,translate_y]])
img = cv2.warpAffine(img, translation_matrix, (img.shape[1],img.shape[0]))
return img
def random_rotate(img, rotate_range):
angle = np.random.uniform(rotate_range[0], rotate_range[1])
rotation_matrix = cv2.getRotationMatrix2D((img.shape[1]/2, img.shape[0]/2), angle, 1)
img = cv2.warpAffine(img, rotation_matrix, (img.shape[1],img.shape[0]))
return img
def random_scale(img, scale_range):
scale = np.random.uniform(scale_range[0], scale_range[1])
img = cv2.resize(img, None, fx=scale, fy=scale)
return img
# 读取图像
img = cv2.imread('image.jpg')
# 设置增强参数范围
brightness_range = [0.5, 1.5] # 亮度范围
saturation_range = [0.5, 1.5] # 饱和度范围
hue_range = [-10, 10] # 色调范围
contrast_range = [0.8, 1.2] # 对比度范围
translate_range = [-0.2, 0.2] # 随机平移范围
rotate_range = [-30, 30] # 旋转角度范围
scale_range = [0.8, 1.2] # 缩放比例范围
# 随机选择一种或多种增强方式
img = random_brightness(img, brightness_range)
img = random_saturation(img, saturation_range)
img = random_hue(img, hue_range)
img = random_contrast(img, contrast_range)
img = random_translate(img, translate_range)
img = random_rotate(img, rotate_range)
img = random_scale(img, scale_range)
# 显示增强后的图像
cv2.imshow('Enhanced Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
以上代码提供了实现亮度、饱和度、色调、对比度、随机平移、旋转、缩放中的任意一种或多种增强的函数,并结合示例图像完成了增强操作。你可以根据需要对各个增强方式的参数范围进行调整,并通过选择对应的函数来实现相应的图像增强效果。
阅读全文