[Insight-users] Use a OpenCV IplImage in ITK

Luis Ibanez luis.ibanez at kitware.com
Thu Nov 26 10:46:29 EST 2009


Hi Marco,

                                     Welcome to ITK !


If you want to integrate ITK and OpenCV in the same application,
you will find useful to look at the Tutorials

                  http://www.itk.org/ITK/help/tutorials.html

In particular to:
"Integrating ITK into your Application"
http://www.itk.org/CourseWare/Training/GettingStarted-V.pdf

This tutorial describes multiple methods for converting an ITK
image into a custom image data structure (IplImage in your case),
as well as how to import a custom data structure (IplImage) into
an ITK image.


--

The IplImage structure in OpenCV has the following terms:


typedef struct _IplImage
{
    int nSize;
    int ID;
    int nChannels;
    int alphaChannel;
    int depth;

    char colorModel[4];
    char channelSeq[4];
    int dataOrder;

    int origin;

    int align;

    int width;
    int height;
    struct _IplROI *roi;
    struct _IplImage *maskROI;
    void *imageId;
    struct _IplTileInfo *tileInfo;
    int imageSize;


    char *imageData;
    int widthStep;
    int BorderMode[4];
    int BorderConst[4];
    char *imageDataOrigin;


}
IplImage;


The essential elements that you will have to match from the IplImage
data structure, into the itk::Image are:

   IplImage::nSize ----> itk::Image  Region  SizeType
   IplImage::imageData -----> itk::Image GetBufferPointer
   IplImage::width  ----> itk::Image::GetLargestPossibleRegion()->GetSize()[0];
   IplImage::height  ----> itk::Image::GetLargestPossibleRegion()->GetSize()[1];


The elements nChannels, alphaChannel and depth, do not quite
apply to ITK, since ITK uses template arguments for its images.

If you are using the commonl IplImage representation, you probably
have an image of 3 channels (RGB), with a 8 bits depth each.

Such an image will be equivalent to:

               itk::Image< itk::RGBPixel< char >, 2 >

but if you set up the depth and number of channels of the IplImage
you can as well use an ITK image of type:

               itk::Image< unsigned char, 2 >


In short, you need to write two functions:

   A) Convert IplImage to ITK Image
   B) Convert ITK Image to IplImage

For writing function (A), you essentially use the
itkImportImageFilter. This is explained in detail in the
ITK Software Guide

    http://www.itk.org/ItkSoftwareGuide.pdf

in Section 4.1.7 "Importing Image Data from a Buffer"
in PDF page:  78.

On the other hand, for writing Function (B), you simply
need to query the essential elements of the ITK image.

In particular you need:

     image->GetBufferedRegion()->GetSize()

that return an array of dimension N with the number of
pixels along each dimension. (ITK supports N-Dimensional)
images.

   IplImage.width    = image->GetBufferedRegion()->GetSize()[0];
   IplImage.height  = image->GetBufferedRegion()->GetSize()[1];

Then, the buffer of data can be passed as:

   IplImage.imageData = image->GetBufferPointer();


-----

The example on the use of the ImportImageFilter is in

    Insight/Examples/DataRepresentation/Image/Image5.cxx


For your convenience, please find attached an example
of joint use of ITK and OpenCV.

The FindOpenCV.cmake file was simply copied from:
http://opencv.willowgarage.com/wiki/Getting_started?action=AttachFile&do=view&target=FindOpenCV.cmake

The file OpenCV_ITK.cxx contains examples of functions
that will convert ITK images to OpenCV images and back.


   Put these three files in the same directory and configure
   with CMake and build.

   Note that the OpenCV display methods require that
   you build OpenCV with GTK support.


     Please give it a try and let us know if you find any problem.

     Also, if you find this useful, it will be great is you repost
     this as a paper for the Insight Journal:          :-)

                 http://www.insight-journal.org/



     Regards,


             Luis


--------------------------------------------------------
On Thu, Nov 26, 2009 at 5:11 AM, Marco Meoni - Sbaush <sbaush at gmail.com> wrote:
> Hi, i'm a newbie in ITK.
> I'm a image processing developer, and at the moment i'm using OpenCV library
> to analyse and process my images.
> I need some comfortable functions of ITK so i need to know how can use
> IplImage (the basic OpenCV image structure) in ITK toolkit.
> I searched on web, but apparently nobody did this in past.
> Thanks for help!
> --
> Marco Meoni - Sbaush
>
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.html
>
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.itk.org/mailman/listinfo/insight-users
>
>
-------------- next part --------------
CMAKE_MINIMUM_REQUIRED(VERSION 2.7)

PROJECT( OpenCV_ITK )

SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR})

FIND_PACKAGE( OpenCV REQUIRED )
INCLUDE_DIRECTORIES( ${OPENCV_INCLUDE_DIR} )

FIND_PACKAGE(ITK REQUIRED)
INCLUDE(${ITK_USE_FILE})

ADD_EXECUTABLE( OpenCV_ITK OpenCV_ITK.cxx )

TARGET_LINK_LIBRARIES( OpenCV_ITK 
  ${OPENCV_LIBRARIES}
  ITKIO ITKCommon )
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenCV_ITK.cxx
Type: text/x-c++src
Size: 5461 bytes
Desc: not available
URL: <http://www.itk.org/pipermail/insight-users/attachments/20091126/899d1a35/attachment-0001.cxx>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: FindOpenCV.cmake
Type: text/x-cmake
Size: 8122 bytes
Desc: not available
URL: <http://www.itk.org/pipermail/insight-users/attachments/20091126/899d1a35/attachment-0001.bin>


More information about the Insight-users mailing list