『Python』图像金字塔、滑动窗口和非极大值抑制实现

2023-04-28,,

图像金字塔

1.在从cv2.resize中,传入参数时先列后行的

2.使用了python中的生成器,调用时使用for i in pyramid即可

3.scaleFactor是缩放因子,需要保证缩放后的图不小于最小尺寸,对应神经网络就是训练尺寸

'''图像金字塔'''
def resize(img, scaleFactor):
# cv2.resize先接收列后接收行,返回亦然
return cv2.resize(img, (int(img.shape[1] * (1/scaleFactor)),
int(img.shape[0] * (1/scaleFactor))),
interpolation=cv2.INTER_AREA)
def pyramid(image, scale=1.5, minSize = (200, 80)):
yield image while True:
image = resize(image, scale)
if image.shape[0] < minSize[1] or image.shape[1] < minSize[0]:
break
yield image

滑动窗口

'''滑动窗口'''
def sliding_window(image, stepSize, windowSize):
for y in range(0, image.shape[0], stepSize):
for x in range(0, image.shape[1], stepSize):
yield(x, y, image[y:y+windowSize[1], x:x+windowSize[0]])

极大值抑制

'''非极大值抑制'''
def non_max_suppression_fast(boxes, overlapThresh):
# 如果没有box,返回空list
if len(boxes) == 0:
return []
# 修改boxes的格式为float方便处理
if boxes.dtype.kind == 'i':
boxes = boxes.astype('float')
# 使用pick收集boxes
pick = []
x1 = boxes[:, 0]
y1 = boxes[:, 1]
x2 = boxes[:, 2]
y2 = boxes[:, 3]
scores = boxes[:, 4]
area = (x2 - x1 + 1) * (y2 - y1 + 1)
# 按照score从小到大的顺序排序indexes
idxs = np.argsort(scores)[::-1] while len(idxs) > 0:
# 分配最后一个(得分最高)index给i,并使用pick收集这个index(即i)
last = len(idxs) - 1
i = idxs[last]
pick.append(i)
# 在得分大于当前i的boxes中,
# 找到重合部分的左上点和右下点
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')

『Python』图像金字塔、滑动窗口和非极大值抑制实现的相关教程结束。

《『Python』图像金字塔、滑动窗口和非极大值抑制实现.doc》

下载本文的Word格式文档,以方便收藏与打印。