FlyCapture2 fc2Image OpenCV IplImage Conversion 两种图像格式之间的转换

fc2Image是FlyCapture SDK的C语言库中的图片格式,由于在Windows上的MinGW无法编译FlyCapture2的C++库,只能使用C语言库,所以当我们在同时使用OpenCV的图像格式IplImage时,有时候就需要两种格式相互转换。如果需要FlyCapture2 Image和OpenCV IplImage之间的转换,可以参见我之前的博客OpenCV IplImage FlyCapture2 Image Conversion 两种图像类的相互转化。我们先来分别看看两种图像格式的定义:

IplImage:

typedef struct _IplImage
{
    int  nSize;             /* sizeof(IplImage) */
    int  ID;                /* version (=0)*/
    int  nChannels;         /* Most of OpenCV functions support 1,2,3 or 4 channels */
    int  alphaChannel;      /* Ignored by OpenCV */
    int  depth;             /* Pixel depth in bits: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16S,
                               IPL_DEPTH_32S, IPL_DEPTH_32F and IPL_DEPTH_64F are supported.  */
    char colorModel[4];     /* Ignored by OpenCV */
    char channelSeq[4];     /* ditto */
    int  dataOrder;         /* 0 - interleaved color channels, 1 - separate color channels.
                               cvCreateImage can only create interleaved images */
    int  origin;            /* 0 - top-left origin,
                               1 - bottom-left origin (Windows bitmaps style).  */
    int  align;             /* Alignment of image rows (4 or 8).
                               OpenCV ignores it and uses widthStep instead.    */
    int  width;             /* Image width in pixels.                           */
    int  height;            /* Image height in pixels.                          */
    struct _IplROI *roi;    /* Image ROI. If NULL, the whole image is selected. */
    struct _IplImage *maskROI;      /* Must be NULL. */
    void  *imageId;                 /* "           " */
    struct _IplTileInfo *tileInfo;  /* "           " */
    int  imageSize;         /* Image data size in bytes
                               (==image->height*image->widthStep
                               in case of interleaved data)*/
    char *imageData;        /* Pointer to aligned image data.         */
    int  widthStep;         /* Size of aligned image row in bytes.    */
    int  BorderMode[4];     /* Ignored by OpenCV.                     */
    int  BorderConst[4];    /* Ditto.                                 */
    char *imageDataOrigin;  /* Pointer to very origin of image data
                               (not necessarily aligned) -
                               needed for correct deallocation */
}
IplImage;

fc2Image:

typedef struct _fc2Image
{
    unsigned int rows;
    unsigned int cols;
    unsigned int stride;
    unsigned char* pData;
    unsigned int dataSize;
    unsigned int receivedDataSize;
    fc2PixelFormat format;
    fc2BayerTileFormat bayerFormat;

    fc2ImageImpl imageImpl;

} fc2Image;

下面我们来进行相互转换:

1. FlyCapture2 fc2Image OpenCV IplImage

/**
 * Convert FlyCapture2 fc2Image to OpenCV IplImage type
 */
