大恒工业相机搭建双目相机(软件)

时间:2025-03-13 14:41:03
#include <vector> #include <string> #include <iostream> #include <fstream> #include <queue> #include <thread> #include <mutex> #include <> #include "opencv2/" #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.", 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; }