# -*- coding: UTF-8 -*-
import numpy as np
import cv2
import matplotlib.pyplot as plt
import os
import copy
path = os.path.dirname(__file__)
os.chdir(path)
## 图片旋转
def rotate_bound(image, angle):
#获取宽高
(h, w) = image.shape[:2]
(cX, cY) = (w / 2, h / 2)
centor=(cX,cY) #旋转的中心点,设为图片的中心点
# 提取旋转矩阵 sin cos ,该角度定义为逆时针
M = cv2.getRotationMatrix2D(centor, angle, 1.0)
# 计算图像的新边界尺寸,图像会变大的
cos = np.abs(M[0, 0])
sin = np.abs(M[0, 1])
nW = int((h * sin) + (w * cos))
nH = int((h * cos) + (w * sin))
# 调整新的图像画布中心点
M[0, 2] += (nW / 2) - cX
M[1, 2] += (nH / 2) - cY
return cv2.warpAffine(image, M, (nW, nH),flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
## 获取图片旋转角度
def get_minAreaRect(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#gray = cv2.bitwise_not(gray)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
coords = np.column_stack(np.where(thresh > 0))
rect= cv2.minAreaRect(coords)
cent,wh,angle_t=rect;
box = cv2.boxPoints(rect)
box = np.int0(box)
cv2.drawContours(image, [box], 0, (255, 0, 0), 1)
cv2.imshow("canny2",image)
print(wh)
print(angle_t)
#角度定义为顺时针,且范围值为:[-90,0),为啥我测试的是个正的数值..
#关于该角度的问题,参考如下:
#https://blog.csdn.net/u010403272/article/details/78890410
#https://blog.csdn.net/qq_24237837/article/details/77850496
if(wh[0]
本文描述了如何检测图片内容的倾斜角度,然后旋转后转正,但是也遗留了一些问题:
1:如何判断内容是否上下翻转,可能后期要看ocr的结果
2:为啥2种调用minAreaRect的角度会不一样



