import os
import cv2
import numpy as np
def getMousePos(leftimg,rightimg,disparity,cost):
def onmouse(event, x, y, flags, param):
cv2.imshow("img", left)
# 双击左键,显示鼠标位置
if event == cv2.EVENT_LBUTTONDBLCLK:
cv2.circle(left, (x, y), 2, (0, 0, 255), -1)
print(x,y) #x是列,y是行
costimg = np.zeros([400, 64 * 5, 3], np.uint8)
for i in range(64):
cv2.circle(costimg,(i*5,(200-cost[y][x][i])),2,(0,0,255),-1)
cv2.imshow("cost", costimg)
if cv2.waitKey() & ord("d"):
cv2.destroyWindow("cost")
left = cv2.imread(leftimg)
#right = cv2.imread(rightimg)
#disp = cv2.imread(disparity)
cv2.namedWindow('img')
cost = np.zeros([240,320,64],np.uint16)
with open("image/cost/1.txt") as file:
content = file.read()
temp = content.split(' ')
for i in range(240):
print(i)
for j in range(320):
for k in range(64):
cost[i][j][k] = int(temp[(i*240+j)*64+k])
print("读取txt完毕")
cv2.setMouseCallback("img", onmouse)
if cv2.waitKey() & 0xFF == 27: # 按下‘q'键,退出
cv2.destroyAllWindows()
return
if __name__ == '__main__':
imgpath = "image"
for f in os.listdir(imgpath+'/left'):
leftimg = imgpath+"/left/"+f
rightimg = imgpath+"/right/"+f
disparity = imgpath+"/disp/"+f
cost = imgpath+"/cost/"+ f.split(".")[0]+".txt"
getMousePos(leftimg,rightimg,disparity,cost)