小编Sha*_*ank的帖子

OpenCV:如何从背景中删除文本

现在我正在尝试创建一个程序,它从背景中删除文本,但我在通过它时遇到了很多问题

我的方法是使用 pytesseract 来获取文本框,一旦我得到文本框,我就使用 cv2.inpaint 来绘制它并从那里删除文本。简而言之:

d = pytesseract.image_to_data(img, output_type=Output.DICT) # Get text
n_boxes = len(d['level']) # get boxes
for i in range(n_boxes): #  Looping through boxes
    # Get coordinates
    (x, y, w, h) = (d['left'][i], d['top'][i], d['width'][i], d['height'][i])
    crop_img = img[y:y+h, x:x+w] # Crop image
    gray = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY)
    gray = inverte(gray) # Inverse it
    thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU)[1]
    dst = cv2.inpaint(crop_img, thresh, 10, cv2.INPAINT_TELEA) # Then Inpaint
    img[y:y+h, x:x+w] = dst # Place back cropped image …
Run Code Online (Sandbox Code Playgroud)

python opencv image image-processing computer-vision

4
推荐指数
1
解决办法
2774
查看次数