从numpy数组中删除连续的RGB值 [英] Deleting consecutive RGB values from a numpy array

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

问题描述

我最初从初始数组创建了一个用于灰度图像的子阵列:



输入数组示例:



a = list(img.getdata()),这是我从图像中得到的输入。

  [(115,45,135),(115,45,135),(115,45,135),(115,45,135) ),(115,45,135),(245,245,35),(245,245,35),(245,245,35),(245,245,35),(245,245,35), (245,445,35),(115,45,135),(115,45,135),(115,45,135),(115,45,135),(115,45,135),(245 ,245,35),(245,245,35),(245,245,35),(245,245,35),(245,245,35),(245,245,35),(115,45) ,135),(115,45,135),(115,45,135),(115,45,135),(115,45,135),(245,245,35),(245,245,35) ),(245,245,35),(245,245,35),(245,245,35),(245,245,35),(115,45,135),(115,45,135), (115,45,135),(115,45,135),(115,45,135),(245,245,35),(245,245,35),(245,245,35),(245 ,245,35),(245,245,35),(245,245,35),(115,45,135),(115,45,135),(115,45,135),(115,45) ,135),(115,45,135),(245,245,35),(245,245,35),(245,245,35),(245,245,35),(245,245,35) ),(245,245,3 5),(55,235,195),(55,235,195),(55,235,195),(55,235,195),(55,235,195),(245,245,95) ,(245,245,95),(245,245,95),(245,245,95),(245,245,95),(245,245,95),(245,235,195),(55,235,195),( 55,235,195),(55,235,195),(55,235,195),(55,235,195),(245,245,95),(245,245,95),(245, 245,95),(245,245,95),(245,245,95),(245,245,95),(55,235,195),(55,235,195),(55,235, 195),(55,235,195),(55,235,195),(245,245,95),(245,245,95),(245,245,95),(245,245,95) ,(245,245,95),(245,245,95),(55,235,195),(55,235,195),(55,235,195),(55,235,195),( 55,235,195),(245,245,95),(245,245,95),(245,245,95),(245,245,95),(245,245,95),(245, 245,95),(55,235,195),(55,235,195),(55,235,195),(55,235,195),(55,235,195),(245,245, 95),(245,245,95),(245,245,95),(245,245,95),(245,245,95),(245,245,95),(55,235,195) ,(55,235,195),(55,235,195),(55,235,195),(55,235,195),(245,245,95),(245,245,95),( 245 ,245,95),(245,245,95),(245,245,95),(245,245,95)] 

numpy输入使用 a = np.array([偏移量=偏移量+宽度偏移范围内的偏移量(0,宽度,高度,宽度)])

  [[[115 45 135] 
[115 45 135]
[115 45 135]
[115 45 135]
[115 45 135]
[245 245 35]
[245 245 35]
[245 245 35 ]
[245 245 35]
[245 245 35]
[245 245 35]]

[[115 45 135]
[115 45 135]
[115 45 135]
[115 45 135]
[115 45 135]
[245 245 35]
[245 245 35]
[245 245 35]
[245 245 35]
[245 245 35]
[245 245 35]]

[[115 45 135]
[115 45 135]
[115 45 135]
[115 45 135]
[115 45 135]
[245 245 35]
[245 245] 35]
[245 245 35]
[245 245 35]
[245 245 35]
[245 245 35]]

[[115] 45 135]
[115 45 135]
[ 115 45 135]
[115 45 135]
[115 45 135]
[245 245 35]
[245 245 35]
[245 245 35]
[245 245 35]
[245 245 35]
[245 245 35]]

[[115 45 135]
[115 45 135]
[115 45 135]
[115 45 135]
[115 45 135]
[245 245 35]
[245 245 35]
[ 245 245 35]
[245 245 35]
[245 245 35]
[245 245 35]]

[[55 235 195]
[55 235 195]
[55 235 195]
[55 235 195]
[55 235 195]
[245 245 95]
[245 245 95]
[245 245 95]
[245 245 95]
[245 245 95]
[245 245 95]]

[[55 235 195] ]
[55 235 195]
[55 235 195]
[55 235 195]
[55 235 195]
[245 245 95]
[245 245 95]
[245 245 95]
[245 245 95]
[245 245 95]
[245 245 95]]

