【问题标题】:scanned text angle fix python using opencv [closed]使用opencv扫描文本角度修复python [关闭]
【发布时间】:2022-01-21 11:34:18
【问题描述】:

我不明白为什么转换不起作用,输出图像是:

所以阈值操作工作正常,但一定有某种问题 转变我似乎没有注意到。提前致谢!

import cv2
import numpy as np

#read the image, do binary processing
im = cv2.imread('scanned.png')
grey = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
cv2.imshow('gray', grey)
#the pixels are reversed to white and black
# gray = cv2.bitwise_not(gray)
ret, thresh = cv2.threshold(grey, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
cv2.imshow('thresh', thresh)


#calculate the minimum border that contains rotated text
coords = np.column_stack(np.where(thresh > 0))
print(coords)
#unction gives the rectangle border containing the whole text area, and the rotation angle of this border is the same as that of the text in the figure
angle = cv2.minAreaRect(coords)[-1]
print(angle)

#adjust the angle
if angle < -45:
  angle = -(90+ angle)
else:
  angle = -angle


#transformation
h, w = im.shape[:2]
center = (w//2, h//2)
print(angle)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated = cv2.warpAffine(im, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
cv2.putText(rotated, 'Angle: {:.2f} degrees'.format(angle), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)

print('[INFO] angle :{:.3f}'.format(angle))
cv2.imshow('Input', im)
cv2.imshow('Rotated', rotated)
cv2.waitKey(0)
cv2.destroyAllWindows()

【问题讨论】:

  • 绘制minarerect。使用boxPoints
  • 轮廓函数返回单个斑点的轮廓。这些要么是单独的字符,要么是……整个图像。您可以尝试通过应用形态膨胀/腐蚀使字符触摸来获取整个文本。
  • 我投票结束这个问题,因为这似乎是一项个人任务,例如家庭作业或工作

标签: python numpy opencv image-processing rotation


【解决方案1】:

应用 5x5 形态膨胀会产生大斑点,可以非常精确地找到其方向。尝试使用最大的。

【讨论】:

    猜你喜欢
    • 2018-04-08
    • 2018-11-14
    • 2021-07-12
    • 1970-01-01
    • 2016-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-04
    相关资源
    最近更新 更多