我正在尝试识别图像中包含文本的部分。为此,我首先使用OpenCV(v.3)对图像进行预处理,然后将矩形/方框添加到文本部分。在

我下面的代码确实报告了一些轮廓。请参阅下面的代码、输入图像和输出。在

代码:import os,sys,cv2,pytesseract

## IMAGE

afile = "test-small.jpg"

def reader(afile):

aimg = cv2.imread(afile,0)

print("Image Shape%s | Size:%s" % (aimg.shape,aimg.size))

return aimg

def boundbox(aimg):

out_path2 = "%s-tagged.jpg" % (afile.rpartition(".")[0])

ret,thresh = cv2.threshold(aimg,127,255,0)

image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

acount = 0

for contour in contours:

acount+=1

x, y, w, h = cv2.boundingRect(contour)

print("Coordinates",x,y,w,h)

if w < 100 and h < 100: ## Avoid tagging small objects i.e. false positives

continue

cv2.rectangle(aimg, (x, y), (x + w, y + h), (255, 0, 0), 8) ##

print("Total contours found:%s" % (acount))

cv2.imwrite(out_path2,aimg)

return out_path2

def main():

aimg = reader(afile)

bimg = boundbox(aimg)

if __name__ == '__main__':

main()

测试图像:

GUWC2.jpg

输出:

15RQm.jpg

问题是:(1)矩形在图像上不可见,(2)文本部分的检测不准确。如何改进上述代码以检测文本部分?在

谢谢你的帮助。在

巴德

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