在Qt下使用映美精黑白相机:Qt 5.12 + ImagingSource(映美精)+ vs2017 Community + OpenCV 3.3

最近应做一个视觉项目需要用到映美精相机,在网上搜索了很多资料没有找到相关内容,因此只能自己一步一步的摸索。

 

一、准备工作

相机型号:ImagingSource DMK 23G445

相机软件:ic_setup_3.4.0,gigecam_setup_3.6.0

其它软件:Qt 5.12.1, vs2017 Community, OpenCV 3.3.0

操作系统:Windows 10 1909 18363.592

 

Qt .pro配置文件如下(配置文件xxx换上电脑用户名):

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = imagingSource_CallBack
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0


SOURCES += \
        main.cpp \
        mainwindow.cpp \
    Listener.cpp

HEADERS += \
        mainwindow.h \
    Listener.h

FORMS += \
        mainwindow.ui

INCLUDEPATH += D:\Projects\opencv\build\include
INCLUDEPATH += D:\Projects\opencv\build\include\opencv
INCLUDEPATH += D:\Projects\opencv\build\include\opencv2
INCLUDEPATH += $$quote(C:\Users\xxx\Documents\IC Imaging Control 3.4\classlib\include)


LIBS += D:\Projects\opencv\build\x64\vc14\lib\opencv_world330.lib
LIBS += D:\Projects\opencv\build\x64\vc14\lib\opencv_world330d.lib
LIBS += $$quote(C:\Users\xxx\Documents\IC Imaging Control 3.4\classlib\x64\release\TIS_UDSHL11_x64.lib)
LIBS += $$quote(C:\Users\xxx\Documents\IC Imaging Control 3.4\classlib\x64\debug\TIS_UDSHL11d_x64.lib)

Listener.h: 配置如下:

#ifndef LISTENER_H
#define LISTENER_H

// Listener.h: interface for the CListener class.
//
// The CListener class is derived from GrabberListener. It overwrites
// the "frameReady()" method. In the frameReady method the member method
// "saveImage()" is called.
// "saveImage()" saves the specified buffer to a BMP file and calls a "Sleep(250)"
// to simulate a time expensive image processing. "saveImage()" is also called
// by the main() function of this example to save all buffers, that have
// not been processed in the frameReady method.#
//
// This class also overwrites the overlayCallback method to draw a
// frame counter.
//
// The CListener object is registered to a Grabber with the parameter
// eFRAMEREADY|eOVERLAYCALLBACK .
//
// If your camera resolution is not 1280 × 960, you should modify the 
// macro definition "CAM_SIZE_X" and "CAM_SIZE_Y".
//
// You should change the "cmdhelper.h" path.
//

#pragma once

#include <QDebug>
#include <QLabel>
#include <QObject>

#include <tisudshl.h>
#include "../cmdhelper.h"	// TODO: You need change the path, the file path is usually as follows:
				// C:/Users/xxx/Documents/IC Imaging Control 3.4/samples/vc10/Common/cmdhelper.h

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

#define NUM_BUFFERS 2
#define CAM_SIZE_X 1280		// TODO: Maybe you need modify the value.
#define CAM_SIZE_Y 960		// TODO: Maybe you need modify the value.

using namespace cv;
using namespace DShowLib;

class CListener : public QObject, public DShowLib::GrabberListener
{
    Q_OBJECT

public:
    // Overwrite the GrabberListener methods we need
    virtual void overlayCallback( DShowLib::Grabber& caller, smart_ptr<DShowLib::OverlayBitmap> pBitmap, const DShowLib::tsMediaSampleDesc& MediaSampleDesc );
    virtual void frameReady( DShowLib::Grabber& caller, smart_ptr<DShowLib::MemBuffer> pBuffer, DWORD FrameNumber );
    virtual void deviceLost( Grabber& caller );

    // Save one image and mark it as saved
    //void saveImage( smart_ptr<DShowLib::MemBuffer> pBuffer, DWORD currFrame );
    // Setup the buffersize.
    void setBufferSize( unsigned long NumBuffers );

    std::vector<bool> m_BufferWritten;	// array of flags which buffers have been saved.
    cv::Mat srcImage_CListener;
    BYTE* pImage;

signals:
    void frameReady_Event(); 
    void deviceLost_Event();    // You can add the slot to process the device lost event.
};

#endif // LISTENER_H

Listener.cpp 配置如下

//
// Listener.cpp: implementation of the CListener class.
//

//#define _WIN32_WINNT 0x0500

#include <iostream>
#include "Listener.h"


//
/*! The overlayCallback() method draws the number of the current frame. The
    frame count is a member of the tsMediaSampleDesc structure that is passed
    to overlayCallback() by the Grabber.*/
void CListener::overlayCallback( Grabber& caller, smart_ptr<OverlayBitmap> pBitmap,
                                 const tsMediaSampleDesc& MediaSampleDesc)
{
    UNREFERENCED_PARAMETER(caller);
    //Grabber callertmp = caller;
    char szText[25];
    if( pBitmap->getEnable() == true ) // Draw only, if the overlay bitmap is enabled.
    {
        sprintf( szText,"%05d ", MediaSampleDesc.FrameNumber+1);
        pBitmap->drawText( RGB(255,255,255), 0, 0, szText );
    }
}


//
/*! The frameReady() method calls the saveImage method to save the image buffer to disk.*/
void CListener::frameReady( Grabber& caller, smart_ptr<MemBuffer> pBuffer, DWORD currFrame)
{
    UNREFERENCED_PARAMETER(caller);
    //Grabber callertmp = caller;
    //std::cout << "Buffer " << currFrame << " processed in CListener::frameReady()." << std::endl;

    //saveImage( pBuffer, currFrame ); // Do the buffer processing.
    //smart_ptr<BITMAPINFOHEADER> pInf = pBuffer->getBitmapInfoHeader();

    // Now retrieve a pointer to the image. For organization of the image data, please refer to:
    // http://www.imagingcontrol.com/ic/docs/html/class/Pixelformat.htm

    BYTE* pImageData = pBuffer->getPtr();
    pImage=pImageData;

    //Calculate the size of the image.
    //int iImageSize = pInf->biWidth * pInf->biHeight * pInf->biBitCount / 8 ;

    //将映美精相机的数据流转化为Mat类,进而进行后续的图像处理
    cv::Mat tempImage(CAM_SIZE_Y, CAM_SIZE_X, CV_8UC1, pImageData);
    srcImage_CListener=tempImage;
    frameReady_Event();         // TODO: You need add the slot the process the signal.

    //saveToFileBMP( *pBuffer, "C:/Users/xxx/Desktop/pic/001.bmp" );
    //::Sleep(250); // Simulate a time expensive processing.

}

void CListener::deviceLost(Grabber &caller)
{
    UNREFERENCED_PARAMETER(caller);
    emit deviceLost_Event();    // TODO: You need add the slot the process the signal.
}

//
/*! Initialize the array of bools that is used to memorize, which buffers were processed in
    the frameReady() method. The size of the array is specified by the parameter NumBuffers.
    It should be equal to the number of buffers in the FrameHandlerSink.
    All members of m_BufferWritten are initialized to false.
    This means that no buffers have been processed.*/
void CListener::setBufferSize( unsigned long NumBuffers )
{
    m_BufferWritten.resize( NumBuffers, false );
}

//
/*! The image passed by the MemBuffer pointer is saved to a BMP file.*/
//void CListener::saveImage( smart_ptr<MemBuffer> pBuffer, DWORD currFrame)
//{
//    char filename[MAX_PATH];
//    if( currFrame < m_BufferWritten.size() )
//    {
//        sprintf( filename, "image%02i.bmp", currFrame );

//        saveToFileBMP( *pBuffer, filename );

//        m_BufferWritten.at( currFrame ) = true;
//    }
//}

在Qt项目中只要添加上面两个文件就可以啦,同时在要开始检测的地方要加入

addListener(pListener, GrabberListener::eALL); // 用法参见映美精参考文档

这样每次相机内存中有图片时,就能通过下面信号函数发送消息,在要处理图像的源文件中添加对应的槽就可以了。

frameReady_Event();

如果采用软触发建议使用 snapImage() 函数取图。

 

 

 

 

 

 

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值