Click here to Skip to main content
15,892,674 members
Articles / Programming Languages / C++

STL like template based coding with the MMX/SSE extension

Rate me:
Please Sign up or sign in to vote.
4.76/5 (17 votes)
17 Nov 20062 min read 114.2K   669   45  
STL like template based coding with the MMX/SSE extension using OpenCV, vigra, and boost.
/************************************************************************/
/*                                                                      */
/*               Copyright 2006-2007 by Hirotaka Niitsuma               */
/*                                                                      */
/*                                                                      */
/*    This file is part of the STLLCV computer vision library.          */
/*    ( Version 0.7, Oct 31 2006 )                                      */
/*    You may use, modify, and distribute this software according       */
/*    to the terms stated in the LICENSE file included in               */
/*    the STLLCV distribution.                                          */
/*                                                                      */
/*    The STLLCV Website is                                             */
/*        http://www2s.biglobe.ne.jp/~niitsuma/STLLCV/                  */
/*    Please direct questions, bug reports, and contributions to        */
/*        niitsuma@mub.biglobe.ne.jp�@�@�@                              */
/*                                                                      */
/*  THIS SOFTWARE IS PROVIDED AS IS AND WITHOUT ANY EXPRESS OR          */
/*  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED      */
/*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
/*                                                                      */
/************************************************************************/

#include <iostream>

#include "stllcv/gil_wrap_iplimage.hpp"

#include "cv.h"

//#include "stllcv/gil_wrap_iplimage_factory.hpp"

#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>());
        }
    }
}



int main(int argc, unsigned char* argv[])
{

	char winName[]="srcImg";
	cvNamedWindow( winName, 1 );

	IplImage *piplimg;

#if 1
	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 gray_view=interleaved_view(piplimg->widthStep ,piplimg->height,
														gray8_interleaved_ptr(
															reinterpret_cast<unsigned char *const >(piplimg->imageData)
															),
														sizeof(unsigned char)*piplimg->widthStep
													  );
	



	gil_wrap_iplimage<gray8_interleaved_ptr> iplwrapgray1(piplimg->widthStep ,piplimg->height);
	std::copy(gray_view.begin(),gray_view.end(),iplwrapgray1.begin());
		cvShowImage( winName, iplwrapgray1.pIplImage );
		std::cout << "Wait Key Input" << std::endl; 
		cvWaitKey(0);

	x_gradient(gray_view,((gray8_interleaved_view_t)iplwrapgray1));
		cvShowImage( winName, iplwrapgray1.pIplImage );
		std::cout << "Wait Key Input" << std::endl; 
		cvWaitKey(0);	



	gray8_interleaved_view_t gray_view1=interleaved_view(iplwrapgray1.width() ,iplwrapgray1.height(),
														gray8_interleaved_ptr(
															reinterpret_cast<unsigned char *const >(iplwrapgray1.pIplImage->imageData)
															),
                                                      sizeof(unsigned char)* iplwrapgray1.pIplImage->widthStep);
	std::copy(gray_view.begin(),gray_view.end(),gray_view1.begin());
		cvShowImage( winName, iplwrapgray1.pIplImage );
		std::cout << "Wait Key Input" << std::endl; 
		cvWaitKey(0);

	x_gradient(gray_view,gray_view1);
		cvShowImage( winName, iplwrapgray1.pIplImage );
		std::cout << "Wait Key Input" << std::endl; 
		cvWaitKey(0);	




	gray8_interleaved_view_t gray_sub_view=subimage_view(gray_view, 20,30, 40,40);


	gil_wrap_iplimage<gray8_interleaved_ptr> iplwrapgray2(40 ,40);
	std::copy(gray_sub_view.begin(),gray_sub_view.end(),iplwrapgray2.begin());
		cvShowImage( winName, iplwrapgray2.pIplImage );
		std::cout << "Wait Key Input" << std::endl; 
		cvWaitKey(0);





	gray8_interleaved_view_t gray_view2=interleaved_view(iplwrapgray2.width() ,iplwrapgray2.height(),
														gray8_interleaved_ptr(
															reinterpret_cast<unsigned char *const >(iplwrapgray2.pIplImage->imageData)
															),
                                                      sizeof(unsigned char)* iplwrapgray2.pIplImage->widthStep);
	std::copy(gray_sub_view.begin(),gray_sub_view.end(),gray_view2.begin());
		cvShowImage( winName, iplwrapgray2.pIplImage );
		std::cout << "Wait Key Input" << std::endl; 
		cvWaitKey(0);




	cvReleaseImage( &piplimg );

#endif


	piplimg=cvLoadImage( "test.jpg");
		cvShowImage( winName, piplimg );
		std::cout << "Wait Key Input" << std::endl; 
		cvWaitKey(0);

	typedef interleaved_ptr<unsigned char*, bgr_t> bgr8_interleaved_ptr;
	typedef type_from_x_iterator<bgr8_interleaved_ptr >::view_t bgr8_interleaved_view_t;

	bgr8_interleaved_view_t bgr_view=interleaved_view(piplimg->widthStep/3 ,piplimg->height,
														bgr8_interleaved_ptr(
															reinterpret_cast<unsigned char *const >(piplimg->imageData)
															),
														sizeof(unsigned char)*piplimg->widthStep
													  );
	



	gil_wrap_iplimage<bgr8_interleaved_ptr> iplwrapbgr1(piplimg->widthStep/3 ,piplimg->height);
	std::copy(bgr_view.begin(),bgr_view.end(),iplwrapbgr1.begin());
		cvShowImage( winName, iplwrapbgr1.pIplImage );
		std::cout << "Wait Key Input" << std::endl; 
		cvWaitKey(0);

	x_gradient(bgr_view,((bgr8_interleaved_view_t)iplwrapbgr1));
		cvShowImage( winName, iplwrapbgr1.pIplImage );
		std::cout << "Wait Key Input" << std::endl; 
		cvWaitKey(0);	


	bgr8_interleaved_view_t bgr_sub_view=subimage_view(bgr_view, 20,30, 40,40);


	gil_wrap_iplimage<bgr8_interleaved_ptr> iplwrapbgr2(40 ,40);
	std::copy(bgr_sub_view.begin(),bgr_sub_view.end(),iplwrapbgr2.begin());
		cvShowImage( winName, iplwrapbgr2.pIplImage );
		std::cout << "Wait Key Input" << std::endl; 
		cvWaitKey(0);


	cvReleaseImage( &piplimg );
	

    return 0;


}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain 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


Written By
Japan Japan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions