我正在尝试编写使用 numpy 和 scipy 生成视差图的代码,但是我在图像的 numpy 数组中存储的值与输出图像中实际显示的值完全不同,并使用杂项保存。我保存。例如,在数组中,没有一个值大于 22,但在图像中,我有从 0 到 255 的完整范围的值。我认为也许 imsave 正在拉伸这些值,以便最大值显示为图像中的值是 255,但我使用 imsave 创建的其他图像的最大值低于 255。
这些是我用来创建视差图的函数,给定两个沿 x 轴移动的 pgm 图像:
def disp(i, j, winSize, leftIm, rightIm): #calculate disparity for a given point
width = leftIm.shape[1]
height = leftIm.shape[0]
w = winSize / 2
minSAD = 9223372036854775807 #max int
for d in range(23):
SAD = 0.0 #SAD
k = i - w
v = i + w
m = j - w
n = j + w
for p in range(k, v+1): #window - x
for q in range(m, n+1): #window y
if(p - d > 0 and p < width and q < height):
SAD += abs((int(leftIm[q][p]) - int(rightIm[q][p - d])))
if(SAD < minSAD):
minSAD = SAD
disp = d
# print "%d, %d" % (i, j)
return (disp, SAD)
def dispMap(winSize, leftIm, rightIm):
width = leftIm.shape[1]
height = leftIm.shape[0]
outIm = np.zeros((height, width))
SADstore = np.zeros((height, width))
w = winSize / 2
for i in range(w, width-w):
for j in range(w, height/3-w):
dispout = disp(i, j, winSize, leftIm, rightIm)
outIm[j][i] = 1 * dispout[0] #should normally multiply by 4
SADstore[j][i] = dispout[1]
return (outIm, SADstore)
忽略 SAD/SAD 存储返回值,我已确保这些不会影响我当前的进程。
这是我用来获取输出的代码:
disp12 = dispMap(9, view1, view2)
disp12im = disp12[0]
misc.imsave('disp121.pgm', disp12im)
目前, disp12im 中的任何内容都不应该 > 23。如果我运行 for 循环来检查数组上的这一点,这仍然是正确的。但是,如果我加载保存的图像并对值运行相同的 for 循环,我会得到大量超过 23 的数字。我做错了什么?