图像矫正就是通过一些已知的参考点,即无失真图象的某些象素点和畸变图象相应象素的坐标间对应关系,拟合出映射关系中的未知系数,并作为恢复其它象素的基础。
1、矫正原理
在进行图片矫正时,有些图片具有小角度的倾斜(±45°以内),导致传入后续识别分类的模型时产生误差。可以利用文本图像具有行间空白的特性,对待检测图像进行角度旋转遍历,并同时进行水平方向像素值投影,当文本方向水平时,投影所得的0值最多。从而达到所要矫正的目的。
2、边缘投影
在参考点足够的情况下,推算全图变形函数,比如:当水平线倾斜时投影值比较小,但是当水平线正常时其在水平面上的投影是最大的。根据这个进行参考,倾斜的物体在水平方向上的投影只要经过旋转处理就可以达到最大,此时的图片就是正常。
3、基于边缘投影法的矫正过程
(1)对检测图像进行角度旋转遍历
(2)进行水平方向像素值投影
(3)找到投影所得的0值最多的方向
(4)对该方向进行矫正
用到的工具:opencv、numpy、matplotlib
首先获取图片的宽度以及高度
def
其次,寻找当前图片文本的旋转角度,在±90度之间
def findangle(_image):
toWidth = _image.shape[1]//2 #500
minCenterDistance = toWidth/20 #10
angleThres = 45
image = _image.copy()
h, w = image.shape[0:2]
if w > h:
maskW = toWidth
maskH = int(toWidth / w * h)
else:
maskH = toWidth
maskW = int(toWidth / h * w)
接着,使用黑色填充图片的其他区域
# 使用黑色填充图片区域
swapImage = cv2.resize(image, (maskW, maskH))
grayImage = cv2.cvtColor(swapImage, cv2.COLOR_BGR2GRAY)
gaussianBlurImage = cv2.GaussianBlur(grayImage, (3, 3), 0, 0)
histImage = cv2.equalizeHist(~gaussianBlurImage)
binaryImage = cv2.adaptiveThreshold(histImage, 1, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 15, -2)
然后,遍历角度时计算的关键点数量
pointsNum = np.sum(binaryImage!=0)//2
紧接着使用连接组件寻找并删除边框线条,计算每个连通区域坐上右下点的索引坐标与其质心的距离,距离大的即为线条
connectivity = 8
num_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(binaryImage, connectivity, cv2.CV_8U)
labels = np.array(labels)
maxnum = [(i, stats[i][-1], centroids[i]) for i in range(len(stats))]
maxnum = sorted(maxnum, key=lambda s: s[1], reverse=True)
if len(maxnum) <= 1:
return 0
for i, (label, count, centroid) in enumerate(maxnum[1:]):
cood = np.array(np.where(labels == label))
distance1 = np.linalg.norm(cood[:,0]-centroid[::-1])
distance2 = np.linalg.norm(cood[:,-1]-centroid[::-1])
if distance1 > minCenterDistance or distance2 > minCenterDistance:
binaryImage[labels == label] = 0
else:
break
最后,横向统计非零元素个数 越少则说明姿态越正
# 横向统计非零元素个数 越少则说明姿态越正
zeroCount = np.sum(hist > toWidth/50)
if zeroCount <= minCount or minCount == -1:
minCount = zeroCount
minRotate = rotate
return minRotate
完整代码
import cv2
import numpy as np
from matplotlib import pyplot as plt
def rotate_bound(image, angle):# 获取图片的宽高
(h, w) = image.shape[:2]
(cX, cY) = (w // 2, h // 2)
M = cv2.getRotationMatrix2D((cX, cY), angle, 1.0)
img = cv2.warpAffine(image, M, (w, h))
return img
def rotate_points(points, angle, cX, cY):
M = cv2.getRotationMatrix2D((cX, cY), angle, 1.0).astype(np.float16)
a = M[:, :2]
b = M[:, 2:]
b = np.reshape(b, newshape=(1, 2))
a = np.transpose(a)
points = np.dot(points, a) + b
points = points.astype(np.int)
return points
def findangle(_image):
toWidth = _image.shape[1]//2 #500
minCenterDistance = toWidth/20 #10
angleThres = 45
image = _image.copy()
h, w = image.shape[0:2]
if w > h:
maskW = toWidth
maskH = int(toWidth / w * h)
else:
maskH = toWidth
maskW = int(toWidth / h * w)
# 使用黑色填充图片区域
swapImage = cv2.resize(image, (maskW, maskH))
grayImage = cv2.cvtColor(swapImage, cv2.COLOR_BGR2GRAY)
gaussianBlurImage = cv2.GaussianBlur(grayImage, (3, 3), 0, 0)
histImage = cv2.equalizeHist(~gaussianBlurImage)
binaryImage = cv2.adaptiveThreshold(histImage, 1, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 15, -2)
pointsNum = np.sum(binaryImage!=0)//2
connectivity = 8
num_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(binaryImage, connectivity, cv2.CV_8U)
labels = np.array(labels)
maxnum = [(i, stats[i][-1], centroids[i]) for i in range(len(stats))]
maxnum = sorted(maxnum, key=lambda s: s[1], reverse=True)
if len(maxnum) <= 1:
return 0
for i, (label, count, centroid) in enumerate(maxnum[1:]):
cood = np.array(np.where(labels == label))
distance1 = np.linalg.norm(cood[:,0]-centroid[::-1])
distance2 = np.linalg.norm(cood[:,-1]-centroid[::-1])
if distance1 > minCenterDistance or distance2 > minCenterDistance:
binaryImage[labels == label] = 0
else:
break
minRotate = 0
minCount = -1
(cX, cY) = (maskW // 2, maskH // 2)
points = np.column_stack(np.where(binaryImage > 0))[:pointsNum].astype(np.int16)
for rotate in range(-angleThres, angleThres):
rotatePoints = rotate_points(points, rotate, cX, cY)
rotatePoints = np.clip(rotatePoints[:,0], 0, maskH-1)
hist, bins = np.histogram(rotatePoints, maskH, [0, maskH])
# 横向统计非零元素个数 越少则说明姿态越正
zeroCount = np.sum(hist > toWidth/50)
if zeroCount <= minCount or minCount == -1:
minCount = zeroCount
minRotate = rotate
return minRotate
Path = '003.jpg'
cv_img = cv2.imdecode(np.fromfile(Path, dtype=np.uint8), -1)
cv_img = cv2.cvtColor(cv_img, cv2.COLOR_RGB2BGR)
for agl in range(-30, 30):
img = cv_img.copy()
img = rotate_bound(img, agl)
angle = findangle(img)
img = rotate_bound(img, -angle)
angle = findangle(img)
cv2.imshow('after', img)
key = cv2.waitKey(1) & 0xFF
# 按'q'健退出循环
if key == ord('q'):
break
cv2.destroyAllWindows()
plt.imshow(img)
plt.show()
与原图效果进行比较
其他图片的矫正效果
相比较于用直线勘测的方法对图像进行矫正