[[55 235 195]
[55 235 195]
[55 235 195]
[55 235 195]
[55 23] [5 195]
[245 245 95]
[245 245 95]
[245 245 95]
[245 245 95]
[245 245 95]
[245 245 95]]

[[55 235 195]
[55 235 195]
[55 235 195]
[55 235 195]
[55 235 195]
[245 245 95]
[245 245 95]
[245 245 95]
[245 245 95]
[245 [245 95]
[245 245 95]]

[[55 235 195]
[55 235 195]
[55 235 195]
[ 55 235 195]
[55 235 195]
[245 245 95]
[245 245 95]
[245 245 95]
[245 245 95]
[245 245 95]
[245 245 95]]

[[55 235 195]
[55 235 195]
[55 235 195]
[55 235 195]
[55 235 195]
[245 245 95]
[245 245 95]
[245 245 95]
[ 245 245 95]
[245 245 95]
[245 245 95]]]

所需输出:

  [[[115 45 135] [245 245 35]] 
[55 235 195] [245 245 95]]]


解决方案

这应该这样做:

  columns_mask = np.insert(np.any(np.any(np.diff(a,axis = 0).astype(np.bool),axis = 1),axis = 1),0,True)
rows_mask = np.insert(np.any(np.any(np.diff(a,axis = 1).astype(np.bool),axis = 0),axis = 1),0,True)
print(a [np.ix_(columns_mask,rows_mask)])

输出:

  [[[115 45 135] 
[245 245 35]]

[[55 235 195]
[245 245 95]]]






说明:

