手撕 实现iou,nms等

自己写的,大致思路的实现

1.nms

import numpy

def bbox_iou(box1, box2):
    """
    box1: [N,4] [x1,y1, x2,y2]
    box2: [M,4]
    """
    a1 = max(box1[0], box2[0])
    b1 = max(box1[1], box2[1])
    a2 = min(box1[2], box2[2])
    b2 = min(box1[3], box2[3])
    inter_aera = (a2-a1)*(b2-b1)
    out_aera = (box1[2]-box1[0])*(box1[3]-box1[1]) + (box2[2]-box2[0])*(box2[3]-box2[1]) - inter_aera
    return inter_aera / out_aera

def nms(bboxes, threshold=0.5):
    """
    bboxes: [N,4] [x1,x2,y1,y2,  confidence]
    """
    sorted(bboxes, key=lambda x:-x[-1])         #按confidence降序
    # 求iou, >0.5则删除

    for i in range(1, len(bboxes)):
        iou = bbox_iou(bboxes[i][:-1], bboxes[0][:-1])      # 计算iou
        if iou>threshold:
            bboxes = np.delete(bboxes, i, axis=0)           # 删除
    
    return bboxes

2. 移动均值滤波

def moving_average(transforms_raw_data, windowsize):
    transforms_smooth = np.copy(transforms_raw_data)
    window = np.ones(int(windowsize)) / float(windowsize)
    for i in range(3):
        transforms_smooth[:, i] = np.convolve(transforms_raw_data[:, i], window, 'same')
    return transforms_smooth[int(windowsize/2)-1]

3. 快速排序

"""
快速排序_2023.11.06

"""



def fastsort2(arr):
    def order(arr, left, right):
        temp = arr[left]
        cur_left = left
        cur_right = right

        while cur_left < cur_right:
            while cur_left < cur_right and arr[cur_right]>=temp:
                cur_right -= 1
            arr[cur_left] = arr[cur_right]
            while cur_left < cur_right and arr[cur_left]<=temp:
                cur_left += 1
            arr[cur_right] = arr[cur_left]
        
        # 找到位置
        arr[cur_left] = temp
        if left=temp:
                cur_right -= 1
            if cur_left left:
            order(arr, left, cur_left-1)
        if cur_left+1 < right:
            order(arr, cur_left+1, right)


    order(input_arr, left=0, right=len(input_arr)-1)


if __name__ =="__main__":
    a = [49, 38, 65, 97, 76, 13, 27, 49]
    # a = [49, 38, 65, 97]
    fastsort(a)
    print("最终结果:", a)

你可能感兴趣的:(Leetcode刷题,深度学习)