我有以下输入图像:
我已经编写了以下代码,以在每个字符上添加框式批注:
import cv2
import numpy as np
import csv
import sys
img = cv2.imread('gray_image.png')
(h, w) = img.shape[:2]
image_size = h*w
mser = cv2.MSER_create()
mser.setMaxArea(int(image_size/2))
mser.setMinArea(10)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #Converting to GrayScale
_, bw = cv2.threshold(gray, 0.0, 255.0, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
regions, rects = mser.detectRegions(bw)
# With the rects you can e.g. crop the letters
for (x, y, w, h) in rects:
cv2.rectangle(img, (x, y), (x+w, y+h), color=(255, 0, 255), thickness=1)
cv2.imwrite('white_bg_new.jpg', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
此代码的输出:
我想提取出框内所有的项目,并在框外应用白色背景。我该怎么办?