4

我有一堆报纸,我的目标是围绕几个 ROI 获得边界框。具体来说,这些投资回报率是:

  • 文章
  • 标题
  • 图片

我正在使用带有 python 的 opencv 来实现所需的结果。我的方法如下:

  1. Canny 边缘检测(由图像模糊处理)
  2. 扩张
  3. 轮廓检测
  4. 轮廓近似和边界框

我已经编写了适当的代码来实现这一点,但轮廓不是那么准确(后面会详细介绍)。示例:我正在展示代码的行为方式:

在此处输入图像描述

如您所见,它并没有检测到那里的所有文章,而是将几篇文章的部分组合到一个边界框中。我怎样才能使它变得更好?我希望它更准确。我尝试了 Canny、Dilation 和模糊的参数,但没有获得更好的结果。这是我的代码:

import cv2
import imutils
import numpy as np
import random

# capture image
path = 'C:/Users/96171/Desktop/dataset_training/jpg/75120201.jpg'
image = cv2.imread(path)
image = imutils.resize(image, width=500)
resize_factor = 1


blur = cv2.GaussianBlur(image, (9, 9), 0)
cv2.imshow('Blurred', blur)
cv2.waitKey()

# this was the golden line that made it better
# edged = cv2.Canny(blur, 0, 150)
edged = cv2.Canny(blur, 0, 170)

cv2.imshow('Edged', edged)
cv2.waitKey()

# edged = cv2.Canny(image, 0, 150)
# cv2.imshow("Edged", edged)
# cv2.waitKey()

# dilated = cv2.dilate(edged, np.ones((15, 15)))
dilated = cv2.dilate(edged, np.ones((3, 3)), iterations=1)
cv2.imshow('Dilated', dilated)
cv2.waitKey()


def _contour_approx_bad(contour, *args, **kwargs):
    """
    Approximate contour and discard non rectangular contours
    :returns: True if rectangle else False
    """
    perimeter = cv2.arcLength(contour, True)
    approx = cv2.approxPolyDP(contour, 0.02 * perimeter, True)

    return len(approx) == 4


contours, _ = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for i, contour in enumerate(contours):
    if not _contour_approx_bad(contour):
        rect = cv2.boundingRect(contour)
        x, y, w, h = [r*resize_factor for r in rect]
        b, g = random.sample(range(0, 255), 2)
        cv2.rectangle(image, (x,y), ((x+w), (y+h)), (b, g, 255), 3)
        # self.crop(name=str(i), **{'start': (x,y), 'end': ((x+w), (y+h))})

cv2.imshow('Final Image', image)
cv2.waitKey()
cv2.imwrite('tobecropped.png', image) 
4

0 回答 0