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.

94 lines
3.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
# 调整图像亮度(img为图像 brightness在-100~100内可取)
def brightness_change(img, brightness):
img_t = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(img_t)
v1 = np.clip(cv2.add(1*v, brightness), 0, 255)
dst = np.uint8(cv2.merge((h, s, v1)))
dst = cv2.cvtColor(dst, cv2.COLOR_HSV2BGR)
return dst
# 平移图像(x为水平移动距离 y为垂直移动距离)
def translation(img, x, y):
height, width, channel = img.shape
M = np.float32([[1, 0, x], [0, 1, y]])
dst = cv2.warpAffine(img, M, (width, height), borderValue=(255, 255, 255))
return dst
# 旋转图像(direction为0是顺时针1是逆时针angle为旋转角度)
def rotation(img, direction, angle):
if direction == 1:
angle = -1*angle
height, width, channel = img.shape
M = cv2.getRotationMatrix2D((width / 2, height / 2), angle, 1)
dst = cv2.warpAffine(img, M, (width, height), borderValue=(255, 255, 255))
return dst
# 翻转图像(direction决定翻转方向0是水平翻转1是垂直翻转)
def flip(img, direction):
if direction == 1:
direction = 0
else:
direction = 1
dst = cv2.flip(img, direction)
return dst
# 放缩图像(proportion是放缩比例)
def scale(img, proportion):
if proportion >= 1:
dst = cv2.resize(img, (0, 0), fx=proportion, fy=proportion, interpolation=cv2.INTER_CUBIC)
else:
dst = cv2.resize(img, (0, 0), fx=proportion, fy=proportion, interpolation=cv2.INTER_AREA)
return dst
# 仿射变换图像(给出三组点的前后坐标,实现仿射变换)
def affine(img, x1, y1, x1_change, y1_change, x2, y2, x2_change, y2_change, x3, y3, x3_change, y3_change):
rows, cols = img.shape[: 2]
post1 = np.float32([[x1, y1], [x2, y2], [x3, y3]])
post2 = np.float32([[x1_change, y1_change], [x2_change, y2_change], [x3_change, y3_change]])
M = cv2.getAffineTransform(post1, post2)
dst = cv2.warpAffine(img, M, (rows, cols), borderValue=(255, 255, 255))
return dst
# 绘制直方图(使用histogramimageHist是中间函数)//主要这个matplotlib不知道为什么用不了我看了你的依赖里好像没有我就查了下换了个方法。
def imageHist(img, type):
# color = (255, 255, 255)
if type == 31:
color = (255, 0, 0)
windowName = 'B Hist'
elif type == 32:
color = (0, 255, 0)
windowName = 'G Hist'
elif type == 33:
color = (0, 0, 255)
windowName = 'R Hist'
Hist = cv2.calcHist([img], [0], None, [256], [0.0, 255.0]) # 计算直方图
minV, maxV, minL, MaxL = cv2.minMaxLoc(Hist) # 获取最大值和最小值,并获取其对应的下标
HistImage = np.zeros([256, 256, 3], np.uint8) # 新建图像模板,用于直方图的绘制
for h in range(256):
intenormal = int(Hist[h] * 256 / maxV) # 避免超出范围
cv2.line(HistImage, (h, 256), (h, 256 - intenormal), color) # 绘制直方图
return HistImage
def histogram(img):
channels = cv2.split(img)
image0 = imageHist(channels[0], 31)
image1 = imageHist(channels[1], 32)
image2 = imageHist(channels[2], 33)
imgs = np.hstack([image0, image1, image2])
return imgs
# 调整图像的对比度
def contrast_change(img, contrast):
img_t = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(img_t)
v1 = np.clip(cv2.add(2 * v, contrast), 0, 255)
dst = np.uint8(cv2.merge((h, s, v1)))
dst = cv2.cvtColor(dst, cv2.COLOR_HSV2BGR)
return dst