• cv2在图像上画不同比例的锚框


    '''
            cv2在图像上画不同比例的锚框
    '''
    import cv2
    import math
    
    # 画宽高比1:1的锚框
    def display_11_anchor(img,anchor_11_left_top,anchor_11_right_bottom):
        cv2.rectangle(img,
                  anchor_11_left_top,
                  anchor_11_right_bottom,
                  (0, 255, 0),      # 颜色:G
                  2)
    
    # 画宽高比1:2的锚框
    def display_12_anchor(img,anchor_12_left_top,anchor_12_right_bottom):
        cv2.rectangle(img,
                  anchor_12_left_top,
                  anchor_12_right_bottom,
                  (255, 0, 0),      # 颜色:B
                  2)
    
    # 画宽高比2:1的锚框
    def display_21_anchor(img,anchor_21_left_top,anchor_21_right_bottom):
        cv2.rectangle(img,
                  anchor_21_left_top,
                  anchor_21_right_bottom,
                  (0, 0, 255),      # 颜色:R
                  2)
    
    # 画出所有锚框,size代表锚框面积占一个网格面积的百分比
    def display_anchor(img,anchor_rows,anchor_columns,size):
        heigh,width,channel = img.shape
        area=heigh*width    # 图片面积
        grid_area=int(area//(anchor_rows*anchor_columns)*size)  # 网格面积
        anchor_11_width=math.sqrt(grid_area)    # 宽高比1:1的锚框宽度
        anchor_11_heigh=anchor_11_width         # 宽高比1:1的锚框高度
        anchor_12_width=math.sqrt(grid_area/2)  # 宽高比1:2的锚框宽度
        anchor_12_heigh=2*anchor_12_width       # 宽高比1:2的锚框高度
        anchor_21_width=anchor_12_heigh         # 宽高比2:1的锚框宽度
        anchor_21_heigh=anchor_12_width         # 宽高比2:1的锚框高度
    
        grid_edge_width=width//anchor_columns   # 一个网格的宽度
        grid_edge_heigh=heigh//anchor_rows      # 一个网格的高度
    
        for i in range(anchor_rows):    # 遍历所有的网格
            for j in range(anchor_columns):
                grid_left_top=[grid_edge_width*j,grid_edge_heigh*i]     # 网格的左上顶点
                grid_right_bottom=[grid_edge_width*(j+1),grid_edge_heigh*(i+1)]     # 网格的右下顶点
    
                grid_centre=[(grid_right_bottom[0]-grid_left_top[0])//2+grid_left_top[0],   # 找到网格的中心
                             (grid_right_bottom[1]-grid_left_top[1])//2+grid_left_top[1]]
    
                # 分别计算三种宽高比的锚框的左上和右下顶点
                anchor_11_left_top=(int(grid_centre[0]-anchor_11_width//2),int(grid_centre[1]-anchor_11_heigh//2))
                anchor_11_right_bottom=(int(grid_centre[0]+anchor_11_width//2),int(grid_centre[1]+anchor_11_heigh//2))
                anchor_12_left_top=(int(grid_centre[0]-anchor_12_width//2),int(grid_centre[1]-anchor_12_heigh//2))
                anchor_12_right_bottom=(int(grid_centre[0]+anchor_12_width//2),int(grid_centre[1]+anchor_12_heigh//2))
                anchor_21_left_top=(int(grid_centre[0]-anchor_21_width//2),int(grid_centre[1]-anchor_21_heigh//2))
                anchor_21_right_bottom=(int(grid_centre[0]+anchor_21_width//2),int(grid_centre[1]+anchor_21_heigh//2))
    
                # 根据顶点画出矩形
                display_11_anchor(img,anchor_11_left_top,anchor_11_right_bottom)
                display_12_anchor(img,anchor_12_left_top,anchor_12_right_bottom)
                display_21_anchor(img,anchor_21_left_top,anchor_21_right_bottom)
    
        cv2.imshow('image', img)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
    
    if __name__ == '__main__':
    
        img_path = 'catdog.jpg'
        img = cv2.imread(img_path)
        display_anchor(img,anchor_rows=6,anchor_columns=6,size=0.4)
    

    最终的显示效果如图:

    图1 宽高比:6:6
    图2 宽高比:3:6
    图3 宽高比:6:3
  • 相关阅读:
    7. Reverse Integer
    4. Median of Two Sorted Arrays
    微服务实战系列(四)-注册中心springcloud alibaba nacos-copy
    微服务实战系列(五)-注册中心Eureka与nacos区别-copy
    Restful、SOAP、RPC、SOA、微服务之间的区别-copy
    从SOA到RPC、SOAP、REST-copy
    spring-session实现分布式集群session的共享-copy
    深入理解 RESTful Api 架构-copy
    RESTful 架构详解-copy
    SpringBoot使用MyBatis-Generator详解-copy
  • 原文地址:https://www.cnblogs.com/gy77/p/15508329.html
Copyright © 2020-2023  润新知