让我们采取一个更具代表性的例子:

  a = np.array([[[1,2,3],
[1,2,3],
[2,4,7],
[2,4,7],
[2,4,7],
[[1,2,3],
[1,2,3],
[2,4,7],
[2,4,7],
[2,4,7]],
[[1,2,3],
[1,2,3],
[3,4,7],
[3,4,7],
[ 3,4,7]],
[[1,2,3],
[1,2,3],
[3,4,7],
[ 3,4,7,
[3,4,7],
[[6,4,3],
[6,4,3],
[ 0,1,7],
[0,1,7],
[0,1,7]],
[[6,4,3],
[ 6,4,3],
[0,1,7],
[0,1,7],
[0,1,7]]])

我选择维度为6x5x3以便于跟踪。



For R,G和B我们将有以下子阵列:

 >>>打印(a [:,:0])#R 
[[1 1 2 2 2]
[1 1 2 2 2]
[1 1 3 3 3]
[1 1 3 3 3]
[6 6 0 0 0]
[6 6 0 0 0]]
>>>打印(a [:,:,1])#G
[[2 2 4 4 4]
[2 2 4 4 4]
[2 2 4 4 4]
[2 2 4 4 4]
[4 4 1 1 1]
[4 4 1 1 1]]
>>>打印(a [:,:,2])#B
[[3 3 7 7 7]
[3 3 7 7 7]
[3 3 7 7 7]
[3 3 7 7 7]
[3 3 7 7 7]
[3 3 7 7 7]]

请注意,在此示例中,我们有6个不同颜色的块。但是对于某些组件,我为了示例选择了相同的值。

预期结果将是:

 #R 
[[1 2]
[1 3]
[6 0]]
#G
[[2 4]
[2 4]
[4 1]]
#B
[[3 7]
[3 7]
[3 7]]

首先,我们计算 diff ,以便找到颜色方块之间的边框。

对于列:

 >>> print(np.diff(a,axis = 0))
[[[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0 ]
[0 0 0]]

[[0 0 0]
[0 0 0]
[1 0 0]
[1 0 0]
[1 0 0]]

[[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0]]

[[5 2 0]
[5 2 0]
[-3 -3 0]
[-3 -3 0]
[-3 -3 0]]

[[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0]]]

和行:

 >>> print(np.diff(a,axis = 1))
[[[0 0 0]
[1 2 4]
[0 0 0]
[0 0 0 ]]

[[0 0 0]
[1 2 4]
[0 0 0]
[0 0 0]]

[[0 0 0]
[2 2 4]
[0 0 0]
[0 0 0]]

[[0 0 0]
[2 2 4]
[0 0 0]
[0 0 0]]

[[0 0 0]
[-6 - 3 4]
[0 0 0]
[0 0 0]]

[[0 0 0]
[-6 -3 4]
[0 0 0]
[0 0 0]]]

仔细查看这些数字来自。



接下来,我们使用 .astype(np.bool)将所有非零元素转换为。我们需要这个来创建布尔掩码。有关使用布尔数组进行索引的详细信息,请参阅 NumPy文档

我们得到的栏目:

 >>> print(np.diff(a,axis = 0).astype(np.bool))
[[[False False False]
[False False False]
[False False False]
[假假]
[假假]]

[[假错假]
[假假假]
[真假假]
[真假]
[真假]]

[[假错假]
[假错假]
[假错假] ]
[假错假]
[假错假]]

[[真真假]
[真真假]
[真真假]
[真真假]
[真真假]]

[[假错假]
[假假假]
[假]假错]
[假错假]
[假错假]]]

和行:

 >>> print(np.diff(a,axis = 1).astype(np.bool))
[[[False False False]
[True True True]
[False False False]
[假假假]]

[[假错假]
[真如真]
[假错假]
[假错假] ]

[[错误假]
[真如真]
[假错假]
[假错假]]

[[假假]
[真如真]
[假假]
[假错假]]

[[假假错误]
[True True True]
[假假]
[假错假]]

[[假错假]
[真实真]
[假错假]
[假错假]]]

现在我们应该对第一个数组跨行应用逻辑OR运算,对第二个数组应用跨列。我们需要这样做,以便不会错过具有相同值的连续块,例如,在R颜色的情况下。

 >>> print(np.any(np.diff(a,axis = 0).astype(np.bool),axis = 1))
[[False False False]
[True False False]
[假错假]
[正确错误]
[假错假]]
>>> print(np.any(np.diff(a,axis = 1).astype(np.bool),axis = 0))
[[False False False]
[True True True]
[假错假]
[假错假]]

请参阅以下问题有关 np.any 的详细信息:如何操作2D numpy数组的所有列的逻辑运算



现在我们对颜色执行相同的操作:

  >>> print(np.any(np.any(np.diff(a,axis = 0).astype(np.bool),axis = 1),axis = 1))
[False True False True False]
>>> print(np.any(np.any(np.diff(a,axis = 1).astype(np.bool),axis = 0),axis = 1))
[False True False False]

最后,使用 np.insert 添加 True 在数组的开头考虑第一个元素:

 >>> ; print(np.insert(np.any(np.any(np.diff(a,axis = 0).astype(np.bool),axis = 1),axis = 1),0,True))
[真假真真假真假]
>>> print(np.insert(np.any(np.any(np.diff(a,axis = 1).astype(np.bool),axis = 0),axis = 1),0,True))
[True False True False False]

现在使用 np.ix _ 到得到所需的结果:

  columns_mask = np.insert(np.any(np.any(np.diff(a,axis) = 0).astype(np.bool),axis = 1),axis = 1),0,True)
rows_mask = np.insert(np.any(np.any(np.diff(a,axis) = 1).astype(np.bool),axis = 0),axis = 1),0,True)

>>> print(a [np.ix_(columns_mask,rows_mask)])
[[[1 2 3]
[2 4 7]]

[[1 2 3]
[3 4 7]]

[[6 4 3]
[0 1 7]]]

这就是它!



我们可以检查单独颜色是否正确:

 > ;>> print(a [np.ix_(columns_mask,rows_mask)] [:,:,0])#R 
[[1 2]
[1 3]
[6 0]]
>>> print(a [np.ix_(columns_mask,rows_mask)] [:,:,1])#G
[[2 4]
[2 4]
[4 1]]
>>> print(a [np.ix_(columns_mask,rows_mask)] [:,:,2])#B
[[3 7]
[3 7]
[3 7]]


I initially created a subarray from the initial array for a greyscale image from this: Deleting consecutive numbers from a numpy array and Remove following duplicates in a numpy array

But now I want to do the same for a coloured image and I'm really confused. I've been working on it for days and simply cannot make sense of how I can approach it.