IplImage* ConvertFC2ImageToOpenCV(fc2Image* pImage)
{
    IplImage* cvImage = NULL;
    bool bColor = true;
    CvSize mySize;
    mySize.height = pImage->rows;
    mySize.width = pImage->cols;
    bool bInitialized = false;
    fc2Image colorImage;
    fc2Error error;
    switch ( pImage->format )
    {
    case FC2_PIXEL_FORMAT_MONO8:     cvImage = cvCreateImageHeader(mySize, 8, 1 );
        cvImage->depth = IPL_DEPTH_8U;
        cvImage->nChannels = 1;
        bColor = false;
        break;
    case FC2_PIXEL_FORMAT_411YUV8:   cvImage = cvCreateImageHeader(mySize, 8, 3 );
        cvImage->depth = IPL_DEPTH_8U;
        cvImage->nChannels = 3;
        break;
    case FC2_PIXEL_FORMAT_422YUV8:   cvImage = cvCreateImageHeader(mySize, 8, 3 );
        cvImage->depth = IPL_DEPTH_8U;
        cvImage->nChannels = 3;
        break;
    case FC2_PIXEL_FORMAT_444YUV8:   cvImage = cvCreateImageHeader(mySize, 8, 3 );
        cvImage->depth = IPL_DEPTH_8U;
        cvImage->nChannels = 3;
        break;
    case FC2_PIXEL_FORMAT_RGB8:      cvImage = cvCreateImageHeader(mySize, 8, 3 );
        cvImage->depth = IPL_DEPTH_8U;
        cvImage->nChannels = 3;
        break;
    case FC2_PIXEL_FORMAT_MONO16:    cvImage = cvCreateImageHeader(mySize, 16, 1 );
        cvImage->depth = IPL_DEPTH_16U;
        cvImage->nChannels = 1;
        bColor = false;
        break;
    case FC2_PIXEL_FORMAT_RGB16:     cvImage = cvCreateImageHeader(mySize, 16, 3 );
        cvImage->depth = IPL_DEPTH_16U;
        cvImage->nChannels = 3;
        break;
    case FC2_PIXEL_FORMAT_S_MONO16:  cvImage = cvCreateImageHeader(mySize, 16, 1 );
        cvImage->depth = IPL_DEPTH_16U;
        cvImage->nChannels = 1;
        bColor = false;
        break;
    case FC2_PIXEL_FORMAT_S_RGB16:   cvImage = cvCreateImageHeader(mySize, 16, 3 );
        cvImage->depth = IPL_DEPTH_16U;
        cvImage->nChannels = 3;
        break;
    case FC2_PIXEL_FORMAT_RAW8:      cvImage = cvCreateImageHeader(mySize, 8, 3 );
        cvImage->depth = IPL_DEPTH_8U;
        cvImage->nChannels = 3;
        break;
    case FC2_PIXEL_FORMAT_RAW16:     cvImage = cvCreateImageHeader(mySize, 8, 3 );
        cvImage->depth = IPL_DEPTH_8U;
        cvImage->nChannels = 3;
        break;
    case FC2_PIXEL_FORMAT_MONO12:    printf("Not supported by OpenCV");
        bColor = false;
        break;
    case FC2_PIXEL_FORMAT_RAW12:     printf("Not supported by OpenCV");
        break;
    case FC2_PIXEL_FORMAT_BGR:       cvImage = cvCreateImageHeader(mySize, 8, 3 );
        cvImage->depth = IPL_DEPTH_8U;
        cvImage->nChannels = 3;
        break;
    case FC2_PIXEL_FORMAT_BGRU:      cvImage = cvCreateImageHeader(mySize, 8, 4 );
        cvImage->depth = IPL_DEPTH_8U;
        cvImage->nChannels = 4;
        break;
    case FC2_PIXEL_FORMAT_RGBU:      cvImage = cvCreateImageHeader(mySize, 8, 4 );
        cvImage->depth = IPL_DEPTH_8U;
        cvImage->nChannels = 4;
        break;
    default: printf("Some error occured...\n");
        return NULL;
    }

    if(bColor) {
        error = fc2CreateImage( &colorImage );
        if ( error != FC2_ERROR_OK )
        {
            printf( "Error in fc2CreateImage: %d\n", error );
        }
        if(!bInitialized)
        {
            fc2SetImageData(&colorImage, new unsigned char[pImage->cols * pImage->rows*3], pImage->cols * pImage->rows*3);
            bInitialized = true;
        }
        fc2ConvertImageTo(FC2_PIXEL_FORMAT_BGR, pImage, &colorImage); //needs to be as BGR to be saved
        if ( error != FC2_ERROR_OK )
        {
            printf( "Error in fc2ConvertImageTo: %d\n", error );
        }
        cvImage->width = colorImage.cols;
        cvImage->height = colorImage.rows;
        cvImage->widthStep = colorImage.stride;
        cvImage->origin = 0; //interleaved color channels
        cvImage->imageDataOrigin = (char*)colorImage.pData; //DataOrigin and Data same pointer, no ROI
        cvImage->imageData = (char*)(colorImage.pData);
        cvImage->widthStep = colorImage.stride;
        cvImage->nSize = sizeof (IplImage);
        cvImage->imageSize = cvImage->height * cvImage->widthStep;
    }
    else
    {
        cvImage->imageDataOrigin   = (char*)(pImage->pData);
        cvImage->imageData         = (char*)(pImage->pData);
        cvImage->widthStep         = pImage->stride;
        cvImage->nSize             = sizeof (IplImage);
        cvImage->imageSize         = cvImage->height * cvImage->widthStep;
    }
    return cvImage;
}

