【code基础】skimage.transform.resize的一些理解
skimage.transform.resize(order, preserve_range)
order: 插值的方法0-5:0-最近邻;1-双线性;
skimage在读使用io.imread读取灰度图像时(as_grey=True / as_gray=True)会做归一化处理数据类型转化为float64;
图像缩放transform.resize同样会将uint8的图像转化为float64类型,这里注意的是!!!!!!!如果已经归一化,但是类型依然是uint8的图像,在缩放之后图像的范围将不再是(0-1)。
主要在resize方面,cv2.resize就是单纯调整尺寸,而skimage.transform.resize会顺便把图片的像素归一化缩放到(0,1)区间内;
图像缩放transform.resize同样会将uint8的图像转化为float64类型,这里注意的是!!!!!!!如果已经归一化,但是类型依然是uint8的图像,在缩放之后图像的范围将不再是(0-1)。
主要在resize方面,cv2.resize就是单纯调整尺寸,而skimage.transform.resize会顺便把图片的像素归一化缩放到(0,1)区间内;
preserve_range : bool, optional
Whether to keep the original range of values. Otherwise, the input
image is converted according to the conventions of `img_as_float`.
通过在代码里设置preserve_range=True就可以保持原来的模式了。发现使用skimage.transform.resize之后,语义分割标签图像的数值发生了改变,数据类型也发生了改变,最关键的是数值发生也发生了改变;仔细查阅官方文档,添加anti_aliasing=False选项即可,因为默认是进行高斯滤波的;

from skimage import io, transform, color import numpy as np print('skimage: ', skimage.__version__) a=np.zeros((20, 20)) a[2:8, 1:3]=1 a[1:3, 4:9]=2 a[3:9, 6:8]=2 print(a) print(a.shape) print(a.dtype) print(np.unique(a)) b = skimage.transform.resize(a,(10, 10),mode='constant', order=0, anti_aliasing=False, preserve_range=True) print(b) print(b.shape) print(b.dtype) print(np.unique(b)) label = skimage.io.imread('sample800/label/0705_1.png') print(label.shape) #(800, 800) print(label.dtype) print(np.unique(label)) lbl = skimage.transform.resize(label,(512, 512),mode='edge', order=0, anti_aliasing=False, preserve_range=True) print(lbl.shape) print(lbl.dtype) print(np.unique(lbl)) label_show = np.zeros((512, 512, 3), dtype=np.uint8) COLORS = [(0, 0, 0), (0, 255, 0), (0, 0, 255), (238, 18, 137), (162, 205, 90), (70, 130, 180), (238, 238, 0), (255, 69, 0), (205, 145, 158), (238, 92, 66), (144, 238, 144), (124, 205, 124), (0, 229, 238), (151, 255, 255), (205, 190, 112)] for i in range(1, 9): label_show[lbl==i]=COLORS[i] import cv2 cv2.imshow("label_img", label_show) cv2.waitKey(1)
description:
anti_aliasingbool, optional Whether to apply a Gaussian filter to smooth the image prior to downsampling. It is crucial to filter when downsampling the image to avoid aliasing artifacts. If not specified, it is set to True when downsampling an image whose data type is not bool.
不知道为什么 preserve_range 不起作用?????
参考
完
各美其美,美美与共,不和他人作比较,不对他人有期待,不批判他人,不钻牛角尖。
心正意诚,做自己该做的事情,做自己喜欢做的事情,安静做一枚有思想的技术媛。
版权声明,转载请注明出处:https://www.cnblogs.com/happyamyhope/
心正意诚,做自己该做的事情,做自己喜欢做的事情,安静做一枚有思想的技术媛。
版权声明,转载请注明出处:https://www.cnblogs.com/happyamyhope/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】