【问题标题】:How to remove all portrait pictures from a document如何从文档中删除所有肖像图片
【发布时间】:2019-09-12 16:13:43
【问题描述】:

我正在对文档图像进行 OCR。我想检测所有图片并从文档图像中删除。我想在文档图像中保留表格。一旦我检测到图片,我将删除然后想要 OCR。我试图找到轮廓试图检测所有更大的区域。不幸的是,它也检测到表格。还如何删除在文档图像中保留其他数据的对象。我正在使用 opencv 和 python

这是我的代码

import os
from PIL import Image
import pytesseract

img = cv2.imread('block2.jpg' , 0)
mask = np.ones(img.shape[:2], dtype="uint8") * 255


ret,thresh1 = cv2.threshold(img,127,255,0)
contours, sd = cv2.findContours(thresh1,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

areacontainer = []

for cnt in contours:
    area = cv2.contourArea(cnt)
    areacontainer.append(area)

avgArea = sum(areacontainer)/len(areacontainer)

    [enter code here][1]

for c in contours:# average area heuristics
    if cv2.contourArea(c)>6*avgArea:
        cv2.drawContours(mask, [c], -1, 0, -1)

binary = cv2.bitwise_and(img, img, mask=mask) # subtracting
cv2.imwrite("bin.jpg" , binary)
cv2.imwrite("mask.jpg" , mask) 

【问题讨论】:

  • 请问什么是“文档图像”?您还应该展示您尝试过的代码并提供所有材料,以便人们可以运行您的代码并更正/改进它。请参阅最小完整可验证示例stackoverflow.com/help/minimal-reproducible-example
  • 我已经添加了输入图片

标签: python image opencv image-processing computer-vision


【解决方案1】:

这是一种方法:

  • 将图像转换为灰度和高斯模糊
  • 执行精确边缘检测
  • 执行形态学运算以平滑图像
  • 使用最小/最大阈值区域查找轮廓和过滤
  • 移除纵向图片

这是检测到的以绿色突出显示的肖像

现在我们有了边界框 ROI,我们可以通过用白色填充图片来有效地移除图片。这是结果

import cv2

image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)
canny = cv2.Canny(blur, 120, 255, 1)

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
close = cv2.morphologyEx(canny, cv2.MORPH_CLOSE, kernel, iterations=2)

cnts = cv2.findContours(close, 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 > 15000 and area < 35000:
        x,y,w,h = cv2.boundingRect(c)
        cv2.rectangle(image, (x, y), (x + w, y + h), (255,255,255), -1)

cv2.imshow('image', image)
cv2.waitKey()

【讨论】:

  • 谢谢你。我还有另一个扩展问题。我有一个蒙版图像,其中包含我的 ROI 。什么是最好的选择坐标?
  • 选坐标是什么意思??
猜你喜欢
  • 1970-01-01
  • 2015-06-21
  • 1970-01-01
  • 2017-07-23
  • 2021-08-19
  • 2021-08-12
  • 1970-01-01
  • 2021-11-13
  • 2017-09-04
相关资源
最近更新 更多