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

问题描述 投票:1回答:1

现在,我正在尝试创建一个程序,该程序可以从背景中删除文本,但是我在处理程序时遇到很多问题

我的方法是使用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 back to the source image

现在的问题是,我无法完全删除文本图片:enter image description here

现在,我不确定我可以使用什么其他方法从图像中删除文本,对此我是陌生的,这就是我面临问题的原因。任何帮助,不胜感激

注意:由于我调整了图像的大小以使其以屏幕尺寸显示,因此图像看起来很拉伸

原始图像:

enter image description here

python image opencv image-processing computer-vision
1个回答
0
投票
这是使用形态学运算和轮廓滤波的方法

    将图像转换为灰度
  • 大津获取二进制图像的阈值
  • 执行变形以将单词连接成单个轮廓
  • 扩张以确保轮廓中包含所有文本位
  • 使用轮廓区域查找轮廓并进行过滤
  • 通过用背景色“填充”轮廓矩形来删除文本

  • 我使用了chrome开发工具来确定图像的背景颜色为(222,228,251)。我建议您cv2.imshow()每一步都了解思想过程。结果是

    enter image description here

    import cv2 image = cv2.imread('1.jpg') gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1] close_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15,3)) close = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, close_kernel, iterations=1) dilate_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,3)) dilate = cv2.dilate(close, dilate_kernel, iterations=1) cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if len(cnts) == 2 else cnts[1] for c in cnts: area = cv2.contourArea(c) if area > 800 and area < 15000: x,y,w,h = cv2.boundingRect(c) cv2.rectangle(image, (x, y), (x + w, y + h), (222,228,251), -1) cv2.imshow('image', image) cv2.waitKey()

  • © www.soinside.com 2019 - 2024. All rights reserved.