简体   繁体   English

在多维数组中查找模式

[英]Finding patterns in multi dimensional arrays

I've got a 4 dimensional array in which I need to find some patters. 我有一个4维数组,需要在其中找到一些模式。 The patter is to be evaluated in the last two dimensions, scanning across the first two. 模式将在最后两个维度中进行评估,并在前两个维度中进行扫描。 It is somewhat like using a hit or miss function over an image, but instead of evaluating across the two dimensions of the image for patters on the values of these points, I need scan across the two dimensions looking for a pattern in the two other dimensions of the point and its surroundings. 这有点像在图像上使用命中或遗漏功能,但是与其在图像的两个维度上评估这些点的值,不如在图像的两个维度上进行评估,我需要在两个维度上进行扫描以寻找另外两个维度上的图案点及其周围环境。 My current code looks like this, but considering it will look for patterns other than the two ones described bellow, as well as run in real time, I need to make in run much faster. 我当前的代码看起来像这样,但是考虑到它将查找下面介绍的两种模式之外的其他模式,并且要实时运行,因此我需要使运行速度更快。

image = np.empty((240,320,2,3), np.bool)

# image receive some data

for y in range(1, 239):
    for x in range(1, 319):
        if image[y][x-1][1][1] and image[y][x][1][2]:
            FindPattern1[y][x] = True
        if image[y-1][x][0][1] and image[y][x][0][2]:
            FindPattern2[y][x] = True

I thought about using the hit or miss transform with some multidimensional structure to scan across image[y][x] , but was unable to get it working perfectly. 我曾考虑过使用具有某些多维结构的点击或错过转换来扫描image[y][x] ,但是无法使其完美运行。 Do you guys have some idea for solving this problem? 你们有解决这个问题的想法吗?

Seems like you simply just need to do some fancy indexing and a logical_and . 似乎您只需要做一些花哨的索引操作和logical_and

find_pattern_1 = np.logical_and(image[1:, :-1, 1, 1], image[1:, 1:, 1, 2])
find_pattern_2 = np.logical_and(image[:-1, 1:, 0, 1], image[1:, 1:, 0, 2])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM