www.pudn.com > colortracker.rar > integralimage.h


#ifndef _INTEGRALIMAGE_H_
#define _INTEGRALIMAGE_H_

#include  
#include  

#ifndef ASSERT
#ifdef _DEBUG
#include 
#define ASSERT(x) assert(x)
#else
#define ASSERT(x) 
#endif
#endif
 
/* ================================================================ */ 
 
template 
class TImage 
{
protected: 
	T *array; 
	int width; 
	int height; 
	int numpixels; 
	int my_memory; 
 
public:	 
	TImage (const T *array, const int, const int);
	TImage (const int, const int);
	TImage (const int, const int, T);
	TImage (const TImage &other){
		width = other.width;
		height = other.height;
		numpixels = other.numpixels;
		array = other.array;
	}
	TImage (){};
	inline void setSize(const int width_, const int height_){
		width = width_;
		height = height_;
		numpixels = width * height;
		array = new T[numpixels]; 
		memset(array,0,sizeof(T)*numpixels);
		my_memory = 1;
	}
	inline void deleteArray(){
		delete [] array;
	}
	 
	virtual ~TImage ( ); 
	 
	virtual T* getArray(){return array;}
	
	void print ( ) const;
	void print ( const int m ) const;
};
 
/* ================================================================ */ 

template 
inline TImage::TImage (const T *array_, const int width_, const int height_ )
{
	width = width_;
	height = height_;
	numpixels = width * height;
	array = const_cast(array_);
	my_memory = 0;
}
 
/* ================================================================ */ 

template 
inline TImage::TImage ( const int width_, const int height_ )
{
	setSize(width_,height_);
}
 
/* ================================================================ */ 

template 
inline TImage::TImage ( const int width_, const int height_ , const T val)
{
	setSize(width_,height_);
	T *ptr = array;
	for(int y=0; y 
inline TImage::~TImage ()
{
	if(my_memory)
		delete [] array;
}
 
/* ================================================================ */ 

template 
void TImage::print ( ) const 
{
	for(int y = 0; y < height; y++) {
		for(int x = 0; x < width; x++) {
			cout << getPixel(x,y) << " ";
		}
		cout << endl;
	}
}
 
/* ================================================================ */ 

template 
void TImage::print ( const int m ) const 
{
	for(int y = 0; y < min(m,height); y++) {
		for(int x = 0; x < min(m,width); x++) {
			cout << getPixel(x,y) << " ";
		}
		cout << endl;
	}
}
 
/* ================================================================ */ 

template 
class TIntegral : public TImage
{ 
	
public: 
	const int getImWidth() const {return width -1;	} 
	const int getImHeight() const {return height -1;	} 
	const int getIntWidth() const {return width;	} 
	const int getIntHeight() const {return height;	} 
	 
	const int getImNumPix() const { return getImWidth()*getImHeight();} 
	const int getIntNumPix() const { return getIntWidth()*getIntHeight();} 
	 
	
	TIntegral(const int width, const int height){ 
		setSize(width,height);
	} 
  void setSize(const int width, const int height){ 
    TImage::setSize(width+1,height+1); 
	}
	TIntegral() : TImage() { 
	} 
 
	/* integrate in place */ 
	void integrate() {  
		int x, y; 
		T * p = array; 
// 		for(int x = 0; x < width; ++x) setIntPixel(x,0,0);   /* create top row of zeros */ 
// 		for(int y = 0; y < width; ++y) setIntPixel(0,y,0);   /* create left column of zeros */ 
 
		for(x = 1; x < width; ++x) { 
			double v0 = getIntPixel(x-1,0); 
			double v1 = getIntPixel(x,0); 
			setIntPixel(x,0,v0+v1);   /* integrate top row */ 
		} 
 
		for(y = 1; y < height; ++y) { 
			double v0 = getIntPixel(0,y-1); 
			double v1 = getIntPixel(0,y); 
			setIntPixel(0,y,v0+v1);   /* create left column of zeros */ 
    } 
 
		for(y = 1; y < height; ++y){ 
			for(x = 1; x < width; ++x){ 
				T val = getIntPixel(x,y) + getIntPixel(x-1, y) + 
				        getIntPixel(x, y-1) - getIntPixel(x-1, y-1); 
			  setIntPixel(x,y,val); 
			} 
		} 
	} 
	
	inline  T getIntPixel (int x, int y) const{
		int lx = x, ly = y;
		if(lx < 0 || ly < 0) return 0; 
		
		if(lx >= width) lx = width - 1; 
		if(ly >= height) ly = height - 1; 
		 
		int index = width * ly + lx; 
		ASSERT (index >= 0 && index < numpixels);
		return array[index];
	}; 
	 
	 
	inline	void setIntPixel ( const int x, const int y, const T &value ) 
	{ 
		int lx = x, ly = y; 
		 
		ASSERT( lx >= 0 && ly >=0 && lx < width && ly < height);  
		if (lx < 0) lx = 0; 
		if (ly < 0) ly = 0; 
		if(lx >= width) lx = width - 1; 
		if(ly >= height) ly = height - 1; 
 
		int index = width * ly + lx; 
		ASSERT (index >= 0 && index < numpixels); 
		array[index] = value; 
		 
	} 
	inline  T getImPixel (int x, int y) const{ 
		return getIntPixel(x+1,y+1); 
	}; 
	 
	 
	inline	void setImPixel ( const int x, const int y, const T &value ) 
	{ 
		setIntPixel(x+1,y+1,value); 
	}; 
	
	void printIm(const char *filename) { 
		FILE *fid = fopen(filename, "w"); 
		int z= 12003; 
		for (int y = 0; y < getImHeight(); y++) { 
			for (int x = 0; x < getImWidth(); x++) 
				fprintf(fid, "%g\t", getImPixel(x, y)); 
			fprintf(fid, "\n"); 
		} 
		fclose(fid); 
	} 
 
	void printInt(const char *filename) { 
		FILE *fid = fopen(filename, "w"); 
		for (int y = 0; y < getIntHeight(); y++) { 
			for (int x = 0; x < getIntWidth(); x++) 
				fprintf(fid, "%g\t", getIntPixel(x, y)); 
			fprintf(fid, "\n"); 
		} 
		fclose(fid); 
	} 
 
	void printImBin(const char *filename, bool flag) { 
		std::ofstream os(filename, ios::binary | ios::app); 
		if (flag) { 
			os.flags(ios::binary); 
			int width = getImWidth(); 
			int height = getImHeight(); 
			os.write((char *)&width, sizeof(width)); 
			os.write((char *)&height, sizeof(height)); 
		} 
		for (int y = 0; y < getImHeight(); y++) { 
			for (int x = 0; x < getImWidth(); x++) { 
				T val = (T)(getImPixel(x, y)); 
				os.write((char *)&val, sizeof(val)); 
			} 
		} 
		os.close(); 
	} 
 
	void printIntBin(const char *filename, bool flag) { 
		std::ofstream os(filename, ios::binary | ios::app); 
		if (flag) { 
			os.flags(ios::binary); 
			int width = getIntWidth(); 
			int height = getIntHeight(); 
			os.write((char *)&width, sizeof(width)); 
			os.write((char *)&height, sizeof(height)); 
		} 
		for (int y = 0; y < getIntHeight(); y++) { 
			for (int x = 0; x < getIntWidth(); x++) { 
				T val = (T)(getIntPixel(x, y)); 
				os.write((char *)&val, sizeof(val)); 
			} 
		} 
		os.close(); 
	} 
 
	void setToMinLog () { 
	  static TImage cache(getIntWidth(), getIntHeight(), -1000); 
		T *ptr = array; 
		memcpy(ptr, cache.getArray(), sizeof(T)*getIntNumPix()); 
	} 

};
 
/* ================================================================ */ 

#endif	_INTEGRALIMAGE_H_