左右眼圖片
需求
需要將左右眼圖像利用視差生成三維點云數據
先問問chatGPT相關知識
進一步問有沒有現成的軟件
chatGPT提到了OpenCV,我們讓chatGPT用OpenCV寫一個程序來做這個事情
當然,代碼里面會有一些錯誤,chatGPT寫的代碼并不會做模擬運行測試,所以,實際跑起來是會有一些錯誤的
我們打印了一下,是float64的數據類型
轉成float32就可以了,這可能是本地庫版本問題造成的,也不能算chatGPT寫的代碼有錯
運行python程序,生成output.ply點云文件,用CloudCompare軟件打開看一下生成的點云
如果只有兩張圖,生成的點云是有很多缺失的,如果通過左右眼立體視頻,也許可以補全很多信息,chatGPT給出一些解決思路
附:完整代碼
import cv2
import numpy as npdef create_output(vertices, colors, filename):colors = colors.reshape(-1, 3)vertices = np.hstack([vertices.reshape(-1, 3), colors])ply_header = '''plyformat ascii 1.0element vertex %(vert_num)dproperty float xproperty float yproperty float zproperty uchar redproperty uchar greenproperty uchar blueend_header'''with open(filename, 'w') as f:f.write(ply_header % dict(vert_num=len(vertices)))np.savetxt(f, vertices, '%f %f %f %d %d %d')# Load the left and right images in gray scale
imgLeft = cv2.imread('imL.bmp', cv2.IMREAD_GRAYSCALE)
imgRight = cv2.imread('imR.bmp', cv2.IMREAD_GRAYSCALE)
imgColor = cv2.imread('imL.bmp', cv2.IMREAD_COLOR)# Create a StereoBM object
stereo = cv2.StereoBM_create(numDisparities=16, blockSize=15)# Compute the disparity map
disparity = stereo.compute(imgLeft, imgRight)# Normalize the disparity map
norm_coeff = 255 / disparity.max()
disparity = disparity * norm_coeff / 255# Reproject the disparity map into 3D
h, w = imgLeft.shape[:2]
f = 0.8*w # guess for focal length
Q = np.float32([[1, 0, 0, -0.5*w],[0,-1, 0, 0.5*h], # turn points 180 deg around x-axis,[0, 0, 0, -f], # so that y-axis looks up[0, 0, 1, 0]])
print(disparity.dtype)
disparity = np.float32(disparity)points = cv2.reprojectImageTo3D(disparity, Q)# Save the point cloud as a .ply file
colors = imgColor
mask = disparity > disparity.min()
output_points = points[mask]
output_colors = colors[mask]
mask = np.isfinite(output_points).all(axis=1)
output_points = output_points[mask]
output_colors = output_colors[mask]
output_file = 'output.ply'
print(output_points.dtype, output_colors.dtype)
print(output_points.shape, output_colors.shape)
create_output(output_points, output_colors, output_file)