代码如下,
不知道真假
#include "stdafx.h"
#include
#include
#include
#include
using namespace std;
using namespace cv;
#include
#include
#include
#include
#include
#include
#include
int main()
{
//IplImage * img1 = cvLoadImage("left.png",0);
//IplImage * img2 = cvLoadImage("right.png",0);
//IplImage * img1 = cvLoadImage("tsukuba_l.png",0);
//IplImage * img2 = cvLoadImage("tsukuba_r.png",0);
IplImage * img1 = cvLoadImage("modelface_left_calib.bmp", 0);
IplImage * img2 = cvLoadImage("modelface_right_calib.bmp", 0);
//IplImage * img1 = cvLoadImage("rectifyImageL.jpg", 0);
//IplImage * img2 = cvLoadImage("rectifyImageR.jpg", 0);
CvStereoGCState* GCState = cvCreateStereoGCState(64, 3);
assert(GCState);
cout << "start matching using GC" << endl;
CvMat* gcdispleft = cvCreateMat(img1->height, img1->width, CV_16S);
CvMat* gcdispright = cvCreateMat(img2->height, img2->width, CV_16S);
CvMat* gcvdisp = cvCreateMat(img1->height, img1->width, CV_8U);
int64 t = getTickCount();
cvFindStereoCorrespondenceGC(img1, img2, gcdispleft, gcdispright, GCState);
t = getTickCount() - t;
cout << "Time elapsed:" << t * 1000 / getTickFrequency() << endl;
//cvNormalize(gcdispleft,gcvdisp,0,255,CV_MINMAX);
cvSaveImage("GC_left_disparity.png", gcvdisp);
//cvNormalize(gcdispright, gcvdisp, 0, 255, CV_MINMAX);
//cvSaveImage("GC_right_disparity.png", gcvdisp);
cvNamedWindow("GC_disparity", 0);
cvShowImage("GC_disparity", gcvdisp);
cvWaitKey(0);
cvReleaseMat(&gcdispleft);
cvReleaseMat(&gcdispright);
cvReleaseMat(&gcvdisp);
return 0;
}
opencv2.4可使用nuget获取
mayyou need this
import numpy as np
import cv2
Q = np.array([[ 1.00000000e+00, 0.00000000e+00, 0.00000000e+00,
-3.17299198e+02],
[ 0.00000000e+00, 1.00000000e+00, 0.00000000e+00,
-1.82994383e+02],
[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
4.71728178e+02],
[ 0.00000000e+00, 0.00000000e+00, 2.48131104e-02,
-0.00000000e+00]])
disp = cv2.imread('GC_left_disparity.png', 0)
disp = disp.astype(np.float32)/16
threeD = cv2.reprojectImageTo3D(disp, Q)
depth = threeD[:,:,2]
height = depth.shape[0]
width =depth.shape[1]
xyz_file = open('depth.txt', 'w+')
for i in range(height):
for j in range(width):
#if j >= 64 and j<=608 and i >= 18 and i <= 342:
#xyz_file.writelines(str(i - 18) + ' ' + str(j-64) + ' ' + str(depth[i][j]) + '\n')
if j >= 64 and j<=576 and i >= 18 and i <= 342:
xyz_file.writelines(str(i - 18) + ' ' + str(j-64) + ' ' + str(depth[i][j]) + '\n')
xyz_file.close()