我是靠谱客的博主 俏皮心锁,最近开发中收集的这篇文章主要介绍linux下使用opencv读取大恒相机,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

前言

大恒网口相机真的有点坑,api很难用,还好技术支持比较有耐心,一点一点的帮着解决了问题。

驱动安装

首先去大恒官网去下载linux下面的驱动,除了C++版本,还有python版本。一定要注意:linux下面安装驱动时,要看自己的内核版本支不支持相机的驱动,这一点很坑,要问清楚,到后面,也是费了很大劲才更换的内核。
在这里插入图片描述
在这里插入图片描述这是我这个相机驱动支持的linux内核,然后,安装驱动即可。
在这里插入图片描述
安装完驱动后,在tools文件夹中,有一个可执行文件,可以用来监测网口,运行即可。除此之外,还有一个可视化界面GalaxyView。
在这里插入图片描述

搭建工程

编译完成的文件夹有一些例子,拿下图第一个例子为例,在QT中建立工程。
在这里插入图片描述在QT中配置好路径,包括头文件和库文件。
在这里插入图片描述

INCLUDEPATH += /home/oliver/software/dhcam_install_20181107/dh_camera/daheng-sdk-x64/sdk/include

LIBS += /home/oliver/software/dhcam_install_20181107/dh_camera/daheng-sdk-x64/sdk/lib/libdximageproc.so.1.0.1602.8161
        /home/oliver/software/dhcam_install_20181107/dh_camera/daheng-sdk-x64/sdk/lib/libgxgvtl.so.1.0.1802.8051
        /home/oliver/software/dhcam_install_20181107/dh_camera/daheng-sdk-x64/sdk/lib/libgxiapi.so.1.0.1810.8121
        /home/oliver/software/dhcam_install_20181107/dh_camera/daheng-sdk-x64/sdk/lib/libgxu3vtl.so.1.0.1802.8051

INCLUDEPATH += /usr/local/include
/usr/local/include/opencv
/usr/local/include/opencv2


LIBS += /usr/local/lib/libopencv_*.so

对源文件进行修改,修改的内容主要在void ProcGetImage(void pParam)函数中,还有定义char* m_rgb_image=NULL,下面有标注。

//-------------------------------------------------------------
/**
file      GxAcquireContinuous.cpp
brief     sample to show how to acquire image continuously. 
version   1.0.1605.9041
date      2016-05-04
*/
//-------------------------------------------------------------

#include "GxIAPI.h"
#include"DxImageProc.h"
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <iostream>
#include<sstream>
#include<fstream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;
using namespace cv::dnn;

#define MEMORY_ALLOT_ERROR -1 
char* m_rgb_image=NULL; //增加的内容

GX_DEV_HANDLE g_device = NULL;              ///< 设备句柄
GX_FRAME_DATA g_frame_data = { 0 };         ///< 采集图像参数
pthread_t g_acquire_thread = 0;             ///< 采集线程ID
bool g_get_image = false;                   ///< 采集线程是否结束的标志:true 运行;false 退出

//获取图像大小并申请图像数据空间
int PreForImage();

//释放资源
int UnPreForImage();

//采集线程函数
void *ProcGetImage(void* param);

//获取错误信息描述
void GetErrorString(GX_STATUS error_status);

int main()
{
    uid_t user = 0;
    user = geteuid();
    if(user != 0)
    {
        printf("n");  
        printf("Please run this application with 'sudo -E ./GxAcquireContinuous' or"
                              " Start with root !n");
        printf("n");
        return 0;
    }

    printf("n");
    printf("-------------------------------------------------------------n");
    printf("sample to show how to acquire image continuously.n");
    #ifdef __x86_64__
    printf("version: 1.0.1605.8041n");
    #elif __i386__
    printf("version: 1.0.1605.9041n");
    #endif
    printf("-------------------------------------------------------------n");
    printf("n");

    printf("Press [x] or [X] and then press [Enter] to Exit the Programn");
    printf("Initializing......"); 
    printf("nn");
    
    
    usleep(2000000);   
     
    //API接口函数返回值 
    GX_STATUS status = GX_STATUS_SUCCESS;

    uint32_t device_num = 0;
    uint32_t ret = 0;
    GX_OPEN_PARAM open_param;

    //初始化设备打开参数,默认打开序号为1的设备
    open_param.accessMode = GX_ACCESS_EXCLUSIVE;
    open_param.openMode = GX_OPEN_INDEX;
    open_param.pszContent = "1";

    //初始化库
    status = GXInitLib(); 
    if(status != GX_STATUS_SUCCESS)
    {
        GetErrorString(status);
        return 0;
    }

    //获取枚举设备个数
    status = GXUpdateDeviceList(&device_num, 1000);
    if(status != GX_STATUS_SUCCESS)
    { 
        GetErrorString(status);
        status = GXCloseLib();
        return 0;
    }

    if(device_num <= 0)
    {
        printf("<No device>n");
        status = GXCloseLib();
        return 0;
    }
    else
    {
        //默认打开第1个设备
        status = GXOpenDevice(&open_param, &g_device);
        if(status == GX_STATUS_SUCCESS)
	{
            printf("<Open device success>n");
        }
        else
        {
            printf("<Open device fail>n");
            status = GXCloseLib();
            return 0;			
        }
    }

    //设置采集模式为连续采集
    status = GXSetEnum(g_device, GX_ENUM_ACQUISITION_MODE, GX_ACQ_MODE_CONTINUOUS);
    if(status != GX_STATUS_SUCCESS)
    {
        GetErrorString(status);
        status = GXCloseDevice(g_device);
        if(g_device != NULL)
        {
            g_device = NULL;
        }
        status = GXCloseLib();
        return 0;
    }

    //设置触发开关为OFF
    status = GXSetEnum(g_device, GX_ENUM_TRIGGER_MODE, GX_TRIGGER_MODE_OFF);
    if(status != GX_STATUS_SUCCESS)
    {
        GetErrorString(status);
        status = GXCloseDevice(g_device);
        if(g_device != NULL)
        {
            g_device = NULL;
        }
        status = GXCloseLib();
        return 0;
    }
	
    //为采集做准备    
    ret = PreForImage();
    if(ret != 0)    
    {
        printf("<Failed to prepare for acquire image>n");
        status = GXCloseDevice(g_device);
        if(g_device != NULL)
        {
            g_device = NULL;
        }
        status = GXCloseLib();
        return 0;
    }

    //启动接收线程
    ret = pthread_create(&g_acquire_thread, 0, ProcGetImage, 0);
    if(ret != 0)
    {
        printf("<Failed to create the collection thread>n");
        status = GXCloseDevice(g_device);
        if(g_device != NULL)
        {
            g_device = NULL;
        }
        status = GXCloseLib();
        return 0;
    }

    bool run = true;
    while(run == true)
    {
        int c = getchar();

        switch(c)
        {
            //退出程序
            case 'X': 
            case 'x':
                run = false;
                break;
            default:
                break;
        }	
    }

    //为停止采集做准备
    ret = UnPreForImage();
    if(ret != 0)
    {
        status = GXCloseDevice(g_device);
        if(g_device != NULL)
        {
            g_device = NULL;
        }
        status = GXCloseLib();
        return 0;
    }

    //关闭设备
    status = GXCloseDevice(g_device);
    if(status != GX_STATUS_SUCCESS)
    {
        GetErrorString(status);
        if(g_device != NULL)
        {
            g_device = NULL;
        }
        status = GXCloseLib();
        return 0;
    }

    //释放库
    status = GXCloseLib();
    return 0;
}

//-------------------------------------------------
/**
brief 获取图像大小并申请图像数据空间
return void
*/
//-------------------------------------------------
int PreForImage()
{
    GX_STATUS status = GX_STATUS_SUCCESS;
    int64_t payload_size = 0;
	
    status = GXGetInt(g_device, GX_INT_PAYLOAD_SIZE, &payload_size);
    if(status != GX_STATUS_SUCCESS)
    {
        GetErrorString(status);
        return status;
    }
	
    g_frame_data.pImgBuf = malloc(payload_size);
    if(g_frame_data.pImgBuf == NULL)
    {
        printf("<Failed to allot memory>n");
        return MEMORY_ALLOT_ERROR;
    }
 
    return 0;
}

//-------------------------------------------------
/**
brief 释放资源
return void
*/
//-------------------------------------------------
int UnPreForImage()
{
    GX_STATUS status = GX_STATUS_SUCCESS;
    uint32_t ret = 0;
   
    //发送停采命令
    status = GXSendCommand(g_device, GX_COMMAND_ACQUISITION_STOP);
    if(status != GX_STATUS_SUCCESS)
    {
        GetErrorString(status);
        return status;
    }

    g_get_image = false;
    ret = pthread_join(g_acquire_thread,NULL);
    if(ret != 0)
    {
        printf("<Failed to release resources>n");
        return ret;
    }
	

    //释放buffer
    if(g_frame_data.pImgBuf != NULL)
    {
        free(g_frame_data.pImgBuf);
        g_frame_data.pImgBuf = NULL;
    }

    return 0;
}

//-------------------------------------------------
/**
brief 采集线程函数
param pParam 线程传入参数
return void*
*/
//-------------------------------------------------
void *ProcGetImage(void* pParam)
{
    GX_STATUS status = GX_STATUS_SUCCESS;

    //接收线程启动标志
    g_get_image = true;

    //发送开采命令
    status = GXSendCommand(g_device, GX_COMMAND_ACQUISITION_START);
    if(status != GX_STATUS_SUCCESS)
    {
        GetErrorString(status);
    }
	
    while(g_get_image)
    {
        if(g_frame_data.pImgBuf == NULL)
        {
            continue;
        }

        status = GXGetImage(g_device, &g_frame_data, 100);
        if(status == GX_STATUS_SUCCESS)
        {
            if(g_frame_data.nStatus == 0)
            {
               //增加的内容
                Mat src;
                m_rgb_image=new char[g_frame_data.nWidth*g_frame_data.nHeight*3];

                src.create(g_frame_data.nHeight,g_frame_data.nWidth,CV_8UC3);

                memcpy(src.data,g_frame_data.pImgBuf,g_frame_data.nWidth*g_frame_data.nHeight);

                DxRaw8toRGB24(g_frame_data.pImgBuf,m_rgb_image,g_frame_data.nWidth, g_frame_data.nHeight,RAW2RGB_NEIGHBOUR,DX_PIXEL_COLOR_FILTER(BAYERBG),false);

                memcpy(src.data,m_rgb_image,g_frame_data.nWidth*g_frame_data.nHeight*3);
                namedWindow("test",0);
                imshow("test",src);
                waitKey(10); 
                //到这里增加结束
                printf("<Successful acquisition : Width: %d Height: %d >n", g_frame_data.nWidth, g_frame_data.nHeight);
            }
        }
    }
}

//----------------------------------------------------------------------------------
/**
brief  获取错误信息描述
param  emErrorStatus  错误码

return void
*/
//----------------------------------------------------------------------------------
void GetErrorString(GX_STATUS error_status)
{
    char *error_info = NULL;
    size_t size = 0;
    GX_STATUS status = GX_STATUS_SUCCESS;
	
    // 获取错误描述信息长度
    status = GXGetLastError(&error_status, NULL, &size);
    if(status != GX_STATUS_SUCCESS)
    {
           GetErrorString(status);
	   return;
    }
	
    error_info = new char[size];
    if (error_info == NULL)
    {
        printf("<Failed to allocate memory>n");
        return ;
    }
	
    // 获取错误信息描述
    status = GXGetLastError(&error_status, error_info, &size);
    if (status != GX_STATUS_SUCCESS)
    {
        printf("<GXGetLastError call fail>n");
    }
    else
    {
        printf("%sn", (char*)error_info);
    }

    // 释放资源
    if (error_info != NULL)
    {
        delete []error_info;
        error_info = NULL;
    }
}

显示结果

在这里插入图片描述在这里插入图片描述

最后

以上就是俏皮心锁为你收集整理的linux下使用opencv读取大恒相机的全部内容,希望文章能够帮你解决linux下使用opencv读取大恒相机所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(61)

评论列表共有 0 条评论

立即
投稿
返回
顶部