当前位置:网站首页>大恒工业相机搭建双目相机(软件)
大恒工业相机搭建双目相机(软件)
2022-08-08 06:20:00 【开门大弟子】
本文主要介绍使用大恒工业水星139相机搭建双目相机的软件过程.以下主要介绍相机采集图像以及保存的过程。
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <queue>
#include <thread>
#include <mutex>
#include <unistd.h>
#include "opencv2/opencv.hpp"
#include "camera_api.h"
#include "tic_toc.h"
bool time_flag_0 = false;
std::chrono::time_point<std::chrono::system_clock> start_0, end_0;
bool time_flag_1 = false;
std::chrono::time_point<std::chrono::system_clock> start_1, end_1;
std::queue<cv::Mat> img0_buf;
std::queue<unsigned int> frame0_ID_buf;
std::queue<double> time0_buf;
std::mutex m0_buf;
std::queue<cv::Mat> img1_buf;
std::queue<unsigned int> frame1_ID_buf;
std::queue<double> time1_buf;
std::mutex m1_buf;
#define image_width 640
#define image_height 480
char *pRGB24Buf_0 = new char[image_width * image_height * 3];
char *pRGB24Buf_1 = new char[image_width * image_height * 3];
void Frame_0_ProcessRGB(GX_FRAME_CALLBACK_PARAM* pFrame)
{
if (pFrame->status == GX_FRAME_STATUS_SUCCESS)
{
std::chrono::time_point<std::chrono::system_clock,std::chrono::microseconds> tp =
std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());//获取当前时间点
double time_now = tp.time_since_epoch().count();
start_0 = std::chrono::system_clock::now();
//double time_now = start_0.time_since_epoch().count();
if(!time_flag_0)
{
end_0 = start_0;
time_flag_0 = true;
}
else
{
std::chrono::duration<double> elapsed_seconds = start_0 - end_0;
std::cout << "the CAM_0 frame frequency is: " << 1/elapsed_seconds.count() << std::endl;
end_0 = start_0;
}
//缓冲区初始化*/
memset(pRGB24Buf_0,0,pFrame->nWidth * pFrame->nHeight * 3 * sizeof(char)); //缓冲区初始化*/
DX_BAYER_CONVERT_TYPE cvtype = RAW2RGB_NEIGHBOUR; //选择插值算法
DX_PIXEL_COLOR_FILTER nBayerType = BAYERRG; //选择图像Bayer格式
bool bFlip = false;
VxInt32 DxStatus = DxRaw8toRGB24(const_cast<void *>(pFrame->pImgBuf),pRGB24Buf_0,pFrame->nWidth,pFrame->nHeight,cvtype,nBayerType,bFlip);
if (DxStatus != DX_OK)
{
return ;
}
cv::Mat image_rgb24(pFrame->nHeight, pFrame->nWidth, CV_8UC3);
memcpy(image_rgb24.data, pRGB24Buf_0, pFrame->nHeight * pFrame->nWidth * 3);
unsigned int frame_id = pFrame->nFrameID;
m0_buf.lock();
img0_buf.push(image_rgb24);
frame0_ID_buf.push(frame_id);
time0_buf.push(time_now);
m0_buf.unlock()
}
return ;
}
void Frame_1_ProcessRGB(GX_FRAME_CALLBACK_PARAM* pFrame)
{
if (pFrame->status == GX_FRAME_STATUS_SUCCESS)
{
std::chrono::time_point<std::chrono::system_clock,std::chrono::microseconds> tp =
std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());//获取当前时间点
double time_now = tp.time_since_epoch().count();
start_1 = std::chrono::system_clock::now();
if(!time_flag_1)
{
end_1 = start_1;
time_flag_1 = true;
}
else
{
std::chrono::duration<double> elapsed_seconds = start_1 - end_1;
std::cout << "the CAM_1 frame frequency is: " << 1/elapsed_seconds.count() << std::endl;
end_1 = start_1;
}
//缓冲区初始化*/
memset(pRGB24Buf_1,0,pFrame->nWidth * pFrame->nHeight * 3 * sizeof(char)); //缓冲区初始化*/
DX_BAYER_CONVERT_TYPE cvtype = RAW2RGB_NEIGHBOUR; //选择插值算法
DX_PIXEL_COLOR_FILTER nBayerType = BAYERRG; //选择图像Bayer格式
bool bFlip = false;
VxInt32 DxStatus = DxRaw8toRGB24(const_cast<void *>(pFrame->pImgBuf),pRGB24Buf_1,pFrame->nWidth,pFrame->nHeight,cvtype,nBayerType,bFlip);
if (DxStatus != DX_OK)
{
return ;
}
cv::Mat image_rgb24(pFrame->nHeight, pFrame->nWidth, CV_8UC3);
memcpy(image_rgb24.data, pRGB24Buf_1, pFrame->nHeight * pFrame->nWidth * 3);
unsigned int frame_id = pFrame->nFrameID;
m1_buf.lock();
img1_buf.push(image_rgb24);
frame1_ID_buf.push(frame_id);
time1_buf.push(time_now);
m1_buf.unlock();
}
return ;
}
void cam0_process()
{
int cam0_num = 0;
while(1)
{
cv::Mat img_frame;
unsigned int id = 0;
double time = 0;
m0_buf.lock();
if(!img0_buf.empty())
{
std::cout << "the img0_buf size is: " << img0_buf.size() << std::endl;
img_frame = img0_buf.front().clone();
id = frame0_ID_buf.front();
time = time0_buf.front();
img0_buf.pop();
frame0_ID_buf.pop();
time0_buf.pop();
}
m0_buf.unlock();
if(img_frame.data)
{
cvtColor(img_frame, img_frame, CV_BGR2RGB);
//cv::imshow("img0:",img_frame);
std::string img0_path = "/home/h1/CODE/stereo_img_record/build/img0/" + std::to_string(cam0_num++) + ".jpg";
std::cout << img0_path << std::endl;
cv::imwrite(img0_path, img_frame);
//cv::imwrite("/home/h1/CODE/stereo_img_record/capture_image/2021.7.30.jpg", img_frame);
cv::waitKey(1);
}
else
{
return;
}
std::chrono::milliseconds dura(1);
std::this_thread::sleep_for(dura);
}
}
void cam1_process()
{
int cam1_num = 1;
while(1)
{
cv::Mat img_frame;
unsigned int id = 0;
double time = 0;
m1_buf.lock();
if(!img1_buf.empty())
{
//std::cout << "the img0_buf size is: " << img0_buf.size() << std::endl;
img_frame = img1_buf.front().clone();
id = frame1_ID_buf.front();
time = time1_buf.front();
img1_buf.pop();
frame1_ID_buf.pop();
time1_buf.pop();
}
m1_buf.unlock();
if(img_frame.data)
{
cvtColor(img_frame, img_frame, CV_BGR2RGB);
std::string img1_path = "/home/h1/CODE/stereo_img_record/build/img1/" + std::to_string(cam1_num++) + ".jpg";
std::cout << img1_path << std::endl;
cv::imwrite(img1_path, img_frame);
cv::imshow("img1:",img_frame);
}
else
{
return;
}
std::chrono::milliseconds dura(1);
std::this_thread::sleep_for(dura);
}
}
int main()
{
GX_STATUS status = Config();
if(status != GX_STATUS_SUCCESS)
{
std::cout << "config Camera Faile ..." << std::endl;
return 0;
}
camera_config cam0_info;
cam0_info.sn_str = "KE0200100061";
cam0_info.SN = &cam0_info.sn_str[0];
camera_config cam1_info;
cam1_info.sn_str = "KE0200120158";
cam1_info.SN = &cam1_info.sn_str[0];
MercureDriver* cam0 = new MercureDriver(cam0_info);
MercureDriver* cam1 = new MercureDriver(cam1_info);
cam0->InitCamera();
cam1->InitCamera();
if(cam0->status != GX_STATUS_SUCCESS || cam1->status != GX_STATUS_SUCCESS)
{
std::cout << "Initial Stereo Camera Faile ..." << std::endl;
return 0;
}
status = GXRegisterCaptureCallback(cam0->hDevice_, NULL, Frame_0_ProcessRGB);
status = GXSendCommand(cam0->hDevice_, GX_COMMAND_ACQUISITION_START);
if(status != GX_STATUS_SUCCESS)
{
std::cout << "Cam0 Start Read Faile ..." << std::endl;
return 0;
}
status = GXRegisterCaptureCallback(cam1->hDevice_, NULL, Frame_1_ProcessRGB);
status = GXSendCommand(cam1->hDevice_, GX_COMMAND_ACQUISITION_START);
if(status != GX_STATUS_SUCCESS)
{
std::cout << "Cam1 Start Read Faile ..." << std::endl;
return 0;
}
std::thread cam0_thread{
cam0_process};
std::thread cam1_thread{
cam1_process};
while(1)
{
std::chrono::milliseconds dura(5);
std::this_thread::sleep_for(dura);
}
return 0;
}
边栏推荐
- File Operations - IO
- postgis 数据表 迁移时错误解决方法
- What does the "busy" Polkadot latest effort mean for DOT investors?
- 学习残差神经网络(ResNet)
- 【RPC】Mercury RPC
- 教你六步拆解 DDD领域驱动设计落地实践
- 【微信小程序】一文学懂小程序的数据绑定和事件绑定
- Summary of digital IC design written test questions (4): some basic knowledge points
- 人机对话中得意图识别(关键词提取,svm工具)
- Runtime - KVC, KVO principle
猜你喜欢

torch.gather() usage interpretation

并查集按秩合并rank数组

学习残差神经网络(ResNet)

Query and track multiple express tracking numbers, and filter the tracking numbers shipped at a certain time

MySQL数据库

卷积神经网络 图像识别,卷积神经网络 图像处理

代码自动初始化

【笔记工具】

The tests that need to be done in the development of medical device products

Several postman features worth collecting will help you do more with less!
随机推荐
MySQL5
分布式系统设计之高可用大全
李沐老师 PyTorch版——线性回归 + softmax回归的简洁实现(3)
仿QQ好友列表,QListWidget!
APISIX Ingress v1.5-rc1 released
Educational Codeforces Round 133 (Rated for Div. 2) C Supplement
Why do big Internet companies keep hiring while frantically laying off staff?
stack-queue
神经网络参数量和计算量,神经网络是参数模型吗
快要“金九银十”了,你开始准备了吗?
Redis 的内存策略
Rust development - Struct usage example
四面拿下字节2-2Offer,入职就是...
【笔记工具】
from sklearn import cross_validation 报错的解决方法
【数学建模】微分方程求解 | dsolve函数 | ode45函数
Rust学习:4_基本类型
Maykle Studio - Deep Learning - BP Neural Network
ax.patches 表示什么?
PAT乙级-B1029 旧键盘(20)