2. OpenCV IplImage to FlyCapture2 fc2Image

/**
 * Convert OpenCV IplImage to FlyCapture2 fc2Image type
 */
fc2Image* ConvertIplImageToFC2Image(IplImage* cvImage)
{
    fc2Image* pImage;
    fc2Error error;
    fc2BayerTileFormat btf = FC2_BT_GBRG;
    error = fc2CreateImage(pImage);
    if ( error != FC2_ERROR_OK )
    {
        printf( "Error in fc2CreateImage: %d\n", error );
    }
    if (cvImage->depth == IPL_DEPTH_8U) {
        if (cvImage->nChannels == 1) error = fc2SetImageDimensions(pImage, cvImage->height, cvImage->width, cvImage->widthStep, FC2_PIXEL_FORMAT_MONO8, btf);
        if (cvImage->nChannels == 3) error = fc2SetImageDimensions(pImage, cvImage->height, cvImage->width, cvImage->widthStep, FC2_PIXEL_FORMAT_RGB8, btf);
        if (cvImage->nChannels == 4) error = fc2SetImageDimensions(pImage, cvImage->height, cvImage->width, cvImage->widthStep, FC2_PIXEL_FORMAT_BGRU, btf);
    } else if (cvImage->depth == IPL_DEPTH_16U) {
        if (cvImage->nChannels == 1) error = fc2SetImageDimensions(pImage, cvImage->height, cvImage->width, cvImage->widthStep, FC2_PIXEL_FORMAT_MONO16, btf);
        if (cvImage->nChannels == 3) error = fc2SetImageDimensions(pImage, cvImage->height, cvImage->width, cvImage->widthStep, FC2_PIXEL_FORMAT_RGB16, btf);
    } else error = fc2SetImageDimensions(pImage, cvImage->height, cvImage->width, cvImage->widthStep, FC2_UNSPECIFIED_PIXEL_FORMAT, btf);
    if ( error != FC2_ERROR_OK )
    {
        printf( "Error in fc2SetImageDimensions: %d\n", error );
    }
    fc2SetImageData(pImage, (const unsigned char*)cvImage->imageData, cvImage->imageSize);
    if (error != FC2_ERROR_OK)
    {
        printf( "Error in fc2SetImageData: %d\n", error );
    }
    return pImage;
}
时间: 2024-10-05 00:46:21

FlyCapture2 fc2Image OpenCV IplImage Conversion 两种图像格式之间的转换的相关文章

在linux环境下编译运行OpenCV程序的两种方法

原来以为在Ubuntu下安装好了OpenCV之后,自己写个简单的程序应该很容易吧,但是呢,就是为了编译一个简单的显示图片的程序我都快被弄崩溃了. 在谷歌和上StackOverFlow查看相关问题解答之后,我下面就介绍Command Line和CMake两种方式. 首先我先粘上我测试的代码吧,文件名为Test.c 1 #include <highgui.h> 2 3 int main(int argc,char ** argv) { 4 5 IplImage* img = cvLoadImage

SELinux 宽容模式(permissive) 强制模式(enforcing) 关闭(disabled) 几种模式之间的转换

在CentOS6.2 中安装intel 的c++和fortran 的编译器时,遇到来一个关于SELinux的强制模式不可执行的情况, 需要关闭SELinux 或者 将enforcing改为 permissive 模式,查询来一些资料后,先对SELinux的几种模式,以及其之间的关系和转换方法做一小结,以备以后查看和学习. SELinux 的启动.关闭与查看 1.并非所有的 Linux distributions 都支持 SELinux 目前 SELinux 支持三种模式,分别如下: ?enfor

Java中实现多线程的两种方式之间的区别

Java提供了线程类Thread来创建多线程的程序.其实,创建线程与创建普通的类的对象的操作是一样的,而线程就是Thread类或其子类的实例对象.每个Thread对象描述了一个单独的线程.要产生一个线程,有两种方法: ◆需要从Java.lang.Thread类派生一个新的线程类,重载它的run()方法:  ◆实现Runnalbe接口,重载Runnalbe接口中的run()方法. 为什么Java要提供两种方法来创建线程呢?它们都有哪些区别?相比而言,哪一种方法更好呢? 在Java中,类仅支持单继承

include的两种用法之间的区别

以下是对include 两种用法的区别 主要有两个方面的不同; 一:执行时间上: <%@ include file="relativeURI"%> 是在翻译阶段执行 <jsp:include page="relativeURI" flush="true" /> 在请求处理阶段执行. 二:引入内容的不同: <%@ include file="relativeURI"%> 引入静态文本(html

Java中有两种实现多线程的方式以及两种方式之间的区别

网上流传很广的是一个网上售票系统讲解.转发过来.已经不知道原文到底是出自哪里了. Java中有两种实现多线程的方式.一是直接继承Thread类,二是实现Runnable接口.那么这两种实现多线程的方式在应用上有什么区别呢? 为了回答这个问题,我们可以通过编写一段代码来进行分析.我们用代码来模拟铁路售票系统,实现通过四个售票点发售某日某次列车的100张车票,一个售票点用一个线程表示. 我们首先这样编写这个程序: Java代码    class ThreadTest extends Thread{

Python 中格式化字符串 % 和 format 两种方法之间的区别

Python2.6引入了 format 格式化字符串的方法,现在格式化字符串有两种方法,就是 % 和 format ,具体这两种方法有什么区别呢?请看以下解析. # 定义一个坐标值 c = (250, 250) # 使用%来格式化 s1 = "敌人坐标:%s" % c 上面的代码很明显会抛出一个如下的TypeError: TypeError: not all arguments converted during string formatting 像这类格式化的需求我们需要写成下面丑陋

linux环境下编译运行OpenCV程序的两种方法

一.命令行Command Line 1 g++ opencv_test.cpp -o opencv_test `pkg-config --cflags --libs opencv` 2 ./opencv_test test.jpg 备注:pkg-config选项--cflags 它是用来指定程序在编译时所需要头文件所在的目录--libs 则是指定程序在链接时所需要的动态链接库的目录 二.CMake工具编译 在程序同目录下创建CMakeLists.txt 1 #文件地址(下载源码安装包中):/op

hive与mysql两种数据源之间的join

这篇文章是基于上一篇文章的续集 一:程序 1.程序. 1 package com.scala.it 2 3 import java.util.Properties 4 5 import org.apache.spark.sql.SaveMode 6 import org.apache.spark.sql.hive.HiveContext 7 import org.apache.spark.{SparkConf, SparkContext} 8 9 object HiveToMysql { 10

hibernate04--三种状态之间的转换

public class StudentTest { Session session=null; Transaction transaction=null; //在执行测试方法之前 先执行before @Before public void before(){ /** * 01.读取核心的配置文件 在src的根目录下面! 底层规定位置! * 在实例化Configuration对象的时候通过configure() * 去src根目录下面,寻找hibernate.cfg.xml文件 */ Confi