切片辅助超推理-sahi库-slice_image使用

news2024/12/25 9:02:18

代码地址:https://github.com/obss/sahi

slice_image是sahi库中一个函数,理解这个函数是理解切片的入口。

一、官方函数使用示例

from sahi.slicing import slice_image

image_path=r'small-vehicles1.jpeg'
output_dir=r'ashi_result'
output_file_name='1-'

#手动设置slice_height,slice_width,overlap_height_ratio,overlap_width_ratio
def set_cfg():
    slice_image_result = slice_image(
        image=image_path,
        output_file_name=output_file_name,
        output_dir=output_dir,
        slice_height=256,
        slice_width=256,
        overlap_height_ratio=0.2,
        overlap_width_ratio=0.2,
    )


#根据图片自动计算slice_height,slice_width,overlap_height_ratio,overlap_width_ratio
def auto_cfg():
    slice_image_result = slice_image(
        image=image_path,
        output_file_name=output_file_name,
        output_dir=output_dir
    )


# set_cfg()
auto_cfg()

原图:

自动切片图:

二、slice_image函数梳理

slice_image位于slicing.py中,在此,仅对于图片进行分析,标注coco格式切片后续有时间另说。

slice_image中最重要的是get_slice_bboxes和get_auto_slice_params

1、get_slice_bboxes函数

def get_slice_bboxes(
    image_height: int,
    image_width: int,
    slice_height: Optional[int] = None,
    slice_width: Optional[int] = None,
    auto_slice_resolution: bool = True,
    overlap_height_ratio: float = 0.2,
    overlap_width_ratio: float = 0.2,
) -> List[List[int]]:
    """Slices `image_pil` in crops.
    Corner values of each slice will be generated using the `slice_height`,
    `slice_width`, `overlap_height_ratio` and `overlap_width_ratio` arguments.

    Args:
        image_height (int): Height of the original image.
        image_width (int): Width of the original image.
        slice_height (int, optional): Height of each slice. Default None.
        slice_width (int, optional): Width of each slice. Default None.
        overlap_height_ratio(float): Fractional overlap in height of each
            slice (e.g. an overlap of 0.2 for a slice of size 100 yields an
            overlap of 20 pixels). Default 0.2.
        overlap_width_ratio(float): Fractional overlap in width of each
            slice (e.g. an overlap of 0.2 for a slice of size 100 yields an
            overlap of 20 pixels). Default 0.2.
        auto_slice_resolution (bool): if not set slice parameters such as slice_height and slice_width,
            it enables automatically calculate these params from image resolution and orientation.

    Returns:
        List[List[int]]: List of 4 corner coordinates for each N slices.
            [
                [slice_0_left, slice_0_top, slice_0_right, slice_0_bottom],
                ...
                [slice_N_left, slice_N_top, slice_N_right, slice_N_bottom]
            ]
    """
    slice_bboxes = []
    y_max = y_min = 0
    # 手动设置切片宽高
    if slice_height and slice_width:
        y_overlap = int(overlap_height_ratio * slice_height)
        x_overlap = int(overlap_width_ratio * slice_width)
    # 自动设置切片宽高
    elif auto_slice_resolution:
        x_overlap, y_overlap, slice_width, slice_height = get_auto_slice_params(height=image_height, width=image_width)
    else:
        raise ValueError("Compute type is not auto and slice width and height are not provided.")

    while y_max < image_height:
        x_min = x_max = 0
        y_max = y_min + slice_height
        while x_max < image_width:
            x_max = x_min + slice_width
            #处理越界
            if y_max > image_height or x_max > image_width:
                xmax = min(image_width, x_max)
                ymax = min(image_height, y_max)
                xmin = max(0, xmax - slice_width)
                ymin = max(0, ymax - slice_height)
                slice_bboxes.append([xmin, ymin, xmax, ymax])
            else:#正常
                slice_bboxes.append([x_min, y_min, x_max, y_max])
            x_min = x_max - x_overlap
        y_min = y_max - y_overlap
    return slice_bboxes

2、get_auto_slice_params

def get_auto_slice_params(height: int, width: int) -> Tuple[int, int, int, int]:
    """
    According to Image HxW calculate overlap sliding window and buffer params
    factor is the power value of 2 closest to the image resolution.
        factor <= 18: low resolution image such as 300x300, 640x640
        18 < factor <= 21: medium resolution image such as 1024x1024, 1336x960
        21 < factor <= 24: high resolution image such as 2048x2048, 2048x4096, 4096x4096
        factor > 24: ultra-high resolution image such as 6380x6380, 4096x8192
    Args:
        height:
        width:

    Returns:
        slicing overlap params x_overlap, y_overlap, slice_width, slice_height
    """
    resolution = height * width #图幅面积
    factor = calc_resolution_factor(resolution) #2的次幂
    if factor <= 18:
        return get_resolution_selector("low", height=height, width=width)
    elif 18 <= factor < 21:
        return get_resolution_selector("medium", height=height, width=width)
    elif 21 <= factor < 24:
        return get_resolution_selector("high", height=height, width=width)
    else:
        return get_resolution_selector("ultra-high", height=height, width=width)


def get_resolution_selector(res: str, height: int, width: int) -> Tuple[int, int, int, int]:
    """

    Args:
        res: resolution of image such as low, medium
        height:
        width:

    Returns:
        trigger slicing params function and return overlap params
    """
    orientation = calc_aspect_ratio_orientation(width=width, height=height) #以长的边为主
    x_overlap, y_overlap, slice_width, slice_height = calc_slice_and_overlap_params(
        resolution=res, height=height, width=width, orientation=orientation
    )

    return x_overlap, y_overlap, slice_width, slice_height

def calc_slice_and_overlap_params(
    resolution: str, height: int, width: int, orientation: str
) -> Tuple[int, int, int, int]:
    """
    This function calculate according to image resolution slice and overlap params.
    Args:
        resolution: str
        height: int
        width: int
        orientation: str

    Returns:
        x_overlap, y_overlap, slice_width, slice_height
    """

    if resolution == "medium":
        split_row, split_col, overlap_height_ratio, overlap_width_ratio = calc_ratio_and_slice(
            orientation, slide=1, ratio=0.8
        )

    elif resolution == "high":
        split_row, split_col, overlap_height_ratio, overlap_width_ratio = calc_ratio_and_slice(
            orientation, slide=2, ratio=0.4
        )

    elif resolution == "ultra-high":
        split_row, split_col, overlap_height_ratio, overlap_width_ratio = calc_ratio_and_slice(
            orientation, slide=4, ratio=0.4
        )
    else:  # low condition
        split_col = 1
        split_row = 1
        overlap_width_ratio = 1
        overlap_height_ratio = 1

    # split_row为沿着宽方向切片量,split_col为沿着高方向切片量
    slice_height = height // split_col
    slice_width = width // split_row

    x_overlap = int(slice_width * overlap_width_ratio)
    y_overlap = int(slice_height * overlap_height_ratio)

    return x_overlap, y_overlap, slice_width, slice_height


def calc_ratio_and_slice(orientation, slide=1, ratio=0.1):
    """
    According to image resolution calculation overlap params
    Args:
        orientation: image capture angle
        slide: sliding window
        ratio: buffer value

    Returns:
        overlap params
    """
    #slice_row为沿着宽方向切片量,slice_col为沿着高方向切片量
    if orientation == "vertical": #高长,沿着高方向切片量多些
        slice_row, slice_col, overlap_height_ratio, overlap_width_ratio = slide, slide * 2, ratio, ratio
    elif orientation == "horizontal":#宽长,沿着宽方向切片量多些
        slice_row, slice_col, overlap_height_ratio, overlap_width_ratio = slide * 2, slide, ratio, ratio
    elif orientation == "square":#宽高相等
        slice_row, slice_col, overlap_height_ratio, overlap_width_ratio = slide, slide, ratio, ratio

    return slice_row, slice_col, overlap_height_ratio, overlap_width_ratio  # noqa

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2195840.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

