我有一个问题,a有一堆图像,我必须为它们生成直方图。但是我必须为每个像素生成一个直方图。一、 e,对于n幅图像的集合,我必须计算像素0,0假设的值并生成直方图,0,1,0,2等等也是如此。为此,我编写了以下方法:class ImageData:
def generate_pixel_histogram(self, images, bins):
"""
Generate a histogram of the image for each pixel, counting
the values assumed for each pixel in a specified bins
"""
max_value = 0.0
min_value = 0.0
for i in range(len(images)):
image = images[i]
max_entry = max(max(p[1:]) for p in image.data)
min_entry = min(min(p[1:]) for p in image.data)
if max_entry > max_value:
max_value = max_entry
if min_entry < min_value:
min_value = min_entry
interval_size = (math.fabs(min_value) + math.fabs(max_value))/bins
for x in range(self.width):
for y in range(self.height):
pixel_histogram = {}
for i in range(bins+1):
key = round(min_value+(i*interval_size), 2)
pixel_histogram[key] = 0.0
for i in range(len(images)):
image = images[i]
value = round(Utils.get_bin(image.data[x][y], interval_size), 2)
pixel_histogram[value] += 1.0/len(images)
self.data[x][y] = pixel_histogram
矩阵的每个位置都存储一个表示直方图的字典。但是,我如何对每个像素进行计算,这个微积分需要相当长的时间,这对我来说是一个很好的并行化问题。但我没有这方面的经验,也不知道怎么做。在
编辑:
我试了一下@Eelco Hoogendoorn告诉我的,效果很好。但是将它应用到我的代码中,其中的数据是用这个构造函数生成的大量图像(在计算完这些值之后,而不仅仅是0),我得到了一个0数组[0 0 0]。我传递给histogram方法的是一个ImageData数组。在
^{pr2}$
现在我得到:hist_data = np.apply_along_axis(hist, 0, data)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/shape_base.py", line 104, in apply_along_axis
outshape[axis] = len(res)
TypeError: object of type 'NoneType' has no len()
任何帮助都将不胜感激。
提前谢谢。在