The problem is the squares are different sizes and I want a pixel for each square represented with the same colour.

Coloured image:

Coloured image

My code for greyscale image:

from PIL import Image
import numpy as np

name1 = raw_input("What is the name of the .png file you want to open? ")

filename1 = "%s.png" % name1

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

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

print " "
print "Intial array from image:"  #print as array
print " "
print a

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

print " "
print "Subarray from Image:"  #print as array
print " "
print b

#img = Image.fromarray(b, mode='L')

print " "
print "Subarray from Image (clearer format):"  #print as array
print " "
for row in b: #print as a table like format
    print(' '.join('{:3}'.format(value) for value in row))

#img.save("chocolate.png")


#print np.mean(b) #finding mean

For example for this image:

Input array example:

From a = list(img.getdata()), this is the input I get from the image.

[(115, 45, 135), (115, 45, 135), (115, 45, 135), (115, 45, 135), (115, 45, 135), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (115, 45, 135), (115, 45, 135), (115, 45, 135), (115, 45, 135), (115, 45, 135), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (115, 45, 135), (115, 45, 135), (115, 45, 135), (115, 45, 135), (115, 45, 135), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (115, 45, 135), (115, 45, 135), (115, 45, 135), (115, 45, 135), (115, 45, 135), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (115, 45, 135), (115, 45, 135), (115, 45, 135), (115, 45, 135), (115, 45, 135), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (245, 245, 35), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (55, 235, 195), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95), (245, 245, 95)]

The numpy input using a = np.array ([a[offset:offset+WIDTH] for offset in range(0, WIDTH*HEIGHT, WIDTH)]):

[[[115  45 135]
  [115  45 135]
  [115  45 135]
  [115  45 135]
  [115  45 135]
  [245 245  35]
  [245 245  35]
  [245 245  35]
  [245 245  35]
  [245 245  35]
  [245 245  35]]

 [[115  45 135]
  [115  45 135]
  [115  45 135]
  [115  45 135]
  [115  45 135]
  [245 245  35]
  [245 245  35]
  [245 245  35]
  [245 245  35]
  [245 245  35]
  [245 245  35]]

 [[115  45 135]
  [115  45 135]
  [115  45 135]
  [115  45 135]
  [115  45 135]
  [245 245  35]
  [245 245  35]
  [245 245  35]
  [245 245  35]
  [245 245  35]
  [245 245  35]]

 [[115  45 135]
  [115  45 135]
  [115  45 135]
  [115  45 135]
  [115  45 135]
  [245 245  35]
  [245 245  35]
  [245 245  35]
  [245 245  35]
  [245 245  35]
  [245 245  35]]

 [[115  45 135]
  [115  45 135]
  [115  45 135]
  [115  45 135]
  [115  45 135]
  [245 245  35]
  [245 245  35]
  [245 245  35]
  [245 245  35]
  [245 245  35]
  [245 245  35]]

 [[ 55 235 195]
  [ 55 235 195]
  [ 55 235 195]
  [ 55 235 195]
  [ 55 235 195]
  [245 245  95]
  [245 245  95]
  [245 245  95]
  [245 245  95]
  [245 245  95]
  [245 245  95]]

 [[ 55 235 195]
  [ 55 235 195]
  [ 55 235 195]
  [ 55 235 195]
  [ 55 235 195]
  [245 245  95]
  [245 245  95]
  [245 245  95]
  [245 245  95]
  [245 245  95]
  [245 245  95]]

 [[ 55 235 195]
  [ 55 235 195]
  [ 55 235 195]
  [ 55 235 195]
  [ 55 235 195]
  [245 245  95]
  [245 245  95]
  [245 245  95]
  [245 245  95]
  [245 245  95]
  [245 245  95]]

 [[ 55 235 195]
  [ 55 235 195]
  [ 55 235 195]
  [ 55 235 195]
  [ 55 235 195]
  [245 245  95]
  [245 245  95]
  [245 245  95]
  [245 245  95]
  [245 245  95]
  [245 245  95]]

 [[ 55 235 195]
  [ 55 235 195]
  [ 55 235 195]
  [ 55 235 195]
  [ 55 235 195]
  [245 245  95]
  [245 245  95]
  [245 245  95]
  [245 245  95]
  [245 245  95]
  [245 245  95]]

 [[ 55 235 195]
  [ 55 235 195]
  [ 55 235 195]
  [ 55 235 195]
  [ 55 235 195]
  [245 245  95]
  [245 245  95]
  [245 245  95]
  [245 245  95]
  [245 245  95]
  [245 245  95]]]

Output desired:

[[[115  45 135] [245 245  35]]
[ 55 235 195] [245 245  95]]]

解决方案

This should do it:

columns_mask = np.insert(np.any(np.any(np.diff(a, axis=0).astype(np.bool), axis=1), axis=1), 0, True)
rows_mask = np.insert(np.any(np.any(np.diff(a, axis=1).astype(np.bool), axis=0), axis=1), 0, True)
print(a[np.ix_(columns_mask, rows_mask)])

Output:

[[[115  45 135]
  [245 245  35]]

 [[ 55 235 195]
  [245 245  95]]]


Explanation:
Let's take a more representative example:

a = np.array([[[1, 2, 3],
               [1, 2, 3],
               [2, 4, 7],
               [2, 4, 7],
               [2, 4, 7]], 
              [[1, 2, 3],
               [1, 2, 3],
               [2, 4, 7],
               [2, 4, 7],
               [2, 4, 7]], 
              [[1, 2, 3],
               [1, 2, 3],
               [3, 4, 7],
               [3, 4, 7],
               [3, 4, 7]],
              [[1, 2, 3],
               [1, 2, 3],
               [3, 4, 7],
               [3, 4, 7],
               [3, 4, 7]],
              [[6, 4, 3],
               [6, 4, 3],
               [0, 1, 7],
               [0, 1, 7],
               [0, 1, 7]],
              [[6, 4, 3],
               [6, 4, 3],
               [0, 1, 7],
               [0, 1, 7],
               [0, 1, 7]]])

I chose dimensions to be 6x5x3 for easier tracking.

For R, G, and B we will have the following subarrays:

>>> print(a[:,:,0])  # R
[[1 1 2 2 2]
 [1 1 2 2 2]
 [1 1 3 3 3]
 [1 1 3 3 3]
 [6 6 0 0 0]
 [6 6 0 0 0]]
>>> print(a[:,:,1])  # G
[[2 2 4 4 4]
 [2 2 4 4 4]
 [2 2 4 4 4]
 [2 2 4 4 4]
 [4 4 1 1 1]
 [4 4 1 1 1]]
>>> print(a[:,:,2])  # B
[[3 3 7 7 7]
 [3 3 7 7 7]
 [3 3 7 7 7]
 [3 3 7 7 7]
 [3 3 7 7 7]
 [3 3 7 7 7]]

Note that in this example we have 6 blocks of different colors. But for some components I chose values to be the same for the sake of example.
Expected result would be:

# R
[[1 2]
 [1 3]
 [6 0]]
# G
[[2 4]
 [2 4]
 [4 1]]
# B
[[3 7]
 [3 7]
 [3 7]]

First, we calculate diff in order to locate borders between color squares.
For columns:

>>> print(np.diff(a, axis=0))
[[[ 0  0  0]
  [ 0  0  0]
  [ 0  0  0]
  [ 0  0  0]
  [ 0  0  0]]

 [[ 0  0  0]
  [ 0  0  0]
  [ 1  0  0]
  [ 1  0  0]
  [ 1  0  0]]

 [[ 0  0  0]
  [ 0  0  0]
  [ 0  0  0]
  [ 0  0  0]
  [ 0  0  0]]

 [[ 5  2  0]
  [ 5  2  0]
  [-3 -3  0]
  [-3 -3  0]
  [-3 -3  0]]

 [[ 0  0  0]
  [ 0  0  0]
  [ 0  0  0]
  [ 0  0  0]
  [ 0  0  0]]]

and for rows:

>>> print(np.diff(a, axis=1))
[[[ 0  0  0]
  [ 1  2  4]
  [ 0  0  0]
  [ 0  0  0]]

 [[ 0  0  0]
  [ 1  2  4]
  [ 0  0  0]
  [ 0  0  0]]

 [[ 0  0  0]
  [ 2  2  4]
  [ 0  0  0]
  [ 0  0  0]]

 [[ 0  0  0]
  [ 2  2  4]
  [ 0  0  0]
  [ 0  0  0]]

 [[ 0  0  0]
  [-6 -3  4]
  [ 0  0  0]
  [ 0  0  0]]

 [[ 0  0  0]
  [-6 -3  4]
  [ 0  0  0]
  [ 0  0  0]]]

See carefully where these numbers come from.

Next, we use .astype(np.bool) to convert all non-zero elements to True. We need this to create boolean masks. See NumPy docs for more information on indexing using boolean arrays.
For columns we get:

>>> print(np.diff(a, axis=0).astype(np.bool))
[[[False False False]
  [False False False]
  [False False False]
  [False False False]
  [False False False]]

 [[False False False]
  [False False False]
  [ True False False]
  [ True False False]
  [ True False False]]

 [[False False False]
  [False False False]
  [False False False]
  [False False False]
  [False False False]]

 [[ True  True False]
  [ True  True False]
  [ True  True False]
  [ True  True False]
  [ True  True False]]

 [[False False False]
  [False False False]
  [False False False]
  [False False False]
  [False False False]]]

and for rows:

>>> print(np.diff(a, axis=1).astype(np.bool))
[[[False False False]
  [ True  True  True]
  [False False False]
  [False False False]]

 [[False False False]
  [ True  True  True]
  [False False False]
  [False False False]]

 [[False False False]
  [ True  True  True]
  [False False False]
  [False False False]]

 [[False False False]
  [ True  True  True]
  [False False False]
  [False False False]]

 [[False False False]
  [ True  True  True]
  [False False False]
  [False False False]]

 [[False False False]
  [ True  True  True]
  [False False False]
  [False False False]]]

Now we should apply logical OR operation across rows for the first array, and across columns for the second array. We need to do that in order not to miss consecutive blocks that have the same values as, for example, in the case of R color.

>>> print(np.any(np.diff(a, axis=0).astype(np.bool), axis=1))
[[False False False]
 [ True False False]
 [False False False]
 [ True  True False]
 [False False False]]
>>> print(np.any(np.diff(a, axis=1).astype(np.bool), axis=0))
[[False False False]
 [ True  True  True]
 [False False False]
 [False False False]]

See the following question for details about np.any: How to operate logic operation of all columns of a 2D numpy array .

Now we perform the same operation but for the colors:

>>> print(np.any(np.any(np.diff(a, axis=0).astype(np.bool), axis=1), axis=1))
[False  True False  True False]
>>> print(np.any(np.any(np.diff(a, axis=1).astype(np.bool), axis=0), axis=1))
[False  True False False]

And finally, using np.insert to add True in the beginning of the arrays to take into account the first elements:

>>> print(np.insert(np.any(np.any(np.diff(a, axis=0).astype(np.bool), axis=1), axis=1), 0, True))
[ True False  True False  True False]
>>> print(np.insert(np.any(np.any(np.diff(a, axis=1).astype(np.bool), axis=0), axis=1), 0, True))
[ True False  True False False]

And now use these indices with np.ix_ to get the desired result:

columns_mask = np.insert(np.any(np.any(np.diff(a, axis=0).astype(np.bool), axis=1), axis=1), 0, True)
rows_mask = np.insert(np.any(np.any(np.diff(a, axis=1).astype(np.bool), axis=0), axis=1), 0, True)

>>> print(a[np.ix_(columns_mask, rows_mask)])
[[[1 2 3]
  [2 4 7]]

 [[1 2 3]
  [3 4 7]]

 [[6 4 3]
  [0 1 7]]]

This is it!

We can check if it's correct for separate colors:

>>> print(a[np.ix_(columns_mask, rows_mask)][:, :, 0])  # R
[[1 2]
 [1 3]
 [6 0]]
>>> print(a[np.ix_(columns_mask, rows_mask)][:, :, 1])  # G
[[2 4]
 [2 4]
 [4 1]]
>>> print(a[np.ix_(columns_mask, rows_mask)][:, :, 2])  # B
[[3 7]
 [3 7]
 [3 7]]

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

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