基于未标定立体系统的Opencv深度图

2024-10-06 07:18:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从一个未校准的方法得到深度图。 通过SIFT方法和cv2.findFundamentalMat得到基本矩阵。然后用cv2.stereoRectifyUncalibrated得到校正矩阵。最后,我可以使用cv2.warpPerspective来校正和计算视差,但后者并不能得到一个好的深度图。这个值非常高,所以我想知道我是否必须使用warpPerspective,或者我必须从用stereoRectifyUncalibrated得到的单应矩阵来计算旋转矩阵。在

我不确定用stereoRectifyUncalibrated校正的单应矩阵的情况下的射影矩阵。在

代码的一部分:

#Obtainment of the correspondent point with SIFT
sift = cv2.SIFT()

###find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(dst1,None)
kp2, des2 = sift.detectAndCompute(dst2,None)

###FLANN parameters
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=50)

flann = cv2.FlannBasedMatcher(index_params,search_params)
matches = flann.knnMatch(des1,des2,k=2)

good = []
pts1 = []
pts2 = []

###ratio test as per Lowe's paper
for i,(m,n) in enumerate(matches):
    if m.distance < 0.8*n.distance:
        good.append(m)
        pts2.append(kp2[m.trainIdx].pt)
        pts1.append(kp1[m.queryIdx].pt)


pts1 = np.array(pts1)
pts2 = np.array(pts2)

#Computation of the fundamental matrix
F,mask= cv2.findFundamentalMat(pts1,pts2,cv2.FM_LMEDS)


# Obtainment of the rectification matrix and use of the warpPerspective to transform them...
pts1 = pts1[:,:][mask.ravel()==1]
pts2 = pts2[:,:][mask.ravel()==1]

pts1 = np.int32(pts1)
pts2 = np.int32(pts2)

p1fNew = pts1.reshape((pts1.shape[0] * 2, 1))
p2fNew = pts2.reshape((pts2.shape[0] * 2, 1))

retBool ,rectmat1, rectmat2 = cv2.stereoRectifyUncalibrated(p1fNew,p2fNew,F,(2048,2048))

dst11 = cv2.warpPerspective(dst1,rectmat1,(2048,2048))
dst22 = cv2.warpPerspective(dst2,rectmat2,(2048,2048))

#calculation of the disparity
stereo = cv2.StereoBM(cv2.STEREO_BM_BASIC_PRESET,ndisparities=16*10, SADWindowSize=9)
disp = stereo.compute(dst22.astype(uint8), dst11.astype(uint8)).astype(np.float32)
plt.imshow(disp);plt.colorbar();plt.clim(0,400)#;plt.show()
plt.savefig("0gauche.png")

#plot depth by using disparity focal length `C1[0,0]` from stereo calibration and `T[0]` the distance between cameras

plt.imshow(C1[0,0]*T[0]/(disp),cmap='hot');plt.clim(-0,500);plt.colorbar();plt.show()

这里是用未标定方法校正的图片(和warpPerspective): enter image description here

这里是用校准方法校正的照片: enter image description here

我不知道这两种图片之间的区别是多么的重要…而对于校准方法来说,它似乎并不一致…奇怪 未标定方法的视差图:

enter image description here

深度图的计算方法是:C1[0,0]*T[0]/(disp) 来自stereoCalibrate的T,但是值非常高。。。在

----------稍后编辑----------------

我试图用“立体校正校正”得到的单应矩阵来“装配”重建矩阵([Devernay97][Garcia01]),但效果并不理想。。。我的用法正确吗?在

^{pr2}$

Tags: ofthe方法npplt矩阵paramscv2