#!/usr/bin/python3# -*- coding: utf-8 -*-import cv2import numpy as npimport timedef Rotate(img, angle=0.0,fill=0):"""旋转:param img:待旋转图像:param angle: 旋转角度:param fill:填充方式,默认0黑色填充:return: img: 旋转后的图像"""w, h = img.shape[:2]center = (int(w / 2), int(h / 2))rot = cv2.getRotationMatrix2D(center, angle, 1.0)img = cv2.warpAffine(img, rot, (h, w), borderValue=fill)return imgdef CalcAngle(img):h, w = img.shape[:2]x1, y1, x2, y2 = 0, 0, 0, 0angle = 0for i in range(h - 1):if img[i][int(w / 3)] == 0 and img[i - 1][int(w / 3)] != 0:# print("1",int(w/3),i)x1, y1 = int(w / 3), iif img[i][int(w * 2 / 3)] == 0 and img[i - 1][int(w * 2 / 3)] != 0:# print("2",int(w*2/3),i)x2, y2 = int(w * 2 / 3), iif x1 != 0 and y1 != 0 and x2 != 0 and y2 != 0:if x2 - x1 == 0 or y2 - y1 == 0:print(u"不需要旋转")return 0else:length = (y2 - y1) / (x2 - x1)angle = np.arctan(length) / 0.017453if angle  45:angle = angle - 90else:passprint(u"旋转角度:", angle)return anglestarts = time.clock()img1=cv2.imread("box.jpg",0)# img=Rotate(img1,2,255)ret,img=cv2.threshold(img1,200,255,cv2.THRESH_BINARY)# cv2.imshow("0",img)img = cv2.Canny(img, 10, 255, apertureSize=3)angle=CalcAngle(img)img=Rotate(img1,angle)ends = time.clock()print("time", ends - starts, "秒")cv2.imwrite("00.jpg",img)# cv2.imshow("00",img)cv2.waitKey(0)cv2.destroyAllWindows()

使用两张测试图片如下:

对于lena的图像测试结果如下:

另一张测试图片结果如下:

也可以使用下面代码进行测试:

