python数字图像处理的包
PIL
Pillow
OpenCV
scikit-image
等等
https://www.cnblogs.com/HL-space/p/10588423.html
import cv2
import sys
import time
import numpy as np
import imgaug as ia
from skimage.transform import radon
from imgaug import augmenters as iaa
import os
filePath = "s/"
for root, dirs, files in os.walk(filePath):
t1 = time.time()
print (len(files))#获取图片数量
j=0
for q in range(106):
if files[q].endswith('jpg'):
j=j+1
img=files[q]#获取图片名字
print(img)
img = cv2.imdecode(np.fromfile(filePath+img, dtype=np.uint8), -1)
w = img.shape[1]
img = cv2.copyMakeBorder(img, 0, 0, int(0.03*w), 0, cv2.BORDER_REPLICATE)
seq = iaa.Sequential([iaa.Multiply((2.2, 2.2), per_channel=1)])
Iy = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
I= seq.augment_image(Iy)
h, w = I.shape
if (w > 240):
I = cv2.resize(I, (240, int((h / w) * 240)))
I= I - np.mean(I) # Demean; make the brightness extend above and below zero
sinogram = radon(I)
r = np.array([np.sqrt(np.mean(np.abs(line) ** 2)) for line in sinogram.transpose()])
rotation = np.argmax(r) #index
print('Rotation: {:.2f} degrees'.format(90 - rotation))
degree=90-rotation
M = cv2.getRotationMatrix2D((w/2, h/2), 90 - rotation, 1)
t2 = time.time()
print(t2-t1)
dst = cv2.warpAffine(img, M, (w, h))
if (degree < 5 and degree > -5):
crop = dst[int(0.04 * h):int(0.97 * h), 0:int(0.99 * w)]
elif(degree<=12 and degree>=-12):
crop = dst[int(0.18 * h):int(0.82 * h), 0:int(0.99*w)]
elif (degree >= 30 or degree <= -30):
crop = dst[int(0.30 * h):int(0.66 * h), 0:int(0.99 * w)]
elif (degree >= 27 or degree <= -27):
crop = dst[int(0.28 * h):int(0.70 * h), 0:int(0.99*w)]
elif (degree >= 25 or degree <= -25):
crop = dst[int(0.26 * h):int(0.72 * h), 0:int(0.99 * w)]
else:
crop = dst[int(0.22 * h):int(0.78 * h), 0:int(0.99*w)]
cv2.imencode('.jpg', crop)[1].tofile("E:/s1/"+files[q])