我如何保留包含所有要素的补丁1 [英] How can I keep the patch which contain all the elements 1

查看:69
本文介绍了我如何保留包含所有要素的补丁1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

from sklearn.feature_extraction.image import extract_patches
import numpy as np

data = np.array([[1, 1 , 0 , 0 , 0 , 0 , 1 , 0],
                 [1, 1 , 1 , 0 , 0 , 1 , 1 , 0],
                 [1, 1 , 0 , 1 , 1 , 0 , 0 , 0],
                 [0, 0 , 0 , 1 , 1 , 0 , 0 , 0],
                 [0, 0 , 0 , 1 , 1 , 0 , 0 , 1],
                 [1, 1 , 0 , 0 , 0 , 0 , 1 , 0],
                 [1, 1 , 0 , 0 , 0 , 0 , 0 , 0]])

patches = extract_patches(data, patch_shape=(2, 2))

如何保留包含所有元素1的补丁?

How can I keep the patch which contain all the elements 1?

推荐答案

从更正到您的文章,我相信您可能正在寻找一种方法来检测形状为(2,2)的子矩阵全都在哪.不能满足该条件的任何地方都应该为零,但是应该优先考虑满足该条件的子矩阵,因为子矩阵可以重叠.

From the corrections to your post, I believe you might be looking for a way to detect where submatrices of shape (2,2) are all ones. Anywhere where that condition isn't fulfilled should be zero, but priority should be given to the submatrices where that condition is fulfilled, because submatrices can be overlapping.

在这种情况下,您很可能会对该矩阵的交错网格感兴趣,该矩阵在每个2x2子矩阵的中央都有一个,只要该子矩阵的4个元素全部为1即可.

In that case, you're most likely interested in the staggered grid of that matrix that has a one in the center of each 2x2 submatrix whenever the 4 elements of that submatrix are all ones:

>>> import numpy as np
>>> from sklearn.feature_extraction.image import extract_patches # similar to numpy's stride_tricks
>>> 
>>> data = np.array([[1, 1, 0, 0, 0, 0, 1, 0],
...                  [1, 1, 1, 0, 0, 1, 1, 0],
...                  [1, 1, 0, 1, 1, 0, 0, 0],
...                  [0, 0, 0, 1, 1, 0, 0, 0],
...                  [0, 0, 0, 1, 1, 0, 0, 1],
...                  [1, 1, 0, 0, 0, 0, 1, 0],
...                  [1, 1, 0, 0, 0, 0, 0, 0]])
>>> 
>>> # to take boundary effects into account, append ones to the right and bottom
... # modify this to `np.zeros` if boundaries are to be set to zero
... data2 = np.ones((data.shape[0]+1, data.shape[1]+1))  
>>> data2[:-1,:-1] = data
>>> vert = np.logical_and(data2[:-1,:], data2[1:,])
>>> dual = np.logical_and(vert[:,:-1], vert[:,1:]) # dual is now the "dual" graph/staggered grid of the data2 array
>>> patches = extract_patches(data2, patch_shape=(2, 2))  # could've used numpy stride_tricks too
>>> patches[dual==0] = 0
>>> patches[dual] = 1  # Give precedence to the dual positives
>>> data2[:-1, :-1].astype(np.uint8)
array([[1, 1, 0, 0, 0, 0, 0, 0],
       [1, 1, 0, 0, 0, 0, 0, 0],
       [1, 1, 0, 1, 1, 0, 0, 0],
       [0, 0, 0, 1, 1, 0, 0, 0],
       [0, 0, 0, 1, 1, 0, 0, 0],
       [1, 1, 0, 0, 0, 0, 0, 0],
       [1, 1, 0, 0, 0, 0, 0, 0]], dtype=uint8)

为完整起见,也可以使用带有np.ones((2,2))内核的 correlation 轻松获得矩阵的交错网格形式.但是,这在计算上更加繁重,因为必须完成更多的工作(乘法和求和),而不是简单的位运算.就速度而言,上述方法将胜过基于相关性的方法. 上面交错的网格dual也可以通过以下方式生成:

For completeness, this staggered grid form of the matrix could also be obtained easily with a correlation with a np.ones((2,2)) kernel. However, that is computationally more heavy, because a lot more work has to be done (multiplications and summations) rather than simple bit-operations. The method above will outperform a correlation-based method in terms of speed. The staggered grid dual above could also be generated in the following way:

patches = extract_patches(data, patch_shape=(2, 2))
dual = patches.all(axis=-1).all(axis=-1)

然后您将获得最终结果:

And you would obtain the final result with:

dual = patches.all(axis=-1).all(axis=-1)
patches[dual==False] = 0
patches[dual] = 1

它与以前的方法的不同之处在于边界处发生的事情.

It differs from the previous method in what happens at the boundaries though.

这篇关于我如何保留包含所有要素的补丁1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