#!/usr/bin/python3# -*- coding: utf-8 -*-import cv2import timeimport numpy as npdef Location(img, tmp, threshold_value=120, dilate=3, resize_multiple=16):"""图像定位:param img: 输入原图:param tmp: 定位匹配模板:param threshold_value: 图像阈值:param dilate: 膨胀值:param resize_multiple:缩小倍率:return: rect:矩形坐标点,从右上xy到右下xy,四个值"""h, w = img.shape[:2]hy, wx = tmp.shape[:2]img = cv2.resize(img, (int(w * 1 / resize_multiple), int(h * 1 / resize_multiple)), interpolation=cv2.INTER_AREA)kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))img = cv2.erode(img, kernel, iterations=dilate)w, h = img.shape[:2]for i in range(w):for j in range(h):if img[i][j] >= threshold_value:img[i][j] = 255else:img[i][j] = 0res = cv2.matchTemplate(img, tmp, cv2.TM_SQDIFF_NORMED)min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)top_left = min_loc# bottom_right = ((top_left[0] + wx) * resize_multiple, (top_left[1] + hy) * resize_multiple)# top_left = (top_left[0] * resize_multiple, top_left[1] * resize_multiple)rect = [top_left[0] * resize_multiple, top_left[1] * resize_multiple, (top_left[0] + wx) * resize_multiple,(top_left[1] + hy) * resize_multiple]return rectdef RotateAngle(img, threshold_value=120, dilate=3,linenum=6):"""计算图像旋转角度:param img: 输入图像:param threshold_value: 阈值分割:param dilate: 膨胀值:return: angle: 旋转角度"""ret,img=cv2.threshold(img,threshold_value,255,cv2.THRESH_BINARY)img_w, img_h = img.shape[:2]# kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 2))# img = cv2.erode(img, kernel, iterations=dilate)line_widthsize = int(img_w)line_lensize = int(img_h / linenum)edges = cv2.Canny(img, 10, 255, apertureSize=3)try:lines = cv2.HoughLinesP(edges, 1, np.pi / 180, line_lensize, minLineLength=int(line_widthsize / 2),maxLineGap=line_widthsize)for line in lines[0]:# print("角度测量的直线坐标", line)x1, y1, x2, y2 = lineif x2 - x1 == 0 or y2 - y1 == 0:print(u"不需要旋转")return 0else:length = (y2 - y1) / (x2 - x1)angle = np.arctan(length) / 0.017453if angle  45:angle = angle - 90else:passprint(u"旋转角度:", angle)return angleexcept:return 0def Rotate(img, angle=0.0):"""旋转:param img:待旋转图像:param angle: 旋转角度:return: img: 旋转后的图像"""w, h = img.shape[:2]center = (int(w / 2), int(h / 2))rot = cv2.getRotationMatrix2D(center, angle, 1.0)img = cv2.warpAffine(img, rot, (h, w), borderValue=255)return imgdef GetObject_Location(img, tmp, threshold_value=120, dilate=3, resize_multiple=16):"""旋转:param img:图像:param tmp: 模板:param threshold_value:阈值:param dilate: 膨胀值:param resize_multiple:缩放倍数:return:"""rect = Location(img, tmp, threshold_value, dilate, resize_multiple)imgout = img[rect[1]:rect[3], rect[0]:rect[2]]angle = RotateAngle(imgout, threshold_value, dilate, resize_multiple, linenum=6)img = Rotate(imgout, angle)return imgdef SaveTemple(img, file_name=".\\data\\Temple1.jpg", threshold_value=200, dilate=3, resize_multiple=16):"""模板生成存储:param img: 输入图像:param file_name: 模板保存地址:param threshold_value: 阈值分割:param dilate: 膨胀值:return: img: 保存模板图片到本地"""h, w = img.shape[:2]img = cv2.resize(img, (int(w * 1 / resize_multiple), int(h * 1 / resize_multiple)), interpolation=cv2.INTER_AREA)img_w, img_h = img.shape[:2]print(img_w, img_h)# 创建标准模板imgout = np.zeros((img_w + 4, img_h + 4, 1), np.uint8)# 图像初始化白色for i in range(img_w + 4):for j in range(img_h + 4):imgout[i][j] = 255# 图像二值化for i in range(img_w):for j in range(img_h):if img[i][j] >= threshold_value:img[i][j] = 255else:img[i][j] = 0kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))img = cv2.erode(img, kernel, iterations=dilate)for i in range(img_w):for j in range(img_h):if img[i][j] >= threshold_value:passelse:imgout[i + 2][j + 2] = 0cv2.imwrite(file_name, imgout)"""一次切割,根据投影切割"""def FirstCutting(img, Cvalue, Cerode, LineNum, LineNum1):(_, thresh) = cv2.threshold(img, Cvalue, 255, cv2.THRESH_BINARY)kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))outimg = cv2.erode(thresh, kernel, iterations=Cerode)height, width = outimg.shape[:2]z = [0] * heightv = [0] * widthhfg = [[0 for col in range(2)] for row in range(height)]lfg = [[0 for col1 in range(2)] for row1 in range(width)]Box = []linea = 0BlackNumber = 0for y in range(height):for x in range(width):cp = outimg[y][x]if cp == 0:linea = linea + 1BlackNumber += 1else:continuez[y] = linealinea = 0inline, start, lineNumber = 1, 0, 0for i in range(0, height):if inline == 1 and z[i] >= LineNum:start = iinline = 0elif (i - start > 3) and z[i] = LineNum1:start1 = i1incol = 0elif (i1 - start1 > 3) and v[i1] = threshold:img[i][j] = 255else:img[i][j] = 0kernel = cv2.getStructuringElement(cv2.MORPH_RECT, KernelValue1)outimg = cv2.erode(img, kernel, iterations=KernelValue)outimg = cv2.dilate(outimg, kernel, iterations=KernelValue)return outimg"""根据投影计算出来的坐标进行数组切割"""starts = time.clock()img = cv2.imread("lena.jpg", 0)# img=Rotate(img,2)angle=RotateAngle(img,200)print(angle)img=Rotate(img,angle)cv2.imwrite("00.jpg",img)ends = time.clock()print("time", ends - starts, "秒")# img=cv2.imread("formal.bmp",0)# SaveTemple(img)

lena结果如下:

美女图片测试结果:

说明:以上代码仅仅是讲解介绍了图像旋转的计算及矫正原理,实际上准确度受不同图像的影响较大,不过里面使用的相关图像变换的函数值得借鉴参考学习。