我有数以万计的图片。我想为每个像素生成一个直方图。我使用NumPy编写了以下代码来执行此操作:
import numpy as np
import matplotlib.pyplot as plt
nimages = 1000
im_shape = (64,64)
nbins = 100
# predefine the histogram bins
hist_bins = np.linspace(0,1,nbins)
# create an array to store histograms for each pixel
perpix_hist = np.zeros((64,64,nbins))
for ni in range(nimages):
#create a simple image with normally distributed pixel values
im = np.random.normal(loc=0.5,scale=0.05,size=im_shape)
#sort each pixel into the predefined histogram
bins_for_this_image = np.searchsorted(hist_bins, im.ravel())
bins_for_this_image = bins_for_this_image.reshape(im_shape)
#this next part adds one to each of those bins
#but this is slow as it loops through each pixel
#how to vectorize?
for i in range(im_shape[0]):
for j in range(im_shape[1]):
perpix_hist[i,j,bins_for_this_image[i,j]] += 1
# plot histogram for a single pixel
plt.plot(hist_bins,perpix_hist[0,0])
plt.xlabel('pixel values')
plt.ylabel('counts')
plt.title('histogram for a single pixel')
plt.show()
我想知道有没有人能帮我把for循环矢量化?我想不出如何正确地索引perpix_hist数组。我有数以万计的图像,每张图像都是1500x1500像素,这太慢了。
1条答案
按热度按时间pgccezyw1#
您可以使用
np.meshgrid
将其矢量化,并提供第一、第二和第三维(您已经拥有的最后一维)的索引。