立體匹配gc算法

代碼如下,
不知道真假

#include "stdafx.h"
#include <opencv\highgui.h>
#include <opencv\cv.h>
#include <opencv\cxcore.h>
#include <iostream>
using namespace std;
using namespace cv;
#include <opencv2/opencv.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\calib3d\calib3d.hpp>
#include <opencv2\features2d\features2d.hpp>
#include <opencv2\legacy\legacy.hpp>
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()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章