【发布时间】:2020-06-22 16:21:06
【问题描述】:
我正在尝试使用cv2.HoughLines 来识别此image 中单词的倾斜角度。
但是,在边缘检测之后,它clearly has too much noise。
我尝试使用cv2.medianBlur 来消除噪音。
但是,有even more noise。
这意味着我无法为霍夫变换设置最小线长阈值。
我还应该查看哪些其他功能?
编辑:在 Rotem 的帮助下,我的代码现在可以识别倾斜角度在 90 到 -90 度之间的图像,包括 90 度但不包括 -90 度。
import numpy as np
import imutils
import math
import pytesseract
img = cv2.imread('omezole.jpg')
resized = imutils.resize(img, width=300)
gray = cv2.cvtColor(resized,cv2.COLOR_BGR2GRAY)
th3 = cv2.threshold(gray, 80, 255, cv2.THRESH_BINARY_INV)[1]
minLineLength = 50
maxLineGap = 3
lines = cv2.HoughLinesP(th3, rho=1, theta=np.pi/180, threshold=100, minLineLength=minLineLength, maxLineGap=maxLineGap)
colLineCopy = cv2.cvtColor(th3,cv2.COLOR_GRAY2BGR)
#Draw but remove all vertical lines, add corresponding angle to ls
ls = []
for line in lines:
if line is None:
angle = 0
else:
x1, y1, x2, y2 = line[0].tolist()
print(line)
#check for vertical lines since you can't find tan90
if (x2-x1==0):
ls.append(-90)
else:
ls.append((math.degrees(math.atan((y2-y1)/(x2-x1)))))
cv2.line(colLineCopy, (x1,y1), (x2,y2), (0,0,250), 2)
#special case of strictly vertical words, if more than 0.2 of the lines are vertical assume, words are vertical
if ls.count(-90)>len(ls)//5:
angle = 90
else:
for angle in ls:
if angle < -80:
ls.remove(angle)
angle = sum(ls)/len(ls)
rotated = imutils.rotate_bound(resized, -angle)
cv2.imshow("HoughLinesP", colLineCopy)
cv2.imshow("rotated", rotated)
gray = cv2.cvtColor(rotated, cv2.COLOR_BGR2GRAY)
threshINV = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY_INV)[1]
cv2.imshow("final", threshINV)
#Run OCR
pytesseract.tesseract_cmd = r'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
custom_config = r'--psm 11'
print(pytesseract.image_to_string(threshINV, config = custom_config))
cv2.waitKey(0)
cv2.destroyAllWindows
``
【问题讨论】:
-
你能发布你的代码吗?
-
请阅读minimal reproducible example,然后阅读edit 您的帖子以包含其中之一。显示您的代码将大大简化提供答案的过程。你的问题不是“噪音”,你可能只需要找到一个合适的阈值。
-
看我的回答stackoverflow.com/questions/36254219/…选择非本地方式或去噪
标签: python opencv image-processing ocr