我正在尝试将散点图中显示良好的点和值叠加到图像中。我有一个大小为(1200,1920,3)的图像,以及一个组织为(x,y,value)的点列表,我称之为uv,大小为(3,16425)。当我在散点图上显示这些点时,它们按我希望的方式显示。但是,当我尝试以大小(1200,1920)的数组显示这些点,然后使用imshow显示时,不仅大多数点都没有显示,而且尺寸也向后。我不知道发生了什么,因为在两种情况下都是相同的数据。任何帮助将不胜感激。
代码:
uv_new = np.zeros((img.shape[0],img.shape[1]))
max_depth = np.max((uv[2,:]).astype(np.float64)) # just a normalizer
uv_int = uv.astype(int) # Since I'm putting it in an array, I need integer x,y coordinates
print(img.shape)
x = (uv_int[0]/np.max(uv_int[0])*(img.shape[0]-1)).astype(int) # Normalize x coordinates
y = (uv_int[1]/np.max(uv_int[1])*(img.shape[1]-1)).astype(int) # Normalize y coordinates
z = 1-uv[2]/max_depth # The color values I want to show
uv_new[x,y] = z # Set the image I want to show with the color values
plt.imshow(uv_new,origin='lower') # Show this image which should contain all the points, but doesn't
print(np.count_nonzero(uv_new))
print(np.count_nonzero(z)) # Comparing these two just to show that no data is lost
plt.figure(figsize=(8,5))
#plt.imshow(img)
cm = plt.cm.get_cmap('jet')
scat = plt.scatter(x,y, c=z, s=1, cmap=cm) # What I want, but is different from the image above, even though it is the exact same data
#plt.axis('off')
plt.show()
编辑: 根据对答案的最新评论,这是更新的结果切换,因此uv_new现在具有形状(img.shape 1,img.shape [0]),并移至uv_new [y,x] = z。
答案 0 :(得分:0)
我相信您的代码段中存在一些错误。
uv_int
,因为在计算x和y时,可以使用astype(int)
将它们转换为整数。count_nonzero
返回两个不同值的原因可能是,当执行归一化时,对于z的多个非零值,您会得到一些重复的坐标(x,y)。应用上一点可能有助于丢弃一些重复项。uv_new[x,y] = z
是imshow
情节转置的原因。是的,x
代表X轴,y
代表Y轴,但请记住,在显示矩阵方面,(行,列)实际上等于(y,x)。因此,请将您的通话更新为uv_new[y, x] = z
,并更改uv_new
的形状。cmap='plasma'
和imshow
的两个调用中都添加关键字scatter
。编辑:
这是一个随机生成的数据的实际示例。前两个图与您的操作类似,不同之处在于,使用了interpolation
的{{1}}关键字。最后一个图与第二个图相似,但是更改了背景色以尝试与第一个图匹配,从而减小了散点的大小。看看第一个和最后一个图像如何相似?我认为这可以大致解释为什么您无法通过显示和散布获得相同的地块。片段和图形如下(由于尺寸限制,图形质量非常差,因此请在计算机上执行该片段以获得更好的图像质量。)
imshow