我扫描了图像,其中的文本需要找出文本和坐标(x,y,高度,宽度)或边界框(X左上,Y左上,X右上,Y右上,X右下,Y右下,X左下,Y左下)
我尝试的没有成功
使用cv2和numpy软件包获取坐标,请参见以下源代码
但无法获取文本
import cv2
import numpy as np
rgb = cv2.imread(r'C:\Users\tom\Desktop\scannedimage.jpg')
small = cv2.cvtColor(rgb, cv2.COLOR_BGR2GRAY)
#threshold the image
_, bw = cv2.threshold(small, 0.0, 255.0, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
# get horizontal mask of large size since text are horizontal components
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (20, 1))
connected = cv2.morphologyEx(bw, cv2.MORPH_CLOSE, kernel)
# find all the contours
contours,hierachy=cv2.findContours(connected.copy(),cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
#Segment the text lines
for idx in range(len(contours)):
x, y, w, h = cv2.boundingRect(contours[idx])
cv2.rectangle(rgb, (x, y), (x+w-1, y+h-1), (0, 255, 0), 2)
print(contours)
输出
[TEXT] [x,y,height,with]
OR