【发布时间】:2020-01-17 18:00:41
【问题描述】:
目前,我正在做一个 OCR 项目,我需要从标签上读取文本(参见下面的示例图片)。我遇到了图像倾斜问题,我需要帮助修复图像倾斜,以便文本是水平的而不是倾斜的。目前我正在使用的过程尝试从给定范围(代码包含在下面)中对不同的角度进行评分,但这种方法不一致,有时会过度校正图像倾斜或完全无法识别倾斜并纠正它。请注意,在进行歪斜校正之前,我将所有图像旋转 270 度以使文本直立,然后通过下面的代码传递图像。传递给函数的图像已经是二值图像了。
代码:
def findScore(img, angle):
"""
Generates a score for the binary image recieved dependent on the determined angle.\n
Vars:\n
- array <- numpy array of the label\n
- angle <- predicted angle at which the image is rotated by\n
Returns:\n
- histogram of the image
- score of potential angle
"""
data = inter.rotate(img, angle, reshape = False, order = 0)
hist = np.sum(data, axis = 1)
score = np.sum((hist[1:] - hist[:-1]) ** 2)
return hist, score
def skewCorrect(img):
"""
Takes in a nparray and determines the skew angle of the text, then corrects the skew and returns the corrected image.\n
Vars:\n
- img <- numpy array of the label\n
Returns:\n
- Corrected image as a numpy array\n
"""
#Crops down the skewImg to determine the skew angle
img = cv2.resize(img, (0, 0), fx = 0.75, fy = 0.75)
delta = 1
limit = 45
angles = np.arange(-limit, limit+delta, delta)
scores = []
for angle in angles:
hist, score = findScore(img, angle)
scores.append(score)
bestScore = max(scores)
bestAngle = angles[scores.index(bestScore)]
rotated = inter.rotate(img, bestAngle, reshape = False, order = 0)
print("[INFO] angle: {:.3f}".format(bestAngle))
#cv2.imshow("Original", img)
#cv2.imshow("Rotated", rotated)
#cv2.waitKey(0)
#Return img
return rotated
校正前后的标签示例图片
修正前->修正后
如果有人能帮我解决这个问题,那将有很大帮助。
【问题讨论】:
-
您可以尝试从轮廓框中获取有关单词的角度。见pyimagesearch.com/2017/02/20/text-skew-correction-opencv-python 或搜索谷歌。该主题有很多链接。
-
@fmw42 这个方法我已经试过了,这个方法不行,一直把图片调0度。您链接的方法仅适用于完美的文本图像,不幸的是,我正在使用的图像远非完美,因此该方法无法正确识别倾斜角度。
-
您是否查看过 Google 搜索中的其他方法?您是否尝试从每个单词的轮廓中获取边界框并查看角度分布或获得平均值?
-
嘿,@PeterS 感谢您的提问。我也在尝试使用 OpenCV 实现 OCR。我在这方面面临一些困难。因此,我想知道您是否可以分享您的 OCR 代码,我可以将其作为参考。这对我会有很大帮助。提前致谢:)
标签: python opencv image-processing computer-vision ocr