STL like template based coding with the MMX/SSE Extension

Source: Internet
Author: User
Tags image processing library unpack
Document directory
  • Matrix operation wrapping
  • Image operation wrapping
  • Using downloaded file
  • Using stllcv with your project

//

Http://www.codeproject.com/Articles/12115/STL-like-template-based-coding-with-the-MMX-SSE-ex

 

By hirotaka niitsuma,
17 Nov 2006 Introduction

Accelerating with MMX/SSE extension is one of the most valid tive Performance gain techniques in image processing, signal processing and numerical computing. To accelerate your application with MMX/SSE,
Intel provides the following solutions:

  • Intel opencv
  • Intel integrated performance primitive (or IPL)
  • Intel C/C ++ Compiler

A combination of these solutions is the best solution. Open CV is the interface part of this combination. However, Coding Based on opencv causes messy and unreadable code. More sophisticated coding with MMX/SSE Extension
Can be achieved by wrapping opencv Based on STL like code.
STL like opencv wrapper (stllcv) is a wrapping of opencv which enables STL like readable code. This wrapping is also an interface to the following STL like libraries:

  • Vigra

    Ublas
  • Adobe Gil

The following are belief explanations about the usage of this wrapping.

Matrix operation wrapping

Ublas is a matrix operation based on expressional template technique. expressional template technique enables simple description of complicated matrix operations.
The interface
Ublas enables the same simple description with varous opencv vraioue functions. The usage of this wrapping is given below.

#include "stllcv/ublascvmatrix.hxx"using namespace stllcv;//init matrixCublasCvMatrix<float,3,3> A;A[0][0]=3; A[0][1]=2; A[0][2]=1;A[1][0]=1; A[1][1]=1; A[1][2]=4;A[2][0]=3; A[2][1]=2; A[2][2]=5;CublasCvMatrix<float,3,3> B;//Multiple as OpenCV matrixcvMatMul( &(CvMat)A , &(CvMat)A, &(CvMat) B );//Mutiple as uBLAS matrixB =boost::numeric::ublas::prod (A,A);

See
Here, for more information on this class.

Image operation wrappinginterface to vigra

Vigra is a STL like image processing library. vigra provides varous STL style image processing functions. This interface

Vigra enables STL style image processing with various opencv variouse functions. The usage of this wrapping is given below.

Usage
#include "vigra/transformimage.hxx"#include "vigra/recursiveconvolution.hxx"#include "stllcv/iplvbasicimageoperation.hxx"#include "stllcv/iplvbasicimage.hxx"#include "highgui.h"using namespace stllcv;#define PEXTYPE unsigned charint main(int argc, char * argv[]){        char winName[]="srcImg";        cvNamedWindow( winName, 1 );        CiplvBasicImage<PEXTYPE> iplvImage1("lena.jpg");        showIplvBasicImag<PEXTYPE>(&iplvImage1,winName);        int width1 = iplvImage1.width();        int height1 =iplvImage1.height();        CiplvBasicImage<PEXTYPE> iplvImage2( width1*2 ,height1*2);        std::fill(iplvImage2.begin(),iplvImage2.end(),100);        iplRotate(iplvImage1.pIplImage,                  iplvImage2.pIplImage,                  30.0, 100 ,150 ,IPL_INTER_NN);             showIplvBasicImag<PEXTYPE>(&iplvImage2,winName);        vigra::initImage(           destIterRange(             iplvImage2.upperLeft() + vigra::Diff2D(50, 100),             iplvImage2.upperLeft() + vigra::Diff2D(400, 200))             ,200);        showIplvBasicImag<PEXTYPE>(&iplvImage2,winName);        vigra::transformImage(srcImageRange(iplvImage2),                       destImage(iplvImage2),                       vigra::linearIntensityTransform(-1, -255));        showIplvBasicImag<PEXTYPE>(&iplvImage2,winName);        CiplvBasicImage<PEXTYPE> iplvImage3tmp( iplvImage2);        CiplvBasicImage<PEXTYPE> iplvImage3( iplvImage2);        int scale = 5;        vigra::recursiveSmoothX(vigra::srcImageRange(iplvImage2),                                vigra::destImage(iplvImage3tmp), scale);        vigra::recursiveSmoothY(vigra::srcImageRange(iplvImage3tmp),                                vigra::destImage(iplvImage3), scale);        showIplvBasicImag<PEXTYPE>(&iplvImage3,winName);        saveIplvBasicImag<PEXTYPE>(&iplvImage3, "out.jpg");        cvDestroyWindow(winName);        return 0;}
Interface to Adobe Gil

Adobe Gil is also STL like image processing library. Usage of our wrapping is given below

#include "stllcv/gil_wrap_iplimage.hpp"#include <iostream>#include "cv.h"#include "stllcv/gil_dynamic_wrap_iplimage.hpp"//including this file icrease complie time#include "highgui.h"using namespace gil;using namespace stllcv;template <typename Out>struct halfdiff_cast_channels {    template <typename T> Out operator()(const T& in1,                                     const T& in2) const {        return Out((in2-in1)/2);}};template <typename SrcView, typename DstView>void x_gradient(const SrcView& src, const DstView& dst) {    typedef typename DstView::channel_t dst_channel_t;    for (int y=0; y<src.height(); ++y) {        typename SrcView::x_iterator src_it = src.row_begin(y);        typename DstView::x_iterator dst_it = dst.row_begin(y);        for (int x=1; x<src.width()-1; ++x) {            transform_channels(src_it[x-1], src_it[x+1], dst_it[x],                                halfdiff_cast_channels<dst_channel_t>());}}}template <typename DstView>struct x_gradient_obj {    typedef void result_type;        // required typedef    const DstView& _dst;    x_gradient_obj(const DstView& dst) : _dst(dst) {}    template <typename SrcView>     void operator()(const SrcView& src)          const { x_gradient(src, _dst); }};template <typename SrcViews, typename DstView>void x_gradient(any_image_view<SrcViews>& src, const DstView& dst) {    apply_operation(src, x_gradient_obj<DstView>(dst));}template<typename ImageClass>void show_image(ImageClass &image, char *win_name ){    cvShowImage( win_name, image.pIplImage );        std::cout << "Wait Key Input" << std::endl;         cvWaitKey(0);}int main(int argc, unsigned char* argv[]){    char winName[]="srcImg";    cvNamedWindow( winName, 1 );    IplImage *gray_piplimg=cvLoadImage( "test.jpg", 0 );        IplImage *color_piplimg=cvLoadImage( "test.jpg");    int width=gray_piplimg->width;    int height=gray_piplimg->height;    int sub_width=115;    int sub_height=113;//plz see type naming rule http://opensource.adobe.com///        gil/html/giltutorial.html#AppendixConventionSec//static wrap    rgb8_planar_view_t  rgb8planarview1    =       gil_view_from_iplimage<rgb8_planar_ptr_t >(color_piplimg);    rgb8_planar_view_t  rgb8planarview2    =       gil_view_from_iplimage<planar_ptr<unsigned char *,        rgb_t> >(color_piplimg);    //planar_color is not supported in some OpenCV's functions.    bgr8_view_t bgr8view1 =      gil_view_from_iplimage<bgr8_ptr_t >(color_piplimg);    //pixel<float ,bgr_t >* == bgr32f_pixel_t* == bgr32f_ptr_t    bgr32f_view_t    bgr32fview1    =      gil_view_from_iplimage<bgr32f_ptr_t>(color_piplimg);    bgr32f_view_t    bgr32fview2    =      gil_view_from_iplimage<bgr32f_pixel_t *>(color_piplimg);    bgr32f_view_t    bgr32fview3    =      gil_view_from_iplimage<pixel<float ,      bgr_t >*>(color_piplimg);        gil_wrap_iplimage<gray8_pixel_t*>    graywrap1("test.jpg");    gil_wrap_iplimage<bgr8_ptr_t> colorwrap1(width,height);    gil_wrap_iplimage<bgr8_ptr_t> colorwrap2(color_piplimg);        std::copy(bgr8view1.begin(),bgr8view1.end(),colorwrap1.begin());    show_image(colorwrap1,winName);    x_gradient(bgr8view1,(bgr8_view_t)colorwrap1);    show_image(colorwrap1,winName);    bgr8_view_t  bgr_sub_view=subimage_view(bgr8view1,                  20,30, sub_width, sub_height);    gil_wrap_iplimage<bgr8_pixel_t *>            color_sub_wrap1(sub_width , sub_height);    std::copy(bgr_sub_view.begin(),bgr_sub_view.end(),           color_sub_wrap1.begin());    show_image(color_sub_wrap1,winName);//dynamic wrap//detail of dynamic image  http://opensource.adobe.com///         gil/html/giltutorial.html#DynamicImageSec//This imp is based on gil::any_image_view    any_ipl_image_view_t color_dynamic_view1(            gil_dynamic_view_from_iplimage(color_piplimg));    gil_dynamic_wrap_iplimage<IplImage>             color_dynamic_wrap1("test.jpg");    gil_dynamic_wrap_iplimage<IplImage>             color_dynamic_wrap2(width,height);    gil_dynamic_wrap_iplimage<IplImage>             color_dynamic_wrap3(color_piplimg);    x_gradient(*(color_dynamic_wrap3.any_image_view_ptr),            (bgr8_view_t)colorwrap1);        std::cout << "x_gradient_any_view_warp_class "                    << std::endl;         show_image(colorwrap1,winName);    cvReleaseImage( &gray_piplimg );        cvReleaseImage( &color_piplimg );        return 0;}
Using stllcvusing downloaded file
  • Install opencvC: \ Program Files \ opencv
  • Install boost 1.33.1C: \ Lib \ boost_000033_1
  • Install vigra 1.4.0C: \ Lib \ vigra1.4.0
  • Install Adobe: GilC: \ Lib \ Adobe \ Gil
  • Unpack downloaded fileStllcv_0_7_7.zipToC: \ Lib \ stllcv_0_7_7
  • OpenC: \ Lib \ stllcv_0_7_7 \ SRC \ stllcv_vs2003.sln
  • You can see projects of above examples
Using stllcv with your project
  • Install opencv and add its include path (C: \ Program Files \ opencv \ CV \ include;
    C: \ Program Files \ opencv \ cxcore \ include;C: \ Program Files \ opencv \ otherlibs \ highgui(And
    C: \ Program Files \ intel \ plsuite \(Include if you have IPL ))
  • Add opencv's libpath (C: \ Program Files \ opencv \ Lib) And add links
    Cxcore. Lib
    ,Cv. Lib,Highgui. Lib,(IPL. LibIf you have IPL)
  • Install boost and add its include path (C: \ Lib \ boost_000033_1)
  • Install vigra and add its include path (C: \ Lib \ vigra1.4.0 \ include)
  • Install Adobe: Gil and add its include path (C: \ Lib \ Adobe \ Gil)
  • Unpack downloaded fileStllcv_0_7_7.zipAnd add the include path (C: \ Lib \ stllcv_0_7_7 \ include)
  • Include appropriate header files based on above examples
  • Then you can use varous wrapping class and functions with your project

The current version of stllcv can be downloaded from
STL like opencv wrapper (stllcv ).

License

This article has no explicit license attached to it but may contain in usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found
Here

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.