[Insight-users] Use a OpenCV IplImage in ITK
Luis Ibanez
luis.ibanez at kitware.com
Thu Nov 26 11:45:34 EST 2009
Hi Tom,
Yes,
It can certainly be more complicated that what I described for Marco.
The code example in my previous email is just intended to get him
started. It is by no means a full integration of ITK and OpenCV.
The conversion examples are only covering one pixel type, with
a given depth and number of channels, and as you pointed out,
it is assuming that no padding is made on the image borders.
Flexibility for all these features will have to be added for a full
integration.
Thanks for adding this item to the wish list for ITK 4.0.
Luis
--------------------------------------------------------------------------------------------------
On Thu, Nov 26, 2009 at 11:22 AM, Tom Vercauteren
<tom.vercauteren at gmail.com> wrote:
> Hi Luis,
>
> As mentioned on this list some time ago
> http://www.cmake.org/pipermail/insight-users/2008-April/025380.html
> if one wants to avoid memory copies, it may be more complex than that
> to integrate ITK and OpenCV.
>
> More specifically, when the size of the image is (typically) not a
> multiple of four, most image libraries that focus on computational
> performance (such as opencv) will pad the buffer to ensure nice data
> alignment (this implies the concept of stride which does not yet
> appear in ITK). It would be great if ITK could also follow that route
> at some point. I have put this request on the ITK 4.0 wish list:
> http://www.itk.org/Wiki/ITK_Release_4.0#Image_Representation
>
> Best regards,
> Tom
>
> On Thu, Nov 26, 2009 at 16:46, Luis Ibanez <luis.ibanez at kitware.com> wrote:
>> 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
>>>
>>>
>>
>> _____________________________________
>> 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
>>
>>
>
More information about the Insight-users
mailing list