1. 程式人生 > >映美精相機的資料流轉成MAT

映美精相機的資料流轉成MAT

       由於專案需要,採用映美精的工業相機,利用其影象進行實時採集,並且將影象資料匯入成Opencv的Mat影象格式,進行進一步處理。

       這個過程涉及到三個方面:

  1. 映美精相機的採集,
  2. 採集得到的影象與Mat格式的轉換,
  3. Mat格式如何在影象控制元件中顯示

       首先在其官方網站下載相機的驅動以及其開發工具包IC Imaging Control .NET Component, C++ Class Library。下載後安裝好,我們可以看到其例子,其中有classlib資料夾,裡面的檔案就是我們需要使用的檔案。然後我們就是根據其例程進行修改。

#include "opencv2/opencv.hpp"

#include "tisudshl.h"

#include "CmdHelper.h"//這兩個標頭檔案是需要包含的,這樣才能進行檔案的生成

using namespace cv;

using namespace _DSHOWLIB_NAMESPACE;

int main()

{

    Mat capframe; //得到的影象

    IplImage* rawImage; //舊版的影象

    Grabber grabber;

    tFrameHandlerSinkPtr pSink;

    BYTE* pBuf[1];

    FrameTypeInfo info;

    tMemBufferCollectionPtr pCollection;

    int Height;

    int Width;

    // 影象的型別

    int captype;

    // 影象的通道

    int capchannels;

    //以下就是相機的初始化

    DShowLib::InitLibrary();

    BOOL CameraExist=PathFileExists(L"lastSelectedDeviceState.xml");

    if (!CameraExist)

    {

        setupDeviceFromFile(grabber);

    }

    if (grabber.getAvailableVideoCaptureDevices()==0)

    {

        return false;

    }



    grabber.loadDeviceStateFromFile("lastSelectedDeviceState.xml");

    //    grabber.saveDeviceStateToFile("lastSelectedDeviceState.xml");

    grabber.setOverlayBitmapPathPosition( ePP_NONE );

    pSink = FrameHandlerSink::create( eY800, 1 );



    pSink->setSnapMode( true );



    // Set the sink.

    grabber.setSinkType( pSink );    



    // Prepare the live mode, to get the output size if the sink.

    if( !grabber.prepareLive( false ) )

    {

        std::cerr << "Could not render the VideoFormat into a eY800 sink.";

        return false;

    }

    pSink->getOutputFrameType( info );

    pBuf[0] = new BYTE[info.buffersize];

    pCollection = MemBufferCollection::create( info, 1, pBuf );

    if( pCollection == 0 || !pSink->setMemBufferCollection( pCollection ) )

    {

        std::cerr << "Could not set the new MemBufferCollection, because types do not match.";

        return false;

    }

    grabber.startLive( false );

    rawImage = cvCreateImage(cvSize(2592,1944),IPL_DEPTH_8U,1);

    rawImage->widthStep=2592;

    rawImage->imageData=(char*)pBuf[0];

    capframe.create(1944,2592,CV_8U);

    Height=2592;

    Width=1944;

    captype=CV_8U;

    capchannels=1;

    //初始化完畢,下面開始獲取一副影象

    pSink->snapImages(1);

    capframe.data=(uchar*)rawImage->imageData;

    //Mat::Mat(rawImage).copyTo(capframe);

    return 1;

}

原文地址:http://www.xncae.com/html/120.html