从numpy数组中删除连续的数字 [英] Deleting consecutive numbers from a numpy array

查看:84
本文介绍了从numpy数组中删除连续的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,但是对于这个项目我真的很困惑.我在此处中显示了一张图片.我想做的是找到像素范围从0到255的图像中所有正方形的平均值.下面的代码显示了我用来计算图片值的方法.出现的问题是像素/正方形的大小不相同,其中一些大于另一个.例如,在某些情况下,此处平方都相等,因此数组都相等.但是,在这张图片中,我们可以看到右侧正方形小于左侧的.

I'm new to python but I'm really confused on this project I'm trying to do. I've got an image shown here. What I'm trying to do is to find an average across all the squares in the image where the pixel range is from 0 to 255. The code below shows the method I used to calculate the value for the picture. The problem that arises is that the pixels/squares are not the same size where some are bigger than the other. For example, at one instance when looking at a part of the initial picture here, the squares are all equal, therefore the arrays are all equal. However in this picture, we can see that the right hand side squares are smaller than the left hand side ones.

我正在考虑从原始数组创建一个子数组,其中任何重复的值都将被忽略.例如下面的例子:

I was thinking of creating a subarray from the original array where, any repeated values are ignored. For example like the one below:

1 1 1 2 2 1 2
1 1 1 2 2 1 2
1 1 1 2 2 1 2
1 1 1 2 2 1 2
3 3 3 4 4 1 2
3 3 3 4 4 1 2
5 5 5 6 6 1 2
7 7 7 8 8 1 2
7 7 7 8 8 1 2

to 

1 2 1 2
3 4 1 2
5 6 1 2
7 8 1 2

我尝试使用以下内容:

1. unique_rows = np.unique(data, axis=0)
2. unique_columns = np.unique(unique_rows, axis=1)

使所有未重复的行从原始显示在新数组中,然后使用第二代码从新数组中获取所有未重复的列.不幸的是,这样做是摆脱了所有重复的值.我想要的只是摆脱重复的连续值.

to get all the not repeated rows to be displayed into a new array from the original and then get all the non-repeated columns from the new array using the 2nd code. Unfortunately, what this does is get rid of all the repeated values. What I want is only get rid of the repeated consecutive values.

我真的很困惑该怎么办,请帮忙!

I'm really confused on what to do please help!

代码:

from PIL import Image
import numpy as np

img = Image.open('greyscale.png').convert('L')  # convert image to 8-bit grayscale
WIDTH, HEIGHT = img.size

data = list(img.getdata()) # convert image data to a list of integers
# convert that to 2D list (list of lists of integers)
data = np.array ([data[offset:offset+WIDTH] for offset in range(0, WIDTH*HEIGHT, WIDTH)])

#unique_rows = np.unique(data, axis=0)
#unique_columns = np.unique(unique_rows, axis=1)

# At this point the image's pixels are all in memory and can be accessed
# individually using data[row][col].

# For example:
#print data
for row in data:
    print(' '.join('{:3}'.format(value) for value in row))

print np.mean(data)

推荐答案

我检查了此解决方案是否适用于您的整个图像,而不适用于较小的样本,并且确实适用.
我将仅针对该小型数组给出一个示例:

I checked if this solution would work on your whole image rather than on that small sample, and it does work.
I will give an example just for that small array:

import numpy as np

x = np.array([[1, 1, 1, 2, 2, 1, 2],
              [1, 1, 1, 2, 2, 1, 2],
              [1, 1, 1, 2, 2, 1, 2],
              [1, 1, 1, 2, 2, 1, 2],
              [3, 3, 3, 4, 4, 1, 2],
              [3, 3, 3, 4, 4, 1, 2],
              [5, 5, 5, 6, 6, 1, 2],
              [7, 7, 7, 8, 8, 1, 2],
              [7, 7, 7, 8, 8, 1, 2]])

rows_mask = np.insert(np.diff(x[:, 0]).astype(np.bool), 0, True)
columns_mask = np.insert(np.diff(x[0]).astype(np.bool), 0, True)

print(x[np.ix_(rows_mask, columns_mask)])

给予:

[[1 2 1 2]
 [3 4 1 2]
 [5 6 1 2]
 [7 8 1 2]]

从这里您可以计算平均值.

From here you can calculate the average.

关于获取 rows_mask columns_mask 的信息,您可以在这里阅读: np.ix _ 仅返回对角线元素:

About getting rows_mask and columns_mask you can read here: Remove following duplicates in a numpy array .
Also, note that without np.ix_ only diagonal elements would be returned: Boolean array indexing

这篇关于从numpy数组中删除连续的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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