我試圖編寫使用numpy和scipy生成視差貼圖的代碼,但是我存儲在我的numpy數組中的值與我的圖像完全不同,這些值實際上顯示在我的輸出圖像,保存與misc.imsave。例如,在數組中,沒有一個值大於22,但在圖像中,我有從0到255的全部值。我認爲,也許imsave正在擴展值,以便最大值顯示爲255,但我有用imsave創建的其他圖像的最大值低於255.scipy imsave保存錯誤的值
這些是我用來創建我的視差地圖的函數,給出兩個pgm圖像已沿x軸移動:
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/SADstore返回值,我確保這些不影響我當前的過程。
這是我用得到我的輸出代碼:
disp12 = dispMap(9, view1, view2)
disp12im = disp12[0]
misc.imsave('disp121.pgm', disp12im)
由於它的電流,沒有什麼disp12im應> 23.如果我運行一個循環來檢查這個陣列上,這仍然是真實的。但是,如果我加載保存的圖像並在值上循環運行,我會得到超過23的數字。我做錯了什麼?