python - 如何使用查找轮廓打开 cv 来增加边界框的大小?
问题描述
我已经使用 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)
解决方案
你总是挑最后一个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)
推荐阅读
- c# - 编辑并继续不适用于 Roslyn 编译的类库
- javascript - 选择恢复默认值的方法
- flutter - 'List 类型的值
' 不能分配给类型为 'Iterable 的变量 ' - python - 如何从接受输入更改为检查列表中的输入?
- python - 如何在我的 CSV 文件中打印用户设置的两个日期之间的数据?
- javascript - 拒绝应用来自 'http://localhost:3000/style.css' 的样式,因为它的 MIME 类型('text/html')
- java - 从 Angular 应用程序检索访问令牌后尝试在 Java 中调用 Microsoft Graph API
- watchkit - 并发症系列支持 - 如果不支持,则不显示并发症系列
- angular - combineLatest 和严格类型的错误 - “没有重载匹配此调用。”
- javascript - 如何将捕获的视频流帧从 html 发送到烧瓶服务器?