gpt4 book ai didi

python - 更改非最大抑制以选择Python中OpenCV的最小框

转载 作者:行者123 更新时间:2023-12-02 17:35:27 27 4
gpt4 key购买 nike

一些快速谷歌搜索将发现您这种用于非最大抑制的代码。

import numpy as np

# Malisiewicz et al.
def non_max_suppression_fast(boxes, overlapThresh):
if len(boxes) == 0:
return []

if boxes.dtype.kind == "i":
boxes = boxes.astype("float")
pick = []

x1 = boxes[:,0]
y1 = boxes[:,1]
x2 = boxes[:,2]
y2 = boxes[:,3]

area = (x2 - x1 + 1) * (y2 - y1 + 1)
idxs = np.argsort(y2)

while len(idxs) > 0:
last = len(idxs) - 1
i = idxs[last]
pick.append(i)

xx1 = np.maximum(x1[i], x1[idxs[:last]])
yy1 = np.maximum(y1[i], y1[idxs[:last]])
xx2 = np.minimum(x2[i], x2[idxs[:last]])
yy2 = np.minimum(y2[i], y2[idxs[:last]])

w = np.maximum(0, xx2 - xx1 + 1)
h = np.maximum(0, yy2 - yy1 + 1)

overlap = (w * h) / area[idxs[:last]]

idxs = np.delete(idxs, np.concatenate(([last],
np.where(overlap > overlapThresh)[0])))

return boxes[pick].astype("int")

这将为一组重叠的框返回最大的框,这些框的面积按功能中可以指示的百分比重叠。

您如何更改此设置以返回一组重叠的框中最小的框?

最佳答案

我认为以上代码是错误的。可能是您在复制代码时犯了一个小错误。但是,通过这个小小的改变就可以解决问题。
idxs = np.argsort(area)
要获得最小值,您只需要在上面的代码中进行以下更改:

# Malisiewicz et al.
def non_max_suppression_fast(boxes, overlapThresh):
if len(boxes) == 0:
return []

if boxes.dtype.kind == "i":
boxes = boxes.astype("float")
pick = []

x1 = boxes[:,0]
y1 = boxes[:,1]
x2 = boxes[:,2]
y2 = boxes[:,3]

area = (x2 - x1 + 1) * (y2 - y1 + 1)
idxs = np.argsort(area)

while len(idxs) > 0:
last = len(idxs) - 1
i = idxs[last]

xx1 = np.maximum(x1[i], x1[idxs[:last]])
yy1 = np.maximum(y1[i], y1[idxs[:last]])
xx2 = np.minimum(x2[i], x2[idxs[:last]])
yy2 = np.minimum(y2[i], y2[idxs[:last]])

w = np.maximum(0, xx2 - xx1 + 1)
h = np.maximum(0, yy2 - yy1 + 1)

overlap = (w * h) / area[idxs[:last]]
### Modification
selected_idx = np.where(overlap > overlapThresh)[0]
selected_idx = np.concatenate(([last], selected_idx))
min_area_idx = min(selected_idx, key=lambda i: area[i])
pick.append(min_area_idx)
### Modification end
idxs = np.delete(idxs, selected_idx)

return boxes[pick].astype("int")

关于python - 更改非最大抑制以选择Python中OpenCV的最小框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51599456/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com