代码是基于windows10操作系统下VS2013编译器里运行的。
当初好奇orbslam2可不可以直接外接摄像头实时运行,在网上冲浪许久,并没有找到在windows10上面运行的代码,网上那些外接摄像头的代码几乎都是在Linux系统下运行的。
在这种情况下,我决定把自己鼓捣出来的能在Windows10系统下运行的代码发出来,Linux系统外接摄像头的我也实现过了,Linux还是方便啊,只要多加几句指令就可以成功。
在GitHub下载orbslam2源代码到自己的电脑上,其中一系列的配置环境,修改参数之类的操作我就不在这里bb了,相关的帖子一大堆。外接笔记本自带摄像头的操作很简单,将mono_kitti.cpp改成以下代码即可:
简略版
#include <iostream>
#include <algorithm>
#include <fstream>
#include <chrono>
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>
#include <System.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <stdio.h>
#include <Windows.h>
#include <process.h>
#include <conio.h>
#include "string.h"
unsigned int g_nPayloadSize = 0;
using namespace std;
using namespace cv;
enum CONVERT_TYPE
{
OpenCV_Mat = 0, // Most of the time, we use 'Mat' format to store image data after OpenCV V2.1
};
int main(int argc, char **argv)
{
if (argc != 4)
{
cerr << endl << "Usage: ./path_to_PF_ORB path_to_vocabulary path_to_settings path_to_dev_video" << endl;
return 1;
}
cv::VideoCapture cap(0);
if (!cap.isOpened())
{
cerr << endl << "Could not open camera feed." << endl;
return -1;
}
// Create SLAM system. It initializes all system threads and gets ready to process frames.
ORB_SLAM2::System SLAM(argv[1], argv[2], ORB_SLAM2::System::MONOCULAR, true);
cout << endl << "-------" << endl;
cout << "Start processing sequence ..." << endl;
// Main loop
int timeStamps = 0;
for (;; timeStamps++)
{
//Create a new Mat
cv::Mat frame;
//Send the captured frame to the new Mat
cap >> frame;
// Pass the image to the SLAM system
SLAM.TrackMonocular(frame, timeStamps);
}
// Stop all threads
SLAM.Shutdown();
// Save camera trajectory
SLAM.SaveKeyFrameTrajectoryTUM("KeyFrameTrajectory.txt");
return 0;
}
结合海康sdk版本
#pragma comment(lib, "MvCameraControl.lib")
#include <iostream>
#include <algorithm>
#include <fstream>
#include <chrono>
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>
#include <System.h>
#include "MvCameraControl.h"
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <stdio.h>
#include <Windows.h>
#include <process.h>
#include <conio.h>
#include "string.h"
unsigned int g_nPayloadSize = 0;
using namespace std;
enum CONVERT_TYPE
{
OpenCV_Mat = 0, // Most of the time, we use 'Mat' format to store image data after OpenCV V2.1
};
// print the discovered devices information to user 将发现的设备信息打印给用户
bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo)
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE)
{
printf("UserDefinedName: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
printf("Serial Number: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chSerialNumber);
printf("Device Number: %d\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.nDeviceNumber);
}
else
printf("Not support.\n");
return true;
}
int RGB2BGR(unsigned char* pRgbData, unsigned int nWidth, unsigned int nHeight)
{
if (NULL == pRgbData)
{
return MV_E_PARAMETER;
}
for (unsigned int j = 0; j < nHeight; j++)
{
for (unsigned int i = 0; i < nWidth; i++)
{
unsigned char red = pRgbData[j * (nWidth * 3) + i * 3];
pRgbData[j * (nWidth * 3) + i * 3] = pRgbData[j * (nWidth * 3) + i * 3 + 2];
pRgbData[j * (nWidth * 3) + i * 3 + 2] = red;
}
}
return MV_OK;
}
// convert data stream in Mat format 以Mat格式转换数据流
bool Convert2Mat(MV_FRAME_OUT_INFO_EX* pstImageInfo, unsigned char * pData)
{
cv::Mat srcImage;
if (pstImageInfo->enPixelType == PixelType_Gvsp_Mono8)
{
srcImage = cv::Mat(pstImageInfo->nHeight, pstImageInfo->nWidth, CV_8UC1, pData);
}
else if (pstImageInfo->enPixelType == PixelType_Gvsp_RGB8_Packed)
{
RGB2BGR(pData, pstImageInfo->nWidth, pstImageInfo->nHeight);
srcImage = cv::Mat(pstImageInfo->nHeight, pstImageInfo->nWidth, CV_8UC3, pData);
}
else
{
printf("unsupported pixel format\n");
return false;
}
if (NULL == srcImage.data)
{
return false;
}
//save converted image in a local file 保存
// try {
//#if defined (VC9_COMPILE)
// cvSaveImage("MatImage.jpg", &(IplImage(srcImage)));
//#else
// cv::imwrite("MatImage.jpg", srcImage);
//#endif
// }
// catch (cv::Exception& ex) {
// fprintf(stderr, "Exception saving image to bmp format: %s\n", ex.what());
// }
srcImage.release();
return true;
}
int main(int argc, char **argv)
{
if (argc != 4)
{
cerr << endl << "Usage: ./path_to_PF_ORB path_to_vocabulary path_to_settings path_to_dev_video" << endl;
return 1;
}
cv::VideoCapture cap(0);
if (!cap.isOpened())
{
cerr << endl << "Could not open camera feed." << endl;
return -1;
}
// Create SLAM system. It initializes all system threads and gets ready to process frames.
ORB_SLAM2::System SLAM(argv[1], argv[2], ORB_SLAM2::System::MONOCULAR, true);
cout << endl << "-------" << endl;
cout << "Start processing sequence ..." << endl;
// Main loop
int timeStamps = 0;
for (;; timeStamps++)
{
//Create a new Mat
cv::Mat frame;
//Send the captured frame to the new Mat
cap >> frame;
// Pass the image to the SLAM system
SLAM.TrackMonocular(frame, timeStamps);
}
// Stop all threads
SLAM.Shutdown();
// Save camera trajectory
SLAM.SaveKeyFrameTrajectoryTUM("KeyFrameTrajectory.txt");
return 0;
}