www.pudn.com > Estereo.rar > stereoMatching.h


/***************************************************************************  
* 
* Copyright 2004 by the Massachusetts Institute  of Technology.   All    
* rights reserved.  
*   
* Developed  by David Demirdjian 
* at the Computer Sciences and Artificial Intelligence Laboratory,  
* MIT, Cambridge, Massachusetts.  
*   
* Permission to use, copy, or modify this software and  its documentation  
* for  educational  and  research purposes only and without fee  is hereby  
* granted, provided  that this copyright notice and the original authors's  
* names appear  on all copies and supporting documentation.  If individual  
* files are  separated from  this  distribution directory  structure, this  
* copyright notice must be included.  For any other uses of this software,  
* in original or  modified form, including but not limited to distribution  
* in whole or in  part, specific  prior permission  must be  obtained from  
* MIT.  These programs shall not  be  used, rewritten, or  adapted as  the  
* basis  of  a  commercial  software  or  hardware product  without  first  
* obtaining appropriate licenses  from MIT.  MIT. makes no representations  
* about the suitability of this  software for any purpose.  It is provided  
* "as is" without express or implied warranty.  
*   
**************************************************************************/ 
#ifndef _ESTEREO_H 
#define _ESTEREO_H 
 
#include  
#include  
#include  
 
 
class StereoMatching { 
public: 
 
	unsigned char** buff; 
	unsigned char* bigBuffer, *bigBuffer_origin;     // buffers used for 8-bits use 
 
 
	// constructor 
	StereoMatching(); 
 
	// destructor 
	~StereoMatching(); 
 
	// context definition: create all buffers for stereo processing 
	void initializeContext(); 
	// destroy all buffers 
	void destroyContext(); 
 
	// -------- set/get stereo parameters --------  
	void setCameraParameters(float focal, float baseline, float u0, float v0); 
	void getCameraParameters(float& focal, float& baseline, float& u0, float& v0) const; 
 
	// set image size 
	void setImageSize(int w, int h); 
	int getWidth() const; 
	int getHeight() const;  
 
	// set undefined depth value ...  
	// all undefined pixels in the disp. image  will have this value 
	void setUndefinedDepthValue(const unsigned char undefined_val); 
 
	// set threshold to set a pixel disparity as defined/undefined 
	void setAcceptDisparityThreshold(const float newVal); 
	float getAcceptDisparityThreshold() const; 
 
	// set horopter: .... not used yet though 
	void setHoropter (const int horopt); 
	int getHoropter() const; 
	// set nb of disp to look for ... from 8 to 64 (step. 8) 
	void setNumDepth (const int nDepth); 
	int getNumDepth() const; 
 
	// set correlation window size from 5 to 17 
	void setCorrelationWindowSize(const int hmaskX, const int hmaskY); 
	int getCorrelationWindowSizeX() const; 
	int getCorrelationWindowSizeY() const; 
 
	 
	// access to internal images 
	// disparity image 
	unsigned char* getDisparityImage() const; 
	// minimum disparity scores 
	unsigned char* getCorrScores() const; 
	// second minimum disparity scores 
	unsigned char* getCorrScores_Sec() const; 
	 
	// -------- fast stereo --------  
	// perform stereo using 3 images 
	void doStereo(const unsigned char* leftImage, const unsigned char* rightImage, const unsigned char* topImage); 
	// perform stereo using 2 images  
	void doStereo(const unsigned char* leftImage, const unsigned char* rightImage); 
 
 
	// -------- filling --------  
	// iterative region growing algorithm:  
	// - mode (algo)	 {0,1} 
	// - acceptNew		threshold to accept a previously undefined pixel as defined 
	// - nbIterations	nb iteration of the algo  
	// - imDisp_start	contains the initial disp image to start with 
	//					if not given, the internal disp. image is used as starting image 
	void doStereo_grow(int mode, const unsigned char acceptNew, int nbIteration, unsigned char* imDisp_start = NULL); 
 
 
	// check if (x,y,d) is a valid hypothesis 
	bool checkValidity(short x, short y, unsigned char d, const char tol) const; 
	// return the diff. between best corr. score and the associated with (x,y,d) ... return -1 if not in the image 
	short checkValidity_error(short x, short y, unsigned char d, const char tol) const; 
 
 
	// -------- sub-pixel estimation --------  
	/// estimate subpixel disparities 'd' on a list of 'nbPoints' image points (x,y) 
	void getList_subPixel(const short* x, const short* y,  float* subpixel_depth_list, int nbPoints,  
						  unsigned char* imDepth_start = NULL); 
	// return a sub-pixels depth IMAGE 
	void getImage_subPixel(float* subpixel_depth_image, float NON_DEF_VAL=-1, unsigned char* imDepth_start = NULL); 
 
 
	// -------- 3D reconstruction of the full scene --------  
	void doReconstruction(unsigned char* imDepth_used = NULL); 
	void doReconstruction(const short* x, const short* y, const float* subpixel_depth_list, int nbPoints, 								  
						  float* X_list, float* Y_list, float* Z_list); 
 
