我注意到一个奇怪的错误,我不确定能理解。我有一个四维矩阵(样本,高度,长度,通道)和这个函数来计算每个通道的直方图(处理图片)。
regions.shape #(110, 60, 100, 3)
def GetColorHist(i):
r = measurements.histogram(i[:,:,0], 20, 220, 10) # histogram of reds
g = measurements.histogram(i[:,:,1], 20, 220, 10) # histogram of greens
b = measurements.histogram(i[:,:,2], 20, 220, 10) # histogram of blues
return(np.dstack((r,g,b)))当应用于矩阵(区域)的特定样本时,它工作得很好。例如:
GetColorHist(regions[70])
> array([[[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 600],
[ 0, 1271, 5400],
[ 3, 4729, 0],
[5942, 0, 0],
[ 55, 0, 0]]])但是我没有把它应用到矩阵的相关维数上。
np.apply_along_axis(GetColorHist,0,regions)
<ipython-input-57-246240b60568> in GetColorHist(i)
1 def GetColorHist(i):
----> 2 r = measurements.histogram(i[:,:,0], 20, 220, 10) # histogram of reds
3 g = measurements.histogram(i[:,:,1], 20, 220, 10) # histogram of greens
4 b = measurements.histogram(i[:,:,2], 20, 220, 10) # histogram of blues
5 return(np.dstack((r,g,b)))
IndexError: too many indices for array我尝试了几种方法,比如改变输出形状,但仍然弄得一团糟。有人明白这是怎么回事吗?
谢谢
发布于 2018-01-15 22:39:50
结果发现,迭代可以很好地工作(并且使用pool.map技巧快速)
pool = ThreadPool(multiprocessing.cpu_count()) # get the number of CPU
X = pool.map(GetColorHist,[regions[x] for x in range(regions.shape[0])])
X = np.array(X)
pool.close()
pool.join()https://stackoverflow.com/questions/48271167
复制相似问题