Contents
import numpy as np
# flipping images with numpy
flipped_img = np.flipr(img)
plt.imshow(flipped_img)
plt.show()
import cv2
import matplotlib.pyplot as plt
def translation(img):
print(img.shape)
height, width = img.shape
# 左移
for i in range(height, 1, -1):
for j in range(width):
if (i < height - 20):
img[j][i] = img[j][i - 20]
elif (i < height - 1):
img[j][i] = 0
plt.imshow(img)
plt.imshow
cv2.imwrite("./demo.png", img)
if __name__ == "__main__":
img = cv2.imread('./person/demo1.png', 0)
img = cv2.resize(img, (124, 124))
translation(img)
图像平移结果
调整图像亮度与对比度算法主要由以下几个步骤组成:
将像素值变小,图像亮度减小,色彩变暗;像素值增大,图像亮度增大,色彩变亮
。 代码如下:
import os
import numpy as np
import cv2
if __name__ == '__main__':
img = cv2.imread('568.Bmp', 0)
img = np.array(img)
mean = np.mean(img)
img = img - mean
img = img * 1.5 + mean * 0.7 # 修对比度和亮度
img = img / 255. # 非常关键,没有会白屏
cv2.imshow('pic', img)
cv2.waitKey()
注意,只需要修改上面代码中的1.5和0.7这两个参数就可以修改对比度和亮度。特别提醒下 img = img/255.这句,之前显示出来图片全是白色的,不知道什么问题。去查了下手册,解释如下:
• If the image is 8-bit unsigned, it is displayed as is. • If the image is 16-bit unsigned or 32-bit integer, the pixels are divided by 256. That is, the value range [0,255*256] is mapped to [0,255]. • If the image is 32-bit floating-point, the pixel values are multiplied by 255. That is, the value range [0,1] is mapped to [0,255]. 最后一句说明了问题,如果图片是32位float数据,显示数据会乘以255,因为他默认输入的数据是[0,1]的数据。
imgaug是一个封装好的用来进行图像augmentation的python库,支持关键点(keypoint)和bounding box一起变换。项目主页:imgaug doc
导入augmenters类:
from imgaug import augmenters as iaa
import imgaug as ia
产生一个处理图片的Sequential,函数原型:
iaa.Sequential(children=None,
random_order=False,
name=None,
deterministic=False,
random_state=None)
参数:
将Augmenter中的部分变换应用在图片处理上,而不是应用所有的Augmenter。
每次从一些列augmenters中选择一个来变换。
对batch中的一部分图片应用一部分Augmenters,剩下的图片应用另外的Augmenters。