	// return 3 images containing X,Y and Z values 
	void getImages_3D(float* X_im, float* Y_im, float* Z_im); 
	void getImage_Z(float* Z_im); // returns depth image only; 
 
	// return arrays containing 3-D reconstruction 
	float* getXlist() const; 
	float* getYlist() const; 
	float* getZlist() const; 
	// return num. of reconstructed points 
	int getNumPoints() const; 
 
 
 
private: 
	int width, height; // image size 
	int horopter; 
	int nbDepth, maxNbDepth; // range to search disp. 
 
	// forsub-pixel estimation 
	float *scorePrev, *scoreInit, *scoreNext; 
	float *scorePrev_origin, *scoreInit_origin, *scoreNext_origin; 
 
	// list of sub-pixels depth 
	bool subPixelPerformed; 
	float *depth_float_list, *depth_float_list_origin; 
	float *X, *X_origin, *Y, *Y_origin, *Z, *Z_origin; 
 
	int *valid_pixels, *valid_pixels_origin;	// index of valid pixels in imDepth 
	int nbValidPixels;		// nb of valid pixels 
 
	// internal images 
	unsigned char *subIm_l,  *subIm_r,  *subIm_t;  
	unsigned char *subIm_l_origin,  *subIm_r_origin,  *subIm_t_origin;  
		 
	unsigned char* corrScore, *corrScoreSec; // corr. scores associated with depth 
	unsigned char* corrScore_origin, *corrScoreSec_origin; // corr. scores associated with depth 
 
	unsigned short *Depth16, *Depth16_origin; // depth and foreground images 
 
	unsigned short **buff16; 
	unsigned short *bigBuffer16, *bigBuffer16_origin; // buffers used for 16-bits use 
	unsigned short *buffTemp, *buffTemp_origin;	// intermediate buffer used for 'sum_row' filtering  (16-bits) 
	 
	// image proc. buffer 
	unsigned char* count_non_null_pixels, *count_non_null_pixels_origin; 
 
	// stereo param 
	int maskSizeX, maskSizeY;		// sizes for rectangle correlation windows 
	float peakValidationThreshold;		 
	unsigned char UNDEFINED_DEPTH; 
 
	unsigned char *imDepth_ref, *imDepth_ref_origin; //*foreground; 
	unsigned char *imDepth,		*imDepth_origin;  
 
	// camera paramteters 
	float focal, baseline, u0, v0; 
	 
 
 
	// sub-pixel disparity estimation 
	void doSubPixel(const unsigned char* depth, const int* idx, float* delta, int nbPoints); 
	void doSubPixel(unsigned char* imDepth_used); 
 
	// 3D reconstruction 
	void doReconstruction(const int* idx, const float* depth_float, int nbPoints, 					  
						   float* X, float* Y, float* Z); 
 
	// internal stereo function 
	void estimateStereo(const unsigned char* iml8_bw,  
						const unsigned char* imr8_bw, 
						const unsigned char* imtop8_bw, 
						int width, int height, 
						int x0, int y0, int windowWidth, int windowHeight, 
						int maskSizeX, int maskSizeY, char minDiff, int nbDepth, 
						unsigned char** buff, int buffStep, // buffStep: dist. between buff[i] and buff[i+1] 
						unsigned char* corrScore, unsigned char* imDepth, 
						int nbPartitions); 
 
	void estimateStereo(const unsigned char* iml8_bw, const unsigned char* imr8_bw,  const unsigned char* imtop8_bw, 
						int width, int height, 
						int maskSizeX, int maskSizeY,  char minDiff, int nbDepth, 
						unsigned char** buff, int buffStep,  
						unsigned char* corrScore, unsigned char* imDepth, int nbPartitions); 
 
	void estimateStereo_sse2(const unsigned char* iml8_bw, const unsigned char* imr8_bw,  const unsigned char* imtop8_bw, 
						int width, int height, 
						int maskSizeX, int maskSizeY,  char minDiff, int nbDepth, 
						unsigned char** buff, int buffStep,  
						unsigned char* corrScore, unsigned char* imDepth, int nbPartitions); 
 
	void estimateStereo_Horiz(const unsigned char* iml8_bw, const unsigned char* imr8_bw,  
							int width, int height, 
							int maskSizeX, int maskSizeY,  char minDiff, int nbDepth, 
							unsigned char** buff, int buffStep,  
							unsigned char* corrScore, unsigned char* imDepth, int nbPartitions); 
 
	void setUndefinedPixels(unsigned char* image); 
}; 
 
#endif