【问题标题】:How to increase size of bounding box using find contour open cv?如何使用查找轮廓opencv增加边界框的大小?
【发布时间】:2020-06-03 23:57:59
【问题描述】:

我已经使用 MSER 方法为文本区域准备了边界框。我只能为一个边界框增加框大小。问题是我想使用查找轮廓方法增加所有预测边界框的大小。在这里我将附上我的代码。

import cv2
import numpy as np

mser = cv2.MSER_create()
img = cv2.imread("C:/Users/Mani/Desktop/img/87.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
vis = img.copy()

coordinates, bboxes = mser.detectRegions(gray)


for bbox in bboxes:
    x, y, w, h = bbox
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)

cx = x + w//2
cy = y + h//2
cr = max(w,h)//2

dr = 10
idx=0
for i in bbox:
    idx+=1
    r = cr + i*dr
    cv2.rectangle(vis,(cx-r,cy-r),(cx+r,cy+r),(0,255,0),2)
    croped =img[cy-r:cy+r,cx-r:cx+r]
    cv2.imshow("croped{}".format(i), croped)

【问题讨论】:

    标签: python opencv deep-learning


    【解决方案1】:

    您总是选择bboxes 中的最后一个。要处理它们,您可以将裁剪代码添加到第一个 for 循环:

    dr = 10
    idx=0
    
    for bbox in bboxes:
        x, y, w, h = bbox
        cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
    
        cx = x + w//2
        cy = y + h//2
        cr = max(w,h)//2
    
        idx+=1
        r = cr + i*dr
        cv2.rectangle(vis,(cx-r,cy-r),(cx+r,cy+r),(0,255,0),2)
        croped =img[cy-r:cy+r,cx-r:cx+r]
        cv2.imshow("croped{}".format(i), croped)
    

    【讨论】:

    • 感谢您的回复.. 我已经尝试过您的建议,但我收到类似“ cv2.rectangle(img,(cx-r,cy-r),(cx+r,cy+) 的错误r),(0,255,0),1) TypeError: an integer is required (got type tuple)"
    • 我不确定,但我猜img 是灰度的,所以你必须输入一个单一的颜色整数,比如 cv2.rectangle(img,(cx-r,cy-r), (cx+r,cy+r),127,1)。 print要确定的输入值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-09
    • 2018-09-29
    • 2020-06-17
    • 1970-01-01
    • 2014-06-17
    • 2022-08-05
    相关资源
    最近更新 更多