qq_45846192
2021-11-08 11:05
采纳率: 100%
浏览 28

工业相机与opencv作图像实时处理,为什么处理后的帧率很低

#include "GxIAPI.h"  
#include "DxImageProc.h"  
#include <opencv2/core/core.hpp>  
#include <opencv2/imgproc/imgproc.hpp>  
#include <opencv2/highgui/highgui.hpp>  
#include <iostream>  

using namespace std;
using namespace cv;

GX_DEV_HANDLE       m_hDevice;              ///< 设备句柄  
BYTE                *m_pBufferRaw;          ///< 原始图像数据  
BYTE                *m_pBufferRGB;          ///< RGB图像数据,用于显示和保存bmp图像  
int64_t             m_nImageHeight;         ///< 原始图像高  
int64_t             m_nImageWidth;          ///< 原始图像宽  
int64_t             m_nPayLoadSize;
int64_t             m_nPixelColorFilter;    ///< Bayer格式  
Mat test;
//图像回调处理函数  
static void GX_STDC OnFrameCallbackFun(GX_FRAME_CALLBACK_PARAM* pFrame)
{
    //PrepareForShowImg();  
    if (pFrame->status == 0)
    {
        //对图像进行某些操作  
        /*memcpy(m_pBufferRaw, pFrame->pImgBuf, pFrame->nImgSize);
        // 黑白相机需要翻转数据后显示
        for (int i = 0; i <m_nImageHeight; i++)      {
        memcpy(m_pImageBuffer + i*m_nImageWidth, m_pBufferRaw + (m_nImageHeight - i - 1)*m_nImageWidth, (size_t)m_nImageWidth);
        }
        IplImage* src;
        src = cvCreateImage(cvSize(m_nImageWidth, m_nImageHeight), 8, 1);
        src->imageData = (char*)m_pImageBuffer;
        cvSaveImage("src.jpg", src);*/
        memcpy(m_pBufferRaw, pFrame->pImgBuf, pFrame->nImgSize);

        // RGB转换  
        DxRaw8toRGB24(m_pBufferRaw
            , m_pBufferRGB
            , (VxUint32)(m_nImageWidth)
            , (VxUint32)(m_nImageHeight)
            , RAW2RGB_NEIGHBOUR
            , DX_PIXEL_COLOR_FILTER(m_nPixelColorFilter)
            , false);
        //  cv_Image->imageData = (char*)m_pBufferRGB;  

        //  cvSaveImage("./test.bmp", cv_Image);  

        //test.data = m_pBufferRaw;  
        memcpy(test.data, m_pBufferRGB, m_nImageWidth*m_nImageHeight * 3);
        
        Mat grad_x, grad_y;
        Mat abs_grad_x, abs_grad_y, dst;

        //求x方向梯度
        Sobel(test, grad_x, CV_16S, 1, 0, 3, 1, 1, BORDER_DEFAULT);
        convertScaleAbs(grad_x, abs_grad_x);
        //imshow("x方向soble", abs_grad_x);

        //求y方向梯度
        Sobel(test, grad_y, CV_16S, 0, 1, 3, 1, 1, BORDER_DEFAULT);
        convertScaleAbs(grad_y, abs_grad_y);
        imshow("y向soble", abs_grad_y);

        //合并梯度
        addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, dst);
        //imshow("整体方向soble", dst);



        waitKey(15);

    }
    return;
}

int main(int argc, char* argv[])
{
    GX_STATUS emStatus = GX_STATUS_SUCCESS;
    GX_OPEN_PARAM openParam;
    uint32_t      nDeviceNum = 0;
    openParam.accessMode = GX_ACCESS_EXCLUSIVE;
    openParam.openMode = GX_OPEN_INDEX;
    openParam.pszContent = "1";
    // 初始化库   
    emStatus = GXInitLib();
    if (emStatus != GX_STATUS_SUCCESS)
    {
        return 0;
    }
    // 枚举设备列表  
    emStatus = GXUpdateDeviceList(&nDeviceNum, 1000);
    if ((emStatus != GX_STATUS_SUCCESS) || (nDeviceNum <= 0))
    {
        return 0;
    }
    //打开设备  
    emStatus = GXOpenDevice(&openParam, &m_hDevice);
    //设置采集模式连续采集  
    emStatus = GXSetEnum(m_hDevice, GX_ENUM_ACQUISITION_MODE, GX_ACQ_MODE_CONTINUOUS);
    emStatus = GXSetInt(m_hDevice, GX_INT_ACQUISITION_SPEED_LEVEL, 1);
    emStatus = GXSetEnum(m_hDevice, GX_ENUM_BALANCE_WHITE_AUTO, GX_BALANCE_WHITE_AUTO_CONTINUOUS);
    bool      bColorFliter = false;
    // 获取图像大小  
    emStatus = GXGetInt(m_hDevice, GX_INT_PAYLOAD_SIZE, &m_nPayLoadSize);
    // 获取宽度  
    emStatus = GXGetInt(m_hDevice, GX_INT_WIDTH, &m_nImageWidth);
    // 获取高度  
    emStatus = GXGetInt(m_hDevice, GX_INT_HEIGHT, &m_nImageHeight);
    test.create(m_nImageHeight, m_nImageWidth, CV_8UC3);
    //判断相机是否支持bayer格式  
    bool m_bColorFilter;
    emStatus = GXIsImplemented(m_hDevice, GX_ENUM_PIXEL_COLOR_FILTER, &m_bColorFilter);
    if (m_bColorFilter)
    {
        emStatus = GXGetEnum(m_hDevice, GX_ENUM_PIXEL_COLOR_FILTER, &m_nPixelColorFilter);
    }
    m_pBufferRGB = new BYTE[(size_t)(m_nImageWidth * m_nImageHeight * 3)];
    if (m_pBufferRGB == NULL)
    {
        return false;
    }
    //为存储原始图像数据申请空间  
    m_pBufferRaw = new BYTE[(size_t)m_nPayLoadSize];
    if (m_pBufferRaw == NULL)
    {
        delete[]m_pBufferRGB;
        m_pBufferRGB = NULL;
        return false;
    }
    //注册图像处理回调函数  
    emStatus = GXRegisterCaptureCallback(m_hDevice, NULL, OnFrameCallbackFun);
    //发送开采命令  
    emStatus = GXSendCommand(m_hDevice, GX_COMMAND_ACQUISITION_START);
    //---------------------  
    //  
    //在这个区间图像会通过OnFrameCallbackFun接口返给用户  
    Sleep(100000);
    //  
    //---------------------  
    //发送停采命令  
    emStatus = GXSendCommand(m_hDevice, GX_COMMAND_ACQUISITION_STOP);
    //注销采集回调  
    emStatus = GXUnregisterCaptureCallback(m_hDevice);
    if (m_pBufferRGB != NULL)
    {
        delete[]m_pBufferRGB;
        m_pBufferRGB = NULL;
    }
    if (m_pBufferRaw != NULL)
    {
        delete[]m_pBufferRaw;
        m_pBufferRaw = NULL;
    }
    emStatus = GXCloseDevice(m_hDevice);
    emStatus = GXCloseLib();
    return 0;
}
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • 九磅十五便士_ 2021-11-08 15:42
    已采纳
    1. 将工作模式从debug改为release会提高一部分效率
    2. 更常用的方式 我认为,你应该再创建一个线程。一个线程用来抓图,另一个线程用来对采集图像做一些处理。
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题