Boost logo

Boost :

From: N H (hirotaka.niitsuma_at_[hidden])
Date: 2006-11-02 19:01:03


I made wrapping of OpenCV using gil.
But now, only gray image is wraped.
---------------------------------------------------------------
#ifdef WIN32
#define _CRT_SECURE_NO_DEPRECATE 1
#pragma warning(disable : 4244) //
#pragma warning(disable : 4996) // MSFT declared it deprecated
#endif

#define USE_GIL_CONCEPT_CHECK

#include <iostream>

#include <ipl.h>
#include "cv.h"
#include "highgui.h"

#include "interleaved_ptr.hpp"

#define IPL_DEPTH_SIGN 0x80000000
#define IPL_DEPTH_MASK 0x7FFFFFFF

#define IPL_DEPTH_1U 1
#define IPL_DEPTH_8U 8
#define IPL_DEPTH_16U 16
#define IPL_DEPTH_32F 32

#define IPL_DEPTH_8S (IPL_DEPTH_SIGN| 8)
#define IPL_DEPTH_16S (IPL_DEPTH_SIGN|16)
#define IPL_DEPTH_32S (IPL_DEPTH_SIGN|32)
#define IPL_DEPTH_64F 64

template<class PIXELTYPE>class CiplImageDepth {public: int
depth;CiplImageDepth(){depth= sizeof(PIXELTYPE);}};
template<>class CiplImageDepth<unsigned char> {public: int
depth;CiplImageDepth(){depth= IPL_DEPTH_8U;}};
template<>class CiplImageDepth<char> {public: int
depth;CiplImageDepth(){depth= IPL_DEPTH_8S;}};
template<>class CiplImageDepth<unsigned short> {public: int
depth;CiplImageDepth(){depth= IPL_DEPTH_16U;}};
template<>class CiplImageDepth<short> {public: int
depth;CiplImageDepth(){depth= IPL_DEPTH_16S;}};
template<>class CiplImageDepth<int> {public: int
depth;CiplImageDepth(){depth= IPL_DEPTH_32S;}};
template<>class CiplImageDepth<float> {public: int
depth;CiplImageDepth(){depth= IPL_DEPTH_32F;}};
template<>class CiplImageDepth<double> {public: int
depth;CiplImageDepth(){depth= IPL_DEPTH_64F;}};

using namespace gil;

template < typename Iterator, typename IPLIMAGE = IplImage>
class iplimage_wrap : public type_from_x_iterator<Iterator>::view_t
{
        Iterator _iterator_;
public:
        IPLIMAGE *pIplImage;

        int iplDepthDef;

        IPLIMAGE* initProc(int width,int height)
        {
                CiplImageDepth<
                        channel_t
> iplImageDepth;

                iplDepthDef=iplImageDepth.depth;
                pIplImage=
                        cvCreateImage( cvSize(width, height),
                        iplDepthDef
                        , 1 );
                return pIplImage;
        }

        iplimage_wrap(int width, int height)
                :
                _iterator_(
                        reinterpret_cast< channel_t * >
                        (
                                (pIplImage=initProc(width,height) )->imageData
                        )
                )
        {
                (type_from_x_iterator<Iterator>::view_t )(*this)
                        = interleaved_view(
                                        pIplImage->widthStep/sizeof(channel_t), height
                                                , _iterator_
                                                , sizeof(channel_t)
                                        );
        }

        ~iplimage_wrap()
        {
                cvReleaseImage( &pIplImage );
        }
};

int main(int argc, unsigned char* argv[])
{
        char winName[]="srcImg";
        cvNamedWindow( winName, 1 );

        IplImage *piplimg;
        piplimg=cvLoadImage( "test.jpg", 0 );

        cvShowImage( winName, piplimg );
        
        {
                std::cout << "Wait Key Input" << std::endl;
                cvWaitKey(0);
        }

        typedef interleaved_ptr<unsigned char*, gray_t> gray8_interleaved_ptr;

        typedef type_from_x_iterator<gray8_interleaved_ptr >::view_t
gray8_interleaved_view_t;

        gray8_interleaved_view_t
maped_view=interleaved_view(piplimg->widthStep ,piplimg->height,
                                                                                                                gray8_interleaved_ptr(
                                                                                                                        reinterpret_cast<unsigned char *const >(piplimg->imageData)
                                                                                                                        ),
                                                      sizeof(unsigned char));

        
        gray8_interleaved_view_t step2 = subimage_view(maped_view, 20,30, 40,40);

        iplimage_wrap<gray8_interleaved_ptr> iplim(40,40);
        (gray8_interleaved_view_t) iplim = step2;

        cvShowImage( winName, iplim.pIplImage );
        
        {
                std::cout << "Wait Key Input" << std::endl;
                cvWaitKey(0);
        }

        cvReleaseImage( &piplimg );

    return 0;
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk