
本文共 3873 字,大约阅读时间需要 12 分钟。
I图像处理框架
一、探索函数
1.1 neighbors函数
1.2 结果早知
def neighbors(shape):
以下讲解以二维为例,假设dhspe=3,5
dim = len(shape)
获取维度,2
block = np.ones([3]*dim)
制作3x3的1数组
block[tuple([1]*dim)] = 0
[1,1]置0,也就是方块中心
idx = np.where(block>0)
获取非0像素索引,也就是除了中心
idx = np.array(idx, dtype=np.uint8).T
矩阵化
idx = np.array(idx-[1]*dim)
减1,中心化,也就空缺的[1,1]变[0,0]
idx此时是[[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]
acc = np.cumprod((1,)+shape[::-1][:-1])
(3,5)变(1,5),累乘,变(1,5)
return np.dot(idx, acc[::-1])
与(5,1)点积,变[-6,-5,-4,-1,1,4,5,6]
得到所有邻居像素的一维索引差。这样用原数组的reval形式,可以实现邻居遍历,兼容任意维度。
二、探索历程
2.1函数定义
matlab图像的邻域操作与块操作
2.2 shape
例如,一维数组a = [1,2,3,4,5,8],取得一个元素用a[i],只有一层
下面是一个二维数组,ndim为2,shape属性值为(3,3)
下面是一个三维数组,ndim为3,shape属性值为(2,2,3)
c =
[
[
[1,2,3],
[4,5,6]
],
[
[7,8,9],
[10,11,12]
]
]
取得一个确切的元素,
用b[i][j][k]的格式,数组嵌套了3层,第一层有2个元素,第二层也是2个元素,第三层有三个元素
作者:张伊
链接:
2.3 [1]*dim与ones()
['Hi!'] * 4 | ['Hi!', 'Hi!', 'Hi!', 'Hi!'] | 重复 |
>>>tuple([1,2,3,4])
(1, 2, 3, 4)
>>> np.ones(5)array([ 1., 1., 1., 1., 1.])
>>>
>>> np.ones((5,), dtype=int)array([1, 1, 1, 1, 1])
>>>
>>> np.ones((2, 1))array([[ 1.], [ 1.]])
>>>
>>> s = (2,2)>>> np.ones(s)array([[ 1., 1.], [ 1., 1.]])
2.4 np.where()
其中condition、y和z都是数组,它的返回值是一个形状与condition相同的数组。当condition中的某个元素为True时,x中对应下标的值从数组y获取,否则从数组z获取:
作者:品颜完月
链接:
2.5、np.cumprod(a)
Return the cumulative product of elements along a given axis.numpy.cumprod(a, axis=None, dtype=None, out=None)
Examples
>>>
>>> a = np.array([1,2,3])>>> np.cumprod(a) # intermediate results 1, 1*2... # total product 1*2*3 = 6array([1, 2, 6])>>> a = np.array([[1, 2, 3], [4, 5, 6]])>>> np.cumprod(a, dtype=float) # specify type of outputarray([ 1., 2., 6., 24., 120., 720.])
The cumulative product for each column (i.e., over the rows) of a:
>>>
>>> np.cumprod(a, axis=0)array([[ 1, 2, 3], [ 4, 10, 18]])
The cumulative product for each row (i.e. over the columns) of a:
>>>
>>> np.cumprod(a,axis=1)array([[ 1, 2, 6], [ 4, 20, 120]])
2.6 a[::-1]
when you do a[::-1] , it starts from the end, towards the first, taking each element. So it reverses a. This is applicable for lists/tuples as well.
Example -
>>> a = '1232'>>> a[::-1]'2321'
2.7 a[:-1]
In [12]: l = list(range(10))
In [13]: l
Out[13]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [16]: l[:-1]
Out[16]: [0, 1, 2, 3, 4, 5, 6, 7, 8]
作者:张凯申
链接:
2.8 np.dot()
机器学习有很多种矩阵乘法,比如点乘叉乘,Hadamard积,克罗内克积,在Python中要怎么用?
from numpy import dot,cross,kron # cross ref:https://docs.scipy.org/doc/numpy/reference/generated/numpy.cross.html#numpy.cross # dot,kron ref:https://docs.scipy.org/doc/numpy/reference/routines.linalg.html from scipy.linalg import hadamard # hadamard ref:https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.hadamard.html#scipy.linalg.hadamard
作者:武锐
链接:https://www.zhihu.com/question/267008582/answer/316890249
三、不懂之处
3.1 block[tuple([1]*dim)] = 0
这个是对角线设置为0?
3.2 idx = np.array(idx - [1]*dim)
这个是几维数组?
四、Imagepy作者闫大说与群友说
4.1 函数目的
这个函数是获得邻居像素的索引差。
在查找极值和分水岭中有用到,目的是将任意维度转一维处理。
4.2 详细讲解
def neighbors(shape):
以下讲解以二维为例,假设dhspe=3,5
dim = len(shape)
获取维度,2
block = np.ones([3]*dim)
制作3x3的1数组
block[tuple([1]*dim)] = 0
[1,1]置0,也就是方块中心
idx = np.where(block>0)
获取非0像素索引,也就是除了中心
idx = np.array(idx, dtype=np.uint8).T
矩阵化
idx = np.array(idx-[1]*dim)
减1,中心化,也就空缺的[1,1]变[0,0]
idx此时是[[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]
acc = np.cumprod((1,)+shape[::-1][:-1])
(3,5)变(1,5),累乘,变(1,5)
return np.dot(idx, acc[::-1])
与(5,1)点积,变[-6,-5,-4,-1,1,4,5,6]
得到所有邻居像素的一维索引差。这样用原数组的ravel形式,可以实现邻居遍历,兼容任意维度。
shape就是个标记,内存都是ravel。
ravel是最接近物理存储模式的形态
如果用shape的方法遍历其实是非线性的,指针要跳来跳去的。
这个写法主要优势是对高维的兼容。
性能没差别,是针对c语言。
分水岭,局部极值,骨架拓扑重建都会用到那个函数。
就是有关高维度邻居遍历的
三维就是做一个高维度3x3立方体,中间置0,然后利用np.where拿到高维度的邻居索引,与目标shape的累乘卷积。
比如w*h的图,相邻列索引差1
而相邻行就差w
如果是w*h*z,你算一下三个维度各差多少
最末端肯定是差1
而上一个维度就差一行
再上一个,就差一个面
你看是不是累乘嘛
同理可以推广到更高维度
然后和小方块的多维坐标点乘
就是±1那些
三维意义是上下左右,点积刚才那个累乘结果,不就是邻居坐标差吗
第一个维度,第二个维度。。。分别前移或后移。。。
甚至没必要弄得很懂,反正np.where一个不漏
中间是自己,所以设0
但是起算点是0,0,所以减1
numpy.ravel
发表评论
最新留言
关于作者
