当前位置:网站首页>skimage学习(1)
skimage学习(1)
2022-07-07 15:32:00 【原知】
1、生成结构化元素
这个例子展示了如何使用skimage中的函数。生成结构元素的形态学。每个图的标题表示函数的调用。
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from skimage.morphology import (square, rectangle, diamond, disk, cube,
octahedron, ball, octagon, star)
#正方形、长方形、菱形、圆盘、立方体、八面体、球体、八角形、星形
# Generate 2D and 3D structuring elements.
struc_2d = {
"square(15)": square(15),
"rectangle(15, 10)": rectangle(15, 10),
"diamond(7)": diamond(7),
"disk(7)": disk(7),
"octagon(7, 4)": octagon(7, 4),
"star(5)": star(5)
}
struc_3d = {
"cube(11)": cube(11),
"octahedron(5)": octahedron(5),
"ball(5)": ball(35),
"ball(5)": ball(35),
}
# Visualize the elements.
fig = plt.figure(figsize=(8, 8))
idx = 1
#tems()函数以列表返回可遍历的(键值)元组数组。
''' plt.text(x, y, string, weight="bold", color="b") x: 注释文本内容所在位置的横坐标 y:注释文本内容所在位置的纵坐标 string:注释文本内容,struc[i, j]为数0、1 weight:注释文本内容的粗细风格 '''
for title, struc in struc_2d.items():
ax = fig.add_subplot(4, 4, idx)#3行3列,位置为
ax.imshow(struc, cmap="Greens", vmin=0, vmax=12)#ax参数用于限定数值的范围,只将vmin和vmax之间的值进行映射,用法如下
for i in range(struc.shape[0]):
for j in range(struc.shape[1]):
ax.text(j, i, struc[i, j], ha="center", va="center", color="w")
ax.set_axis_off()
ax.set_title(title)
idx += 1
for title, struc in struc_3d.items():
ax = fig.add_subplot(4, 4, idx, projection=Axes3D.name)
ax.voxels(struc)
ax.set_title(title)
idx += 1
fig.tight_layout()
plt.show()
2、图像/阵列上的块视图
这个例子演示了skimage.util()中的view_as_blocks的使用。当一个人想对非重叠图像块执行局部操作时,块视图是非常有用的。我们用skimage中的astronaut。数据,并将其“切分”为方方面面。然后,在每个块上,我们要么汇集该块的平均值,最大值或中值。结果显示在一起,连同一个三阶的样条插值的原始宇航员图像缩放。
import numpy as np
from scipy import ndimage as ndi
from matplotlib import pyplot as plt
import matplotlib.cm as cm
from skimage import data
from skimage import color
from skimage.util import view_as_blocks
# get astronaut from skimage.data in grayscale
l = color.rgb2gray(data.astronaut())
#img=skimage.io.imread('11.jpg',)
#l = color.rgb2gray(img)
# size of blocks
block_shape = (4,4)
#将宇航员图片分为矩阵块(大小为block_shape)
view = view_as_blocks(l, block_shape)
# 最后两个维度合二为一,变为数组方便操作
#img.shape[0]:图像的垂直尺寸(高度) img.shape[1]:图像的水平尺寸(宽度)
flatten_view = view.reshape(view.shape[0], view.shape[1], -1)
# 通过取每个块的“均值”、“最大值”或“中值”重新采样图像。mean()函数功能:求取均值
mean_view = np.mean(flatten_view, axis=2)
max_view = np.max(flatten_view, axis=2)
median_view = np.median(flatten_view, axis=2)
# 画子图,sharex和sharey:表⽰坐标轴的属性是否相同
fig, axes = plt.subplots(2, 2, figsize=(8, 8), sharex=True, sharey=True)
ax = axes.ravel()#将多维数据展平为⼀维数据,它相当于 reshape(-1, order=order) 。
''' https://vimsky.com/examples/usage/python-scipy.ndimage.zoom.html https://www.jianshu.com/p/909851f46411 ndi.zoom(input,zoom,output=None,order,mode='constant',cval=0.0,prefilter=True)缩放数组。使用请求顺序的样条插值对数组进行缩放。 input: 以数组形式输入图片 zoom:浮点数或数组。如果是一个浮点数,对每一个轴放缩相同的倍数。如果是一个数组,则对每一个轴分配一个值。 output:输出,默认为None order:整型(范围0-5)样条插值的顺序,默认为3。详见后续 '''
l_resized = ndi.zoom(l, 2, order=3)
ax[0].set_title("Original rescaled with\n spline interpolation (order=3)")
ax[0].imshow(l_resized, extent=(-0.5, 128.5, 128.5, -0.5),
cmap=cm.Greys_r)
ax[1].set_title("Block view with\n local mean pooling")
ax[1].imshow(mean_view, cmap=cm.Greys_r)
ax[2].set_title("Block view with\n local max pooling")
ax[2].imshow(max_view, cmap=cm.Greys_r)
ax[3].set_title("Block view with\n local median pooling")
ax[3].imshow(median_view, cmap=cm.Greys_r)
for a in ax:
a.set_axis_off()
fig.tight_layout()
plt.show()
3、使用简单的 NumPy 操作来处理图像
此脚本说明了如何使用基本的 NumPy 操作,例如切片、屏蔽和花式索引,以修改图像的像素值。
#使用基本的 NumPy 操作,例如切片、屏蔽和花式索引,以修改图像的像素值。
import numpy as np
from skimage import data
import matplotlib.pyplot as plt
#读入,camera是ndarray的数组
camera = data.camera()
camera[:10] = 0#第0-9位设为0
mask = camera < 87#‘< ’为条件语句,只能返回“真假”。将camera中像素值<87的位置记为true,其余为false
camera[mask] = 255#再利用mask所标记的位置(true和false),将值为true的值设为255
inds_x = np.arange(len(camera))#横坐标0-511,arange(a,b,c)函数产生a~b(不包括b),间隔为c的一个数组,参511为终点,起点取0,步长取默认值1。
inds_y = (4 * inds_x) % len(camera)#生成步长为4的数组,纵坐标
camera[inds_x, inds_y] = 0 #按inds_x, inds_y的值将像素置为零
l_x, l_y = camera.shape[0], camera.shape[1]#读取矩阵长度
print(l_x,l_y)
X, Y = np.ogrid[:l_x, :l_y]#产生两个长为512的二维数组
print(X,Y)
outer_disk_mask = (X - l_x / 2)**2 + (Y - l_y / 2)**2 > (l_x / 2)**2#生成圆形的网格坐标
camera[outer_disk_mask] = 0 # 对网格坐标赋,除圆以外的都变为黑色的
plt.figure(figsize=(4, 4)) # 建立figure的尺寸比例
plt.imshow(camera, cmap='gray') # 显示图像
plt.axis('off')
plt.show()
边栏推荐
- [summary of knowledge] summary of notes on using SVN in PHP
- LeetCode 213. 打家劫舍 II 每日一题
- 01tire+链式前向星+dfs+贪心练习题.1
- HAVE FUN | “飞船计划”活动最新进展
- Introduction to ThinkPHP URL routing
- Lowcode: four ways to help transportation companies enhance supply chain management
- Personal notes of graphics (1)
- LeetCode 1626. 无矛盾的最佳球队 每日一题
- 【PHP】PHP接口继承及接口多继承原理与实现方法
- Three. JS series (2): API structure diagram-2
猜你喜欢
Master this set of refined Android advanced interview questions analysis, oppoandroid interview questions
The difference and working principle between compiler and interpreter
Module VI
运算符
如何快速检查钢网开口面积比是否符合 IPC7525
【C 语言】 题集 of Ⅹ
C语言进阶——函数指针
Master this promotion path and share interview materials
最新高频Android面试题目分享,带你一起探究Android事件分发机制
最新Android面试合集,android视频提取音频
随机推荐
spark调优(三):持久化减少二次查询
Prometheus API deletes all data of a specified job
Pisa-Proxy SQL 解析之 Lex & Yacc
DAPP defi NFT LP single and dual currency liquidity mining system development details and source code
字节跳动Android面试,知识点总结+面试题解析
os、sys、random标准库主要功能
How can laravel get the public path
浅浅理解.net core的路由
最新高频Android面试题目分享,带你一起探究Android事件分发机制
Opencv personal notes
SqlServer2014+: 创建表的同时创建索引
Pycharm IDE下载
【医学分割】attention-unet
QT中自定义控件的创建到封装到工具栏过程(二):自定义控件封装到工具栏
LeetCode 1043. 分隔数组以得到最大和 每日一题
Have fun | latest progress of "spacecraft program" activities
ATM系统
Laravel post shows an exception when submitting data
两类更新丢失及解决办法
Personal notes of graphics (3)