我正在尝试在文档中的每个单词上放置边框。我已经成功地在每个字母上或多或少地放置了边界框,但是无法在每个单词上都置入边界框。我在下面使用相同的代码
im_ns = cv.imread('image.jpg')
gray = cv.cvtColor(im_ns,cv.COLOR_BGR2GRAY)
blurred_g = cv.GaussianBlur(gray,(11,11),0)
ret, th1 = cv.threshold(blurred_g,127,255,cv.THRESH_BINARY)
th2 = cv.adaptiveThreshold(blurred_g,255,cv.ADAPTIVE_THRESH_MEAN_C,cv.THRESH_BINARY,11,2)
th3 = cv.adaptiveThreshold(blurred_g,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY,11,2)
th3_1 = cv.adaptiveThreshold(th3,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY,11,2)
plt.figure(figsize=(30,25))
plt.imshow(th3_1)
th3_er = cv.erode(th3_1,None,iterations=1)
th3_di = cv.dilate(th3_er,None,iterations=1)
labels = measure.label(th3_di1, neighbors=8, background=255)
mask = np.zeros(th3_di1.shape, dtype="uint8")
for lab in np.unique(labels):
if lab == 0:
continue
labelMask = np.zeros(th3_di.shape, dtype="uint8")
labelMask[labels == lab] = 255
numPixels = cv.countNonZero(labelMask)
if numPixels > 13:
mask = cv.add(mask, labelMask)
cnts = cv.findContours(mask.copy(), cv.RETR_EXTERNAL,cv.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
cnts = contours.sort_contours(cnts)[0]
t = th3_di.copy()
for (i, c) in enumerate(cnts):
(x,y,w,h) = cv.boundingRect(c)
cv.rectangle(t,(x,y),(x+w,y+h),(0,255),2)
cv.imshow("Image", t)
cv.waitKey(10000)
cv.destroyAllWindows()