numpy histogram布尔索引与维度0的索引数组不匹配;维度是

时间:2017-12-02 12:33:24

标签: python arrays numpy

我试图通过创建每个图像中像素强度的直方图来减去天文图像,然后将天空值设置为等于具有最高频率的bin的强度。然后想法是从该帧内的每个像素中减去该天空值.IndexError:布尔索引与维度0的索引数组不匹配; dimension是3651469但相应的布尔维度是3651468

#sciFlat is a list containing three images in array form.

sciFlat = np.asarray(sciFlat)
minpix = min(sciFlat.flatten())
maxpix = max(sciFlat.flatten())
rng = int(maxpix-minpix)
#These are histogram ranges, now loop through each image.
#Sky subtract science images.
sciSky = []
for i in range(3):
    hf = np.histogram(sciFlat[i].flatten(), bins=rng, range=(minpix,maxpix))
    skyval = hf[1][hf[0] == max(hf[0])]
    print(skyval)
    skySub = sciFlat[i] - skyval
    sciSky.append(skySub)

我希望代码能够成功完成,因为numpy.histogram应该返回hist(一个大小为n的扁平数组)和bin_edges(长度为n的1D数组)。

IndexError Traceback(最近一次调用最后一次)

142     hf = np.histogram(sciFlat[i].flatten(), bins=rng, range=(minpix,maxpix))
143---> **skyval = hf[1][hf[0] == max(hf[0])]** <----
144     print(skyval)
145     skySub = sciFlat[i] - skyval

IndexError:布尔索引与维度0的索引数组不匹配; dimension是3651469但相应的布尔维度是3651468

1 个答案:

答案 0 :(得分:3)

我认为只是你的逻辑在循环中失败,写出直方图输出:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Syntax error on token "firstNumber", ++ expected after this token

at ForLoop.main(ForLoop.java:16)

直方图为您提供值的边缘,您最想要的是边缘的中点:

for i in range(3):
    hist, edges = np.histogram(sciFlat[i].flatten(), bins=rng, range=(minpix,maxpix))
    skyval = edges[hist == max(hist)]
    print(skyval)
    skySub = sciFlat[i] - skyval
    sciSky.append(skySub)

通过设置mids而不是边缘,您的直方图具有相同的尺寸。为了说明边和中间的差异:

for i in range(3):
    hist, edges = np.histogram(sciFlat[i].flatten(), bins=rng, range=(minpix,maxpix))
    mids = edges[:-1] + np.diff(edges)/2
    skyval = mids[hist.argmax()]
    print(skyval)
    skySub = sciFlat[i] - skyval
    sciSky.append(skySub)

Star表示最大的中点,如您所见,它位于边缘之间: enter image description here