kinect fusion+opencv程序

#define _CRT_SECURE_NO_WARNINGS
#include "pch.h"
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html

// This code is also subject to the license terms in the LICENSE_KinectFusion.md file found in this module's directory

#include <iostream>
#include <fstream>
#include <opencv2/imgproc.hpp>
#include <opencv2/calib3d.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/rgbd/kinfu.hpp>

using namespace cv;
using namespace cv::kinfu;
using namespace std;


#define CV_ROOT "d:/opencv430contrib"
#define CV_VERSION_ID CVAUX_STR(CV_MAJOR_VERSION) CVAUX_STR(CV_MINOR_VERSION) CVAUX_STR(CV_SUBMINOR_VERSION)
#if CV_MAJOR_VERSION>2
#ifdef _DEBUG
#define cvLIB(name) CV_ROOT "/x64/vc15/lib/opencv_world" CV_VERSION_ID "d"
#else
#define cvLIB(name) CV_ROOT "/x64/vc15/lib/opencv_world" CV_VERSION_ID
#endif
#pragma comment( lib, cvLIB("any"))
#else
#ifdef _DEBUG
#define cvLIB(name) CV_ROOT "/build/x64/vc15/lib/opencv_" name CV_VERSION_ID "d"
#else
#define cvLIB(name) CV_ROOT "/build/x64/vc15/lib/opencv_" name CV_VERSION_ID
#endif
#pragma comment( lib, cvLIB("core"))
#pragma comment( lib, cvLIB("imgproc"))
#pragma comment( lib, cvLIB("highgui"))
#endif

static vector<string> readDepth(std::string fileList)
{
	vector<string> v;
	fstream file(fileList);
	if (!file.is_open())
		throw std::runtime_error("Failed to read depth list");
	std::string dir;
	size_t slashIdx = fileList.rfind('/');
	slashIdx = slashIdx != std::string::npos ? slashIdx : fileList.rfind('\\');
	dir = fileList.substr(0, slashIdx);

	while (!file.eof())
	{
		std::string s, imgPath;
		std::getline(file, s);
		if (s.empty() || s[0] == '#') continue;
		std::stringstream ss;
		ss << s;
		double thumb;
		ss >> thumb >> imgPath;
		ss >> imgPath;
		cout << imgPath << endl;
		v.push_back(dir + '/' + imgPath);
	}

	return v;
}

struct DepthSource
{
public:
	DepthSource(String fileListName) :
		depthFileList(fileListName.empty() ? vector<string>() : readDepth(fileListName)),
		frameIdx(0),
	//	undistortMap1(),
	//	undistortMap2(),
		useKinect2Workarounds(true)
	{ }

	UMat getDepth()
	{
		UMat out;
		if (frameIdx < depthFileList.size())
		{
			cout << depthFileList[frameIdx] << endl;
			Mat f = cv::imread(depthFileList[frameIdx++], IMREAD_ANYDEPTH);
			f.copyTo(out);
		}
		else
		{
			return UMat();
		}
		if (out.empty())
			throw std::runtime_error("Matrix is empty");
		return out;
	}

	bool empty()
	{
		return depthFileList.empty();
	}
	vector<string> depthFileList;
	size_t frameIdx;
//	UMat undistortMap1, undistortMap2;
	bool useKinect2Workarounds;
};

int main(int argc, char **argv)
{
	Ptr<DepthSource> ds = makePtr<DepthSource>("rgbd_dataset/depth.txt");
	Ptr<Params> params = Params::defaultParams();	
	cv::setUseOptimized(true);
//	params->frameSize = Size(512, 424);
	Ptr<KinFu> kf = KinFu::create(params);
	UMat rendered,points,normals;
/*	UMat cvt8;
	Mat frame = imread("images/8hao/depth/KinectScreenshot-Depth-05-27-31.bmp", IMREAD_ANYDEPTH);
	convertScaleAbs(frame, cvt8, 0.25*256. / params->depthFactor);
	imshow("depth", cvt8);
	if (!kf->update(frame))
	{
		kf->reset();
		std::cout << "reset" << std::endl;
	}
	kf->render(rendered);
	imshow("render", rendered);
	while (cv::waitKey(100) != 'q')//如果不使用數據集,程序到這裏就結束了。
		;
*/



	int64 prevTime = getTickCount();
	int i = 0;
	for (UMat frame = ds->getDepth(); !frame.empty(); frame = ds->getDepth())
	{
		cout << i << endl;
		i++;
		UMat cvt8;
		convertScaleAbs(frame, cvt8, 0.25*256. / params->depthFactor);
		imshow("depth", cvt8);
		if (!kf->update(frame))
		{
			kf->reset();
			std::cout << "reset" << std::endl;
		} 
		kf->render(rendered);
		int64 newTime = getTickCount();
		putText(rendered, cv::format("FPS: %2d press R to reset, P to pause, Q to quit",
			(int)(getTickFrequency() / (newTime - prevTime))),
			Point(0, rendered.rows - 1), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 255, 255));
		prevTime = newTime;
		imshow("render", rendered);
		int c = waitKey(1);
		switch (c)
		{
		case 'r':
			kf->reset();
		case 'q':
			return 0;
		default:
			break;
		}
		if (i > 10)//10幀停止
			break;
	} 
	return 0;
}

 上邊這個主要是原版刪減了大量無關內容,但對小白來說理解起來還是費勁。下面這個纔是真正的小白版,全部代碼加上鍊接lib文件和頭文件不足50行。

#define _CRT_SECURE_NO_WARNINGS
#include "pch.h"
#include <iostream>
#include <fstream>
#include <opencv2/imgproc.hpp>
#include <opencv2/calib3d.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/rgbd/kinfu.hpp>
using namespace cv;
using namespace cv::kinfu;
using namespace std;
#define CV_ROOT "d:/opencv430contrib"
#define CV_VERSION_ID CVAUX_STR(CV_MAJOR_VERSION) CVAUX_STR(CV_MINOR_VERSION) CVAUX_STR(CV_SUBMINOR_VERSION)
#ifdef _DEBUG
#define cvLIB(name) CV_ROOT "/x64/vc15/lib/opencv_world" CV_VERSION_ID "d"
#else
#define cvLIB(name) CV_ROOT "/x64/vc15/lib/opencv_world" CV_VERSION_ID
#endif
#pragma comment( lib, cvLIB("any"))

int main()
{
	Ptr<Params> params = Params::defaultParams();	
	cv::setUseOptimized(true);
	params->frameSize = Size(512, 424);//自己深度圖像的尺寸
	Ptr<KinFu> kf = KinFu::create(params);
	UMat rendered,points,normals,cvt8;
	Mat frame = imread("images/8hao/depth/KinectScreenshot-Depth-05-27-31.bmp", IMREAD_ANYDEPTH);
	convertScaleAbs(frame, cvt8, 0.25*256. / params->depthFactor);
	imshow("depth", cvt8);
	if (!kf->update(frame))
	{
		kf->reset();
		std::cout << "reset" << std::endl;
	}
	kf->render(rendered);
	imshow("render", rendered);
	while (cv::waitKey(100) != 'q')
		;
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章