sr4000自带API和opencv结合获取图像

/*
 * =====================================================================================
 *
 *       Filename:  main.cpp
 *      Environment:    
 *    Description:  SR4K的API使用(libMesaSR.dll)
 *
 *
 *        Version:  1.0
 *        Created:  2013/10/30 20:47:31
 *         Author:  yuliyang
I*
 *             Mail:  wzyuliyang911@gmail.com
 *             Blog:  http://www.cnblogs.com/yuliyang
 *
 * =====================================================================================
 */

#include "definesSR.h"
#include "libMesaSR.h"
#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"  
#include "opencv2/highgui/highgui.hpp"  
#include "opencv2/imgproc/imgproc.hpp"  
using namespace std;


/*------------------------------------------------------------------------------------------------------------
 *
 *  从内存中dump出内容
 *  共144*176*sizeof(unsigned short int类型)个字节
 *
 *------------------------------------------------------------------------------------------------------------*/
//void Dump(unsigned char* p,int num)
//{
//    puts("Dump of memory byte by byte regrouped in blocks of 8 byte
"
//        "   bytes:  0 1 2 3 4 5 6 7| 8 9 a b c d e f|1011121314151617|18191a1b1c1d1e1f|
"
//        "--------:-----------------+----------------+----------------+----------------|");
//    for(int i=0;i<num;i++)
//    {
//        if(!(i&0x1f))
//        {
//            printf("%08x: ",i);
//        }
//        printf("%02x",p[i]);
//
//        if(!((i+1)&0x07))
//        {
//            putchar('|');
//            if(!((i+1)&0x1f))
//            {
//                putchar('
');
//            }
//        }
//    }
//    putchar('
');
//}
int main(){

    /*------------------------------------------------------------------------------------------------------------
     *  创建一个SR4K设备对象                                                                                                  
     *------------------------------------------------------------------------------------------------------------*/
    CMesaDevice *cam=NULL;
    
    SR_OpenDlg(&cam,1,NULL);                    

    /*------------------------------------------------------------------------------------------------------------
     *  设置工作模式在15MHZ                                                                                                  
     *------------------------------------------------------------------------------------------------------------*/
    SR_SetModulationFrequency(cam,MF_15MHz);
    SR_SetMode(cam,AM_COR_FIX_PTRN|AM_CONV_GRAY|AM_DENOISE_ANF); /* 其他一些属性的设置:去噪等 */

    /*------------------------------------------------------------------------------------------------------------
     *  获取行数和列数                                                                                                  
     *------------------------------------------------------------------------------------------------------------*/
    int r= SR_GetRows(cam);
    int c= SR_GetCols(cam);

    size_t bufSz=r*c*3*sizeof(unsigned int);    /* 创建缓存 3种图像,depth图,Amplitude幅度图,和确信度图 */
    void *buf=malloc(bufSz);  
    memset(buf,0xaf,bufSz);                     /* 缓存区清零 */
    /*------------------------------------------------------------------------------------------------------------
     *  以下用于坐标变换,转到笛卡尔坐标系                                                                                                  
     *------------------------------------------------------------------------------------------------------------*/
    short *xS16=(short*)buf, *yS16=&xS16[r*c]; WORD *zU16=(WORD*)&yS16[r*c];
    int pitchS16X=sizeof(short),  pitchS16Y=sizeof(short), pitchU16Z=sizeof(WORD);
    SR_Acquire(cam);
    //    SR_CoordTrfUint16(cam, 0,    0,    zU16, pitchS16X, pitchS16Y, pitchU16Z);    
    SR_CoordTrfUint16(cam, xS16, yS16, zU16, pitchS16X, pitchS16Y, pitchU16Z);    

    int num=SR_Acquire(cam);                    /* 触发设备获取图像 */
    ImgEntry  *images;
    int numImg=SR_GetImageList(cam,&images);    /* 获得图像列表 */
    cout<<numImg<<endl;
    void * data;                                /* 指向图像数据部分的指针 */
    
    data=SR_GetImage(cam, 0);
    /*------------------------------------------------------------------------------------------------------------
     *  用opencv显示图像                                                                                                  
     *------------------------------------------------------------------------------------------------------------*/
    IplImage *temp=cvCreateImage(cvSize(176,144),IPL_DEPTH_16U,1);
    unsigned short int * pdata;
    pdata=(unsigned short int*)data;
    for (int i=0;i<temp->height;i++)
    {
        for (int j=0;j<temp->width;j++)
        {
            CV_IMAGE_ELEM( temp, unsigned short int, i, j)=*pdata; /* 给图像赋值 */
            pdata++;

        }
    }
    
    /*------------------------------------------------------------------------------------------------------------
     *  显示图像                                                                                                  
     *------------------------------------------------------------------------------------------------------------*/
    cvShowImage("1",temp);
    cvWaitKey(0);
    cvDestroyWindow("1");
    cvReleaseImage(&temp);

    /*for(int i=0;i<numImg;i++)
    {
         data=SR_GetImage(cam, i);
         Dump((unsigned char*)data,(int)r*c*2);
    }*/
    /*WORD w1=images->width;
    WORD h1=images->height;
    cout<<images->dataType<<endl;
    cout<<images->imgType<<endl;
    
    cout<<w1<<endl;
    cout<<h1<<endl;*/
    //SR_GetImage(cam,0);
    //SR_StreamToFile(cam,"11.srs",0);          /* 导出srs格式文件 */
    //SR_StreamToFile(cam,"11.srs",2);
    //SR_CoordTrfFlt(cam, x, y, z, sizeof(float) , sizeof(float) , sizeof(float));

    /*-----------------------------------------------------------------------------
     *  释放内存
     *-----------------------------------------------------------------------------*/
    SR_Close(cam) ;
    free(buf); //free allocated buffers
    
    return 0;
}
View Code

测试结果:

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/yuliyang/p/3407369.html