首先,我已经阅读了这个问题
我有一个来自图片的np.array
[[255 255 255 ... 255 255 255]
[255 255 0 ... 255 255 255]
[255 255 255 ... 255 255 255]
...
[255 255 0 ... 0 255 255]
[255 255 0 ... 255 255 255]
[255 255 255 ... 255 255 255]]
我想删除行中值为0
的数量小于特定值的行。
我的代码如下:
import numpy
from collections import Counter
for i in range(pixelarray.shape[0]):
# Counter(pixelarray[i])[0] represent the amount of 0 in one row.
if Counter(pixelarray[i])[0] < 2: # check the amount of 0,if it is smaller than 2,delete it.
pixelarray = np.delete(pixelarray,i,axis=0) # delete the row
print(pixelarray)
但是它出现了错误:
Traceback (most recent call last):
File "E:/work/Compile/python/OCR/PictureHandling.py", line 23, in <module>
if Counter(pixelarray[i])[0] <= 1:
IndexError: index 183 is out of bounds for axis 0 with size 183
我该怎么办?
rows_with_min_zeros = pixelarray[(pixelarray == 0).sum(1) >= MIN_ZEROS]
。 - jdehesa