使用Open-CV Python创建具有对角线视差的立体bm显示贴图

2024-09-29 17:20:04 发布

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

我有一个立体对,想创建一个视差图。然而,这两幅图像之间的转换并不是简单的从左向右或上下移动,而是两者的某种结合。我曾尝试在opencvpython中使用StereoBM函数,但结果是图像上有黑白对角线。我的问题是,是否可以使用视差在对角线方向上的两幅图像来计算视差图,或者是否需要旋转图像才能使该功能工作?在

编辑:在阅读了下面的答案,并做了一些研究后,我决定试试立体校正功能。我首先用SURF在第一个图像中找到关键点,然后对第二个图像重复这个步骤。然后我使用基于FLANN的匹配器来匹配这些点,并移除异常值。然后使用findFundamentalMat函数找到基本mat,然后调用stereorectilyuncalibrated。但是,我得到的错误是这样开头的:(-215)CV_IS_MAT(_points1)&;CV_IS_MAT(_points2)&;(\u points1->;rows==1 | | u points1->;cols==1)&;。。。在

我已经确定了所有的数据类型都是相同的,并且每个点阵列的维度都是相同的。我把代码的一部分放在下面使用stereorectilyuncalibrated的地方。在

#Detect feature points with SURF
detector = cv2.SURF()
kp1, desc1 = detector.detectAndCompute(img1, None)
kp2, desc2 = detector.detectAndCompute(img2, None)

#Match Points
FLANN_INDEX_KDTREE = 1  # bug: flann enums are missing
flann_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
matcher = cv2.FlannBasedMatcher(flann_params, {})
matches = matcher.knnMatch(desc1, trainDescriptors = desc2, k=2)
mkp1, mkp2 = [], []
ratio = 0.75
for m in matches:
    if len(m) == 2 and m[0].distance < m[1].distance * ratio:
        m = m[0]
        mkp1.append( kp1[m.queryIdx] )
        mkp2.append( kp2[m.trainIdx] )
np.float32([kp.pt for kp in mkp1])
p1 = np.float32([kp.pt for kp in mkp1])
p2 = np.float32([kp.pt for kp in mkp2])
kp_pairs = zip(mkp1, mkp2)
H, status = cv2.findHomography(p1, p2, cv2.RANSAC, 5.0)
print '%d / %d  inliers/matched' % (np.sum(status), len(status))
statusmat = np.zeros((max(status.shape),2),dtype = np.float64)
statusmat[:,0] = status[:,0]
statusmat[:,1] = status[:,0]
status = np.array(status, dtype=bool)
p1f=p1[status.view(np.ndarray).ravel()==1,:] #Remove Outliers
p2f=p2[status.view(np.ndarray).ravel()==1,:] #Remove Outliers

#Attempt to rectify using stereoRectifyUncalibrated
fundmat, mask = cv2.findFundamentalMat(p1f,p2f,cv2.RANSAC,3,0.99,)
rectmat1, rectmat2 = cv2.stereoRectifyUncalibrated(p1f,p2f,fundmat,imgsize)

谢谢你到目前为止的答案!在


Tags: in图像forstatusnpcv2detectorsurf
1条回答
网友
1楼 · 发布于 2024-09-29 17:20:04

似乎这个函数stereorectilyuncalibrated采用行或列向量,而不是nx2矩阵 输出似乎也有3个元素

p1fNew = p1f.reshape((p1f.shape[0] * 2, 1))
p2fNew = p2f.reshape((p2f.shape[0] * 2, 1))

retBool ,rectmat1, rectmat2 = cv2.stereoRectifyUncalibrated(p1fNew,p2fNew,fundmat,imgsize)

相关问题 更多 >

    热门问题