我已经根据resize_image()函数的解析对原图像与resize图像进行了解析,

若有读者想对原图像与目标图像不同尺寸验证,可根据以下代码,调整函数参数,

其细节如下:

  1. import cv2 as cv
    import numpy as np
    img=cv.imread('D:\\MASKRCNN\\mask-rcnn-me\\MASKRCNN_myself\\0.bmp')
    def resize_image(image, min_dim=230, max_dim=220, min_scale=2, mode="square"):
    """Resizes an image keeping the aspect ratio unchanged.
  2.  
  3. min_dim: if provided, resizes the image such that it's smaller dimension == min_dim
    max_dim: if provided, ensures that the image longest side doesn't exceed this value.
    min_scale: if provided, ensure that the image is scaled up by at least
    this percent even if min_dim doesn't require it.
    mode: Resizing mode.
    none: No resizing. Return the image unchanged.
    square: Resize and pad with zeros to get a square image of size [max_dim, max_dim].
    pad64: Pads width and height with zeros to make them multiples of 64.
    If min_dim or min_scale are provided, it scales the image up
    before padding. max_dim is ignored in this mode.
    The multiple of 64 is needed to ensure smooth scaling of feature
    maps up and down the 6 levels of the FPN pyramid (2**6=64).
    crop: Picks random crops from the image. First, scales the image based
    on min_dim and min_scale, then picks a random crop of
    size min_dim x min_dim. Can be used in training only.
    max_dim is not used in this mode.
  4.  
  5. Returns:
    image: the resized image
    window: (y1, x1, y2, x2). If max_dim is provided, padding might
    be inserted in the returned image. If so, this window is the
    coordinates of the image part of the full image (excluding
    the padding). The x2, y2 pixels are not included.
    scale: The scale factor used to resize the image
    padding: Padding added to the image [(top, bottom), (left, right), (0, 0)]
    """
    # Keep track of image dtype and return results in the same dtype
    image_dtype = image.dtype
    print('original_image_shape=',image.shape)
    # Default window (y1, x1, y2, x2) and default scale == 1.
    h, w = image.shape[:2]
    window = (0, 0, h, w)
    scale = 1
    padding = [(0, 0), (0, 0), (0, 0)]
    crop = None
  6.  
  7. if mode == "none":
    return image, window, scale, padding, crop
  8.  
  9. # Scale?
    if min_dim:
    # Scale up but not down
    scale = max(1, min_dim / min(h, w)) # h, w是原始图片的高与宽
    if min_scale and scale < min_scale: # min_scale是最小填充倍数的,至少要大于它
    scale = min_scale
  10.  
  11. # Does it exceed max dim?
    if max_dim and mode == "square":
    image_max = max(h, w)
    if round(image_max * scale) > max_dim: # 最终原图片最大边扩充不能超过最大max_dim维度,否则重新选择scale
    scale = max_dim / image_max
  12.  
  13. # Resize image using bilinear interpolation
    print('scale=',scale)
    if scale != 1:
    image = cv.resize(image, (round(h * scale), round(w * scale)))
    print('resize_image=',image.shape)
    # 上一行代码对图像做了resize,那么会改变图像的尺寸,这是我不愿意看到的,我觉的这样会对缺陷特征有损失,
    # 或者出现变异,因此小心这里的变化
    # Need padding or cropping?
    if mode == "square":
    # Get new height and width
    h, w = image.shape[:2] # 此时已经将原图按照scale进行了改变
    top_pad = (max_dim - h) // 2
    bottom_pad = max_dim - h - top_pad
    left_pad = (max_dim - w) // 2
    right_pad = max_dim - w - left_pad
    padding = [(top_pad, bottom_pad), (left_pad, right_pad), (0, 0)]
    print('padding=',padding)
    print('before_pad.shape=',image.shape)
    image = np.pad(image, padding, mode='constant', constant_values=0) # 将改变的图片进行了填充
    print('after_pad.shape=', image.shape)
  14.  
  15. window = (top_pad, left_pad, h + top_pad, w + left_pad) # 保存经过resize后图片的真实大小
    print('window=',window)
  16.  
  17. return image.astype(image_dtype), window, scale, padding
  18.  
  19. print('输入图像尺寸小于max_dim的尺寸')
    resize_image(img, min_dim=230, max_dim=280, min_scale=2, mode="square")
  20.  
  21. print('输入图像尺寸大于max_dim的尺寸')
    resize_image(img, min_dim=200, max_dim=220, min_scale=2, mode="square")
  22.  
  23. 结果如下:

  1.  

mask-rcnn代码解读(六):resize_image()函数的解析的更多相关文章

  1. 使用colab运行深度学习gpu应用(Mask R-CNN)实践

    1,目的 Google Colaboratory(https://colab.research.google.com)是谷歌开放的一款研究工具,主要用于机器学习的开发和研究.这款工具现在可以免费使用, ...

  2. [代码解析]Mask R-CNN介绍与实现(转)

    文章来源 DFann 版权声明:如果你觉得写的还可以,可以考虑打赏一下.转载请联系. https://blog.csdn.net/u011974639/article/details/78483779 ...

  3. 论文阅读笔记三十六:Mask R-CNN(CVPR2017)

    论文源址:https://arxiv.org/pdf/1703.06870.pdf 开源代码:https://github.com/matterport/Mask_RCNN 摘要 Mask R-CNN ...

  4. CVPR2019 | Mask Scoring R-CNN 论文解读

    Mask Scoring R-CNN CVPR2019 | Mask Scoring R-CNN 论文解读 作者 | 文永亮 研究方向 | 目标检测.GAN 推荐理由: 本文解读的是一篇发表于CVPR ...

  5. Mask R-CNN用于目标检测和分割代码实现

    Mask R-CNN用于目标检测和分割代码实现 Mask R-CNN for object detection and instance segmentation on Keras and Tenso ...

  6. 目标检测论文解读11——Mask R-CNN

    目的 让Faster R-CNN能做实例分割的任务. 方法 模型的结构图如下. 与Faster R-CNN相比,主要有两点变化. (1) 用RoI Align替代RoI Pool. 首先回顾一下RoI ...

  7. 论文笔记:Mask R-CNN

    之前在一次组会上,师弟诉苦说他用 UNet 处理一个病灶分割的任务,但效果极差,我看了他的数据后发现,那些病灶区域比起整张图而言非常的小,而 UNet 采用的损失函数通常是逐像素的分类损失,如此一来, ...

  8. Mask RCNN 源码阅读(update)

    之前看了Google官网的object_dectect 的源码,感觉Google大神写的还不错.最近想玩下Mask RCNN,就看了下源码,这里刚好当做总结和梳理.链接如下: Google官网的obj ...

  9. Mask RCNN 学习笔记

    下面会介绍基于ResNet50的Mask RCNN网络,其中会涉及到RPN.FPN.ROIAlign以及分类.回归使用的损失函数等 介绍时所采用的MaskRCNN源码(python版本)来源于GitH ...

随机推荐

  1. Mybatis逆向工程中的 mybatis-generator:generate 代码生成器的使用

    使用逆向工程可以根据数据库的表名字生成pojo层(实体类),mapper层(dao层,直接与底层的XML中映射相关),XML(映射执SQL语句) 下面请看具体生成步骤 1. 点击generatorCo ...

  2. 【洛谷P4148】简单题(kd-tree)

    传送门 题意: 给出一个\(n*n\)的棋盘,现在有两种操作:一种是某个格子里的数字加上\(A\),另一种是询问矩阵和. 空间限制:\(20MB\),强制在线. 思路: 直接\(kd-tree\)来搞 ...

  3. 6、zabbix自定义监控

    一.概述 为什么需要自定义监控呢? 虽然zabbix已经给我们准备好了很多的模板,但是有的东西还是无法监控,这时候就要我们自定义监控了. 自定义监控的思路? 比如我们现在想要监控这个值,如下所示,模板 ...

  4. JS阻止冒泡和取消默认事件(默认行为)

    本文链接:http://caibaojian.com/javascript-stoppropagation-preventdefault.html 阻止事件冒泡 function(e){ if( e ...

  5. 元昊讲django框架

    一 什么是web框架? 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单地说,就是你用别人搭建好的舞台来做表演. 对于所有 ...

  6. Python Singleton Pattern(单例模式)

    简介 单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在.当你希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上用场. 当 ...

  7. ESP8266 LUA脚本语言开发: 外设篇-串口

    https://nodemcu.readthedocs.io/en/master/modules/uart/ 串口发送数据 发送一个16进制到串口 uart.write(0, 0xaa) 注: 之所以 ...

  8. ESP8266 LUA脚本语言开发: 外设篇-GPIO输入检测

    咱使用 GPIO0 https://nodemcu.readthedocs.io/en/master/modules/gpio/#gpioread 第一种 GPIO设置为输出的状态下读取引脚状态 gp ...

  9. nuxtjs踩坑指南

    1.nuxt引入问题:Can't resolve 'stylus-loader' 原因在于没有安装stylus,安装即可:npm install stylus stylus-loader --save ...

  10. Unity编辑器扩展学习 转载

    https://www.xuanyusong.com/archives/category/unity/unity3deditor 1 using UnityEngine; public class T ...