草书检测搞不出,整点小知识学一下:
# -*- coding:utf-8 -*-
# coding=utf-8
#coco
#三种方法实现对图片实现旋转、倒置、翻转
import os
import uuid
import cv2 as cv
from PIL import Image
from ffmpy import FFmpeg
import matplotlib.pyplot as plt
import numpy as np
'''---------------------------------------------------方法一-------------------------------------------------------'''
# 垂直翻转
def vflip(image_path: str, output_dir: str):
ext = _check_format(image_path)
result = os.path.join(output_dir, '{}.{}'.format(uuid.uuid4(), ext))
ff = FFmpeg(inputs={image_path: None},
outputs={result: '-vf vflip -y'})
print(ff.cmd)
ff.run()
return result
# 水平翻转
def hflip(image_path: str, output_dir: str):
ext = _check_format(image_path)
result = os.path.join(output_dir, '{}.{}'.format(uuid.uuid4(), ext))
ff = FFmpeg(inputs={image_path: None},
outputs={result: '-vf hflip -y'})
print(ff.cmd)
ff.run()
return result
# 顺时针旋转
def rotate(image_path: str, output_dir: str, angle: int):
ext = _check_format(image_path)
result = os.path.join(output_dir, '{}.{}'.format(uuid.uuid4(), ext))
ff = FFmpeg(inputs={image_path: None},
outputs={result: '-vf rotate=PI*{}/180 -y'.format(angle)})
print(ff.cmd)
ff.run()
return result
# 转置
'''
type:0 逆时针旋转90度,对称翻转
type:1 顺时针旋转90度
type:2 逆时针旋转90度
type:3 顺时针旋转90度,对称翻转
'''
#逆时针旋转
def transpose(image_path: str, output_dir: str, type: int):
ext = _check_format(image_path)
result = os.path.join(output_dir, '{}.{}'.format(uuid.uuid4(), ext))
ff = FFmpeg(inputs={image_path: None},
outputs={result: '-vf transpose={} -y'.format(type)})
print(ff.cmd)
ff.run()
return result
#获得图片的后缀
def _check_format(image_path: str):
ext = os.path.basename(image_path).strip().split('.')[-1]
if ext not in ['png', 'jpg']:
raise Exception('format error')
return ext
'''-------------------------------------------------方法二-----------------------------------------------------'''
def method_two():
path='E:/project/Template_detection/Image_preprocessing/cake.jpg'
image = Image.open(path)
image.show()
#左右水平翻转
#out =image.transpose(Image.FLIP_LEFT_RIGHT)
#上下翻转
#out = image.transpose(Image.FLIP_TOP_BOTTOM)
#顺时针旋转90度
#out = image.transpose(Image.ROTATE_90)
#逆时针旋转45度
out = image.rotate(45)
out.save('E:/project/Template_detection/Image_preprocessing/cake_5.png','png')
'''--------------------------------------------------方法三--------------------------------------------------------'''
def method_three():
path = 'E:/project/Template_detection/Image_preprocessing/cake.jpg'
image = cv.imread(path)
'''改变图像的大小'''
image1 = cv.resize(image,(400,400))
'''图像旋转
rows, cols, chnl = image.shape
# 旋转参数:旋转中心,旋转角度,scale
#旋转角度'-'顺时针旋转,'+'逆时针旋转
M = cv.getRotationMatrix2D((cols / 2, rows / 2), -60, 1)
# 参数:原始图像,旋转参数,元素图像宽高
rotated = cv.warpAffine(image, M, (cols, rows))
# 图片显示
cv.imshow("rotated", rotated)
cv.imshow("image", image)
# 等待窗口
cv.waitKey(0)
cv.destroyAllWindows()'''
'''图像翻转
# 灰度处理
scr = cv.cvtColor(image, cv.COLOR_BGR2RGB)
# 图像翻转
# 0以X轴对称翻转,>0以Y轴对称翻转,<0以X轴Y轴同时翻转
image1 = cv.flip(scr, 0)
image2 = cv.flip(scr, 1)
image3 = cv.flip(scr, -1)
# 图像显示
titles = ["image", "image1", "image2", "image3"]
images = [scr, image1, image2, image3]
for i in range(4):
#subplot(2, 2, 1)指的是在一个2行2列共4个子图的图中,定位第1个图来进行操作。最后的数字就是表示第几个子图,此数字的变化来定位不同的子图。
plt.subplot(2, 2, i + 1), plt.imshow(images[i])
plt.xticks([]), plt.yticks([])
plt.title(titles[i])
plt.show()'''
'''图像平移'''
image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
rows, cols, chnl = image.shape
# 图片下,上,右,左平移
M = np.float32([[1, 0, 0], [0, 1, 100]])
image1 = cv.warpAffine(image, M, (cols, rows))
M = np.float32([[1, 0, 0], [0, 1, -100]])
image2 = cv.warpAffine(image, M, (cols, rows))
M = np.float32([[1, 0, 100], [0, 1, 0]])
image3 = cv.warpAffine(image, M, (cols, rows))
M = np.float32([[1, 0, -100], [0, 1, 0]])
image4 = cv.warpAffine(image, M, (cols, rows))
# 图像显示
tieles = ["image1", "image2", "image3", "image4"]
images = [image1, image2, image3, image4]
for i in range(4):
plt.subplot(2, 2, i + 1), plt.imshow(images[i])
plt.xticks([]), plt.yticks([])
plt.title(tieles[i])
plt.show()
'''--------------------------------------------------开始-----------------------------------------------------'''
if __name__ == '__main__':
#print(hflip('E:/project/Template_detection/Image_preprocessing/cake.jpg', 'E:/project/Template_detection/Image_preprocessing/'))
#method_two()
method_three()
实验图片:
翻转结果:
栗子就举这一个了。
每个方法实践要注释其他方法,不然会出错。
加油,追梦人!