LeetCode 3311. 构造符合图结构的二维矩阵

LeetCode 3311. 构造符合图结构的二维矩阵 给你一个二维整数数组 edges &#xff0c;它表示一棵 n 个节点的 无向 图&#xff0c;其中 edges[i] [ui, vi] 表示节点 ui 和 vi 之间有一条边。 请你构造一个二维矩阵&#xff0c;满足以下条件&#xff1a; 矩阵中每个格子 一一对应…

力扣之1336.每次访问的交易次数

题目&#xff1a; sql建表语句&#xff1a; Create table If Not Exists Visits (user_id int, visit_date date); Create table If Not Exists Transactions (user_id int, transaction_date date, amount int); Truncate table Visits; insert into Visits (user_id,…

如何让客户主动成为你的品牌大使

在销售领域&#xff0c;转介绍被公认为一把无坚不摧的利器&#xff0c;它不仅铸就了高成交率的辉煌&#xff0c;更以惊人的速度缩短了成交周期。一位精通转介绍艺术的销售员&#xff0c;其业绩自然熠熠生辉&#xff0c;工作之路亦显得游刃有余。 然而&#xff0c;面对这一宝藏…

Bianchi模型、python计算及ns3验证_关于2~10 STA验证的补充

首先就是预设修改, NS3中bitrate是OfdmRate54Mbps,STA数目我设置了2-10,ack长度是14bytes,数据长36,头36(trace中只有1536和14两个长度,也就是数据长度1500,头36,ack14),SIFS和SLOT是16us和9us(在phy的定义中,11a的时候,sifs是16,slot是9),difs是34us(在bia…

2024Java最新面试题总结(针对于一些小厂、中厂)

这是根据个人面试经历总结出来的一些经验希望可以帮助到有需要的人。 面试的时候&#xff0c;会先让你进行自我介绍&#xff0c;这个大家准备一两分钟的面试稿就可以。然后就是正式面试&#xff0c;面试官一般是两个人以上&#xff0c;开始&#xff0c;面试官会先提问一些基本…

【NLP自然语言处理】01-基础学习路径简介

目的&#xff1a;让大家能够在 AI-NLP 领域由基础到入门具体安排&#xff1a; NLP介绍 文本预处理RNN 及其变体&#xff08;涉及案例&#xff09;Transformer 原理详解迁移学习 和 Bert 模型详解 &#xff08;涉及案例&#xff09;特点&#xff1a; 原理 实践每个文章会有练习…

Nat. Commun.:飞秒激光书写受蚂蚁启发的可重构微型机器人集体

背景介绍生物在各种环境中的集体行为十分普遍&#xff0c;它们能够自发有序地完成单个个体难以完成的任务。目前&#xff0c;生物集体的形成主要分为两大类。第一类生物个体之间没有直接接触&#xff0c;如蜜蜂、鱼和鸟类&#xff0c;这导致这些集体不稳定&#xff0c;容易受到…

Ubuntu24 Firefox和Window Firefox同步问题

由于平常开发在Ubuntu系统&#xff0c;但是日常学习查资料又在Window系统&#xff0c;查资料保存网页到浏览器中时&#xff0c;经常遇到两个平台标签同步问题&#xff0c;希望可以选一个支持Ubuntu、Window的浏览器。 选用Google Chrome浏览器&#xff0c;确实挺好用&#xff…

微服务seata解析部署使用全流程

官网地址&#xff1a; Seata 是什么&#xff1f; | Apache Seata 1、Seata术语 用来管理分布式事务&#xff0c;由阿里巴巴出品。 【1、TC (Transaction Coordinator) - 事务协调者】 用来维护事务的&#xff0c;包括主事务和分支事务。 【2、TM (Transaction Manager) - …

TCP与UDP协议(三次握手四次挥手)

TCP与UDP 简介TCP和UDP一、TCP1.1 TCP的三次握手问题来了&#xff1a;为啥是三次握手而不是两次呢&#xff1f; 1.2建立连接后的通信过程&#xff08;丢包与乱序问题&#xff09;1.3四次挥手问题来了&#xff1a;为什么要四次挥手&#xff1f; 二、UDP 简介TCP和UDP TCP、UDP都…

机器学习笔记(持续更新)

重复值处理&#xff1a; 重复值处理代码&#xff1a; import pandas as pd data pd.DataFrame({学号: [1, 2, 3, 4, 5, 6, 7, 7, 8],身高: [172,162,175,170,168,160,164,164,160],体重: [70,62,75,68,67,58,64,64,53] }) data.drop_duplicates([学号], keep last, inplaceT…

SQL第13课挑战题

1. 使用inner join&#xff0c;以检索每个顾客的名称&#xff08;customers表中的cust_name&#xff09;和所有的订单号&#xff08;orders表中的order_num). 2. 修改第一题&#xff0c;仅列出所有顾客&#xff0c;及时他们没有下过订单。 3. 使用outer join联结products表和or…

【Easy RL】Easy RL蘑菇书全书学习笔记

【Easy RL】Easy RL蘑菇书全书学习笔记 第一章 强化学习基础1.1 强化学习概述监督学习强化学习与监督学习的不同之处二者的区别总结强化学习的特征强化学习的优越性预演&#xff08;rollout&#xff09;和 轨迹&#xff08;trajectory&#xff09;的概念端到端的概念深度强化学…

BurpSuite内置浏览器打不开(实用解决法/简便)

也不知道出现了什么问题就是莫名其妙的内置浏览器打不开&#xff0c;有时候不想配置代理很麻烦&#xff0c;这里实用的解决办法. 任务一 报错情况&#xff08;反正也看不懂&#xff09; 任务二 我们先去尝试打开这个运行模式&#xff0c;然后我们再去重试&#xff0c;如果还是打…

【重学 MySQL】五十九、二进制字符串类型与 JSON 类型

【重学 MySQL】五十九、二进制字符串类型与 JSON 类型 二进制字符串类型JSON类型 在MySQL数据库中&#xff0c;二进制字符串类型与JSON类型各自具有独特的特点和用途。 二进制字符串类型 二进制字符串类型在MySQL中主要用于存储二进制数据。这类数据类型包括BINARY、VARBINAR…

使用 ChatGPT Canvas 辅助 ABAP 开发

ChatGPT Canvas 是最近 OpenAI 推出的一个新功能,它不仅仅是一个简单的聊天对话式窗口。 Canvas 旨在扩展 ChatGPT 平台的功能,超越简单的问答交互。 Canvas 可以在单独的窗口中打开专用工作区,用户能够更直观、更高效地与 ChatGPT 在复杂的写作或者编码项目进行协作。 有…

Nginx08-反向代理

零、文章目录 Nginx08-反向代理 1、概述 关于正向代理和反向代理&#xff0c;我们在前面已经介绍过了&#xff0c;简而言之就是正向代理代理的对象是客户端&#xff0c;反向代理代理的是服务端&#xff0c;这是两者之间最大的区别。 Nginx即可以实现正向代理&#xff0c;也可…

【Unity】版本不一致且未升级资产,导致 Unity Sprite 2D 动画播放错误

自己的 Unity版本是 2022.3.45f1。目前折腾的这插件 2D Action RPG Engine: Mythril2D &#xff0c;推荐使用的 Unity 版本是 2021.3.18。 倒腾了这个 unity animation 动画半天&#xff0c;发现这个 animation sprite resolver 在导入动画帧的时候&#xff0c;一直都导入的是…

LeetCode 11 Container with Most Water 解题思路和python代码

题目&#xff1a; You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]). Find two lines that together with the x-axis form a container, such that the co…

【论文阅读】AUTOREGRESSIVE ACTION SEQUENCE LEARNING FOR ROBOTIC MANIPULATION

ABSTRACT 自回归模型在自然语言处理中取得了显著的成功。在这项工作中&#xff0c;我们为机器人操纵任务设计了一个简单而有效的自回归架构。我们提出了Chunking Causal Transformer&#xff08;cct&#xff09;&#xff0c;它扩展了因果关系transformers的下一个单标记预测 n…