stereoMatching_algo.cpp
资源名称:estereo2.zip [点击查看]
上传用户:fengshi120
上传日期:2014-07-17
资源大小:6155k
文件大小:15k
源码类别:
3D图形编程
开发平台:
C/C++
- /***************************************************************************
- *
- * Copyright 2000 by David Demirdjian. All rights reserved.
- *
- * Developed by David Demirdjian
- *
- * 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 David Demirdjian. The author makes
- * no representations about the suitability of this software for any purpose.
- * It is provided "as is" without express or implied warranty.
- *
- **************************************************************************/
- #include "stdafx.h"
- #include "stereoMatching.h"
- #include "processingMMX.h"
- // ************************************************************
- // ************************************************************
- // *** Main algorithm for stereo computation
- // ************************************************************
- // ************************************************************
- typedef unsigned char uchar;
- typedef unsigned short ushort;
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #define _PROCESS_AND_COMPARE_SSE2_
- ImgSubandAdd_sse2(imr8_bw , imtop8_bw +(i+k)*width , iml8_bw +(i+k) ,ptBuff , subImageSize);
- sum_Row_mmx(ptBuff , ptBuff16+2*backStep, subImageSize, maskSizeX);
- avg_Col_mmx(ptBuff16+backStep , ptBuff-backStep,subImageSize, width, maskSizeY);
- copyMMX(ptBuff16, ptBuff16+subImageSize, 2*backStep*sizeof(short));
- findMinimumCorrelation_mmx(ptBuff-backStep, i+k, imDepth8u+offset-backStep,
- corrScore+offset-backStep, corrScoreSec+offset-backStep,
- subImageSize);
- void StereoMatching::estimateStereo(const uchar* iml8_bw,
- const uchar* imr8_bw,
- const uchar* imtop8_bw,
- int width, int height, int maskSizeX, int maskSizeY,
- char minDiff, int nbDepth, int nbPartitions,
- StereoBuffer* sb, StereoImage* sdata)
- {
- // assure good partitioning (.....)
- while (height/nbPartitions - 2*(maskSizeY/2) <=0) {
- nbPartitions /= 2;
- }
- int i,j,k;
- int subImageSize=width*height/nbPartitions;
- int backStep = (maskSizeY/2)*width;
- uchar* ptBuff;
- ushort* ptBuff16;
- int siz;
- // ----------- process the first sub-images -----------
- for (i=0; i<nbDepth; i+=8) {
- ptBuff = sb->buff[i];
- ImgSubandAdd2(imr8_bw , imtop8_bw +i*width , iml8_bw +i , ptBuff , subImageSize, sb->buffStep, width);
- for (k=0; k<8; ++k)
- {
- ptBuff = sb->buff[i+k];
- ptBuff16 = sb->buff16[i+k];
- siz = __max(0, subImageSize - 2*backStep);
- sum_Row_mmx(ptBuff , ptBuff16 , subImageSize,maskSizeX);
- avg_Col_mmx(ptBuff16+backStep, ptBuff+backStep, siz, width, maskSizeY);
- copyMMX(ptBuff16,ptBuff16+subImageSize-2*backStep,2*backStep*sizeof(short));
- if (i==0 && k==0)
- initMinimumCorrelation(ptBuff, 0, sdata->imDepth8u, sb->corrScore, sb->corrScoreSec, subImageSize);
- else
- findMinimumCorrelation_mmx(ptBuff+backStep, i+k, sdata->imDepth8u+backStep,
- sb->corrScore+backStep, sb->corrScoreSec+backStep,
- subImageSize-backStep);
- }
- }
- // check that there are enough diff. between best and second disp
- compareBestAndSecond(sb->corrScore+backStep, sb->corrScoreSec+backStep, minDiff,
- sdata->UNDEFINED_DEPTH, sdata->imDepth8u+backStep, subImageSize-backStep);
- int offset=subImageSize;
- // ----------- process the other ones -----------
- for (j=1; j<nbPartitions; ++j, offset+=subImageSize)
- {
- // process depth i=0
- i=0;
- ptBuff = sb->buff[0]+offset;
- ptBuff16 = sb->buff16[0];
- // estimate absolute differences between the 3 stereo images
- ImgSubandAdd2(imr8_bw +offset, imtop8_bw + i*width +offset, iml8_bw + i +offset,
- ptBuff, subImageSize, sb->buffStep, width);
- // ptBuff16 data from [0] to [backStep] come from previous iter
- // sum_Row_mmx fills ptBuff16 from [2*backStep] to [2*backStep+subImageSize]
- sum_Row_mmx(ptBuff , ptBuff16+2*backStep, subImageSize, maskSizeX);
- // avg_Col_mmx process data in ptBuff16 from [0] to [subImageSize]
- avg_Col_mmx(ptBuff16+backStep , ptBuff-backStep,subImageSize, width, maskSizeY);
- // then copy ptBuff16 data [subImageSize] to [2*backStep+subImageSize]
- // to [0] to [2*backStep]
- copyMMX(ptBuff16, ptBuff16+subImageSize, 2*backStep*sizeof(short));
- // find minimum SAD score
- initMinimumCorrelation(ptBuff-backStep, 0, sdata->imDepth8u+offset-backStep,
- sb->corrScore+offset-backStep, sb->corrScoreSec+offset-backStep, subImageSize);
- // do stuff 7 times
- for (k=1; k<=7; ++k) {
- ptBuff+=sb->buffStep; ptBuff16+=sb->buffStep;
- sb->filterSAD(maskSizeX, maskSizeY, ptBuff, ptBuff16,
- backStep, subImageSize, offset);
- findMinimumCorrelation_mmx(ptBuff-backStep, i+k, sdata->imDepth8u+offset-backStep,
- sb->corrScore+offset-backStep,
- sb->corrScoreSec+offset-backStep,
- subImageSize);
- }
- for (i=8; i<nbDepth; i+=8) {
- ptBuff = sb->buff[i]+offset;
- ptBuff16 = sb->buff16[i];
- // estimate absolute differences between the 3 stereo images
- ImgSubandAdd2(imr8_bw +offset, imtop8_bw + i*width +offset, iml8_bw + i +offset,
- ptBuff, subImageSize, sb->buffStep, width);
- // process and comparison
- for (k=0; k<=7; ++k) {
- sb->filterSAD(maskSizeX, maskSizeY, ptBuff, ptBuff16, backStep,
- subImageSize, offset);
- findMinimumCorrelation_mmx(ptBuff-backStep, i+k, sdata->imDepth8u+offset-backStep,
- sb->corrScore+offset-backStep,
- sb->corrScoreSec+offset-backStep,
- subImageSize);
- ptBuff+=sb->buffStep; ptBuff16+=sb->buffStep;
- }
- }
- // check that there are enough diff. between best and second disp
- compareBestAndSecond(sb->corrScore+offset-backStep, sb->corrScoreSec+offset-backStep, minDiff,
- sdata->UNDEFINED_DEPTH, sdata->imDepth8u+offset-backStep, subImageSize);
- }
- }
- void StereoMatching::estimateStereo_sse2(const uchar* iml8_bw,
- const uchar* imr8_bw,
- const uchar* imtop8_bw,
- int width, int height, int maskSizeX, int maskSizeY,
- char minDiff, int nbDepth, int nbPartitions,
- StereoBuffer* sb, StereoImage* sdata)
- {
- // assure good partitioning (.....)
- while (height/nbPartitions - 2*(maskSizeY/2) <=0)
- {
- nbPartitions /= 2;
- }
- int i,j;
- int subImageSize=width*height/nbPartitions;
- int backStep = (maskSizeY/2)*width;
- int buffStep16 = sb->buffStep;
- uchar* ptBuff;
- ushort* ptBuff16;
- int siz;
- // ----------- process the first sub-images -----------
- for (i=0; i<nbDepth; i++)
- {
- ptBuff = sb->buff[i];
- ptBuff16 = sb->buff16[i];
- ImgSubandAdd_sse2(imr8_bw , imtop8_bw +i*width , iml8_bw +i ,ptBuff , subImageSize);
- siz = __max(0, subImageSize - 2*backStep);
- sum_Row_mmx(ptBuff , ptBuff16 , subImageSize,maskSizeX);
- avg_Col_sse2(ptBuff16+backStep, ptBuff+backStep, siz, width, maskSizeY);
- copySSE(ptBuff16,ptBuff16+subImageSize-2*backStep,2*backStep*sizeof(short));
- if (i==0)
- initMinimumCorrelation(ptBuff, 0, sdata->imDepth8u, sb->corrScore, sb->corrScoreSec, subImageSize);
- else
- findMinimumCorrelation_mmx(ptBuff+backStep, i, sdata->imDepth8u+backStep,
- sb->corrScore+backStep, sb->corrScoreSec+backStep,
- subImageSize-backStep);
- }
- // check that there are enough diff. between best and second disp
- compareBestAndSecond(sb->corrScore+backStep, sb->corrScoreSec+backStep, minDiff,
- sdata->UNDEFINED_DEPTH, sdata->imDepth8u+backStep, subImageSize-backStep);
- int offset=subImageSize;
- // ----------- process the other ones -----------
- for (j=1; j<nbPartitions; ++j, offset+=subImageSize)
- {
- for (i=0; i<nbDepth; i++)
- {
- ptBuff = sb->buff[i]+offset;
- ptBuff16 = sb->buff16[i];
- // process and comparison
- ImgSubandAdd_sse2(imr8_bw+offset , imtop8_bw+offset +i*width , iml8_bw+offset +i ,ptBuff , subImageSize);
- // filtering
- sum_Row_5_mmx(ptBuff , ptBuff16+2*backStep, subImageSize); //, maskSizeX);
- avg_Col_sse2(ptBuff16+backStep , ptBuff-backStep, subImageSize, width, maskSizeY);
- copySSE(ptBuff16, ptBuff16+subImageSize, 2*backStep*sizeof(short));
- if (i==0)
- initMinimumCorrelation(ptBuff-backStep, 0, sdata->imDepth8u+offset-backStep, sb->corrScore+offset-backStep,
- sb->corrScoreSec+offset-backStep, subImageSize);
- else
- findMinimumCorrelation_mmx(ptBuff-backStep, i, sdata->imDepth8u+offset-backStep,
- sb->corrScore+offset-backStep, sb->corrScoreSec+offset-backStep, subImageSize);
- }
- // check that there are enough diff. between best and second disp
- compareBestAndSecond(sb->corrScore+offset-backStep, sb->corrScoreSec+offset-backStep, minDiff,
- sdata->UNDEFINED_DEPTH, sdata->imDepth8u+offset-backStep, subImageSize);
- }
- }
- void StereoMatching::estimateStereo_Horiz(const uchar* iml8_bw, const uchar* imr8_bw,
- int width, int height, int maskSizeX, int maskSizeY,
- char minDiff, int nbDepth, int nbPartitions,
- StereoBuffer* sb, StereoImage* sdata)
- {
- int i,k;
- int imageSize=width*height;
- int backStep = (maskSizeY/2)*width;
- uchar* ptBuff;
- ushort* ptBuff16;
- int siz = 8*(__max(0, imageSize - 2*backStep)/8);
- for (i=0; i<nbDepth; i+=8) {
- ptBuff = sb->buff[i];
- // compute SAD for (imr8_bw, iml8_bw+i), (imr8_bw, iml8_bw+i+1), ....
- // .. (imr8_bw, iml8_bw+i+7)
- ImgSubandAdd_Horiz(imr8_bw, iml8_bw+i, ptBuff, imageSize, sb->buffStep, width);
- for (k=0; k<8; ++k) {
- ptBuff = sb->buff[i+k];
- ptBuff16 = sb->buff16[i+k];
- // apply maskSizeX x maskSizeY average mask
- //sum_Row(ptBuff , ptBuff16, imageSize,maskSizeX);
- //avg_Col(ptBuff16 +backStep, ptBuff +backStep, siz, width, maskSizeY);
- sum_Row_mmx(ptBuff , ptBuff16, imageSize,maskSizeX);
- avg_Col_mmx(ptBuff16 +backStep, ptBuff +backStep, siz, width, maskSizeY);
- // translate data
- copyMMX(ptBuff16,ptBuff16+siz,2*backStep*sizeof(short));
- if (i==0 && k==0)
- initMinimumCorrelation(ptBuff, 0, sdata->imDepth8u, sb->corrScore,
- sb->corrScoreSec, imageSize);
- else
- /*findMinimumCorrelation(ptBuff+backStep, i+k,
- sdata->imDepth8u+backStep,
- sb->corrScore+backStep, sb->corrScoreSec+backStep,
- siz);*/
- findMinimumCorrelation_mmx(ptBuff+backStep, i+k,
- sdata->imDepth8u+backStep,
- sb->corrScore+backStep, sb->corrScoreSec+backStep,
- siz);
- }
- }
- // check that there are enough diff. between best and second disp
- compareBestAndSecond(sb->corrScore+backStep, sb->corrScoreSec+backStep, minDiff,
- sdata->UNDEFINED_DEPTH, sdata->imDepth8u+backStep, siz);
- }
- void StereoMatching::estimateStereo_Horiz_mmx(const uchar* iml8_bw, const uchar* imr8_bw,
- int width, int height, int maskSizeX, int maskSizeY,
- char minDiff, int nbDepth, int nbPartitions,
- StereoBuffer* sb, StereoImage* sdata)
- {
- // assure good partitioning (.....)
- while (height/nbPartitions - 2*(maskSizeY/2) <=0)
- {
- nbPartitions /= 2;
- }
- int i,j,k;
- int subImageSize=width*height/nbPartitions;
- int backStep = (maskSizeY/2)*width;
- uchar* ptBuff;
- ushort* ptBuff16;
- int siz = __max(0, subImageSize - 2*backStep);
- // ----------- process the first sub-images -----------
- for (i=0; i<nbDepth; i+=8) {
- ptBuff = sb->buff[i];
- // compute SAD for (imr8_bw, iml8_bw+i), (imr8_bw, iml8_bw+i+1), ....
- // .. (imr8_bw, iml8_bw+i+7)
- ImgSubandAdd_Horiz(imr8_bw, iml8_bw+i, ptBuff, subImageSize, sb->buffStep, width);
- for (k=0; k<8; ++k) {
- ptBuff = sb->buff[i+k];
- ptBuff16 = sb->buff16[i+k];
- // apply maskSizeX x maskSizeY avergae mask
- sum_Row_mmx(ptBuff , ptBuff16, subImageSize,maskSizeX);
- avg_Col_mmx(ptBuff16 +backStep, ptBuff +backStep, siz, width, maskSizeY);
- // translate data
- copyMMX(ptBuff16,ptBuff16+subImageSize-2*backStep,2*backStep*sizeof(short));
- if (i==0 && k==0)
- initMinimumCorrelation(ptBuff, 0, sdata->imDepth8u, sb->corrScore,
- sb->corrScoreSec, subImageSize);
- else
- findMinimumCorrelation_mmx(ptBuff+backStep, i+k,
- sdata->imDepth8u+backStep,
- sb->corrScore+backStep, sb->corrScoreSec+backStep,
- subImageSize-backStep);
- }
- }
- // check that there are enough diff. between best and second disp
- compareBestAndSecond(sb->corrScore+backStep, sb->corrScoreSec+backStep, minDiff,
- sdata->UNDEFINED_DEPTH, sdata->imDepth8u+backStep, subImageSize-backStep);
- int offset=subImageSize;
- // ----------- process the other ones -----------
- for (j=1; j<nbPartitions; ++j, offset+=subImageSize) {
- // process depth i=0
- i=0;
- ptBuff = sb->buff[0]+offset;
- // estimate absolute differences between the stereo images
- ImgSubandAdd_Horiz(imr8_bw +offset, iml8_bw +i +offset, ptBuff,
- subImageSize, sb->buffStep, width);
- ptBuff16 = sb->buff16[0];
- sb->filterSAD(maskSizeX, maskSizeY, ptBuff, ptBuff16,
- backStep, subImageSize, offset);
- initMinimumCorrelation(ptBuff-backStep, 0, sdata->imDepth8u+offset-backStep,
- sb->corrScore+offset-backStep,
- sb->corrScoreSec+offset-backStep, subImageSize);
- // process and comparison
- for (int k=1; k<=7; ++k) {
- ptBuff +=sb->buffStep;
- ptBuff16+=sb->buffStep;
- sb->filterSAD(maskSizeX, maskSizeY, ptBuff, ptBuff16,
- backStep, subImageSize, offset);
- findMinimumCorrelation_mmx(ptBuff-backStep, i+k, sdata->imDepth8u+offset-backStep,
- sb->corrScore+offset-backStep, sb->corrScoreSec+offset-backStep,
- subImageSize);
- }
- for (i=8; i<nbDepth; i+=8) {
- ptBuff = sb->buff[i]+offset;
- // estimate absolute differences between the 3 stereo images
- ImgSubandAdd_Horiz(imr8_bw +offset, iml8_bw + i +offset, ptBuff,
- subImageSize, sb->buffStep, width);
- ptBuff16 = sb->buff16[i];
- // process and comparison
- for (int k=0; k<=7; ++k) {
- ptBuff +=sb->buffStep;
- ptBuff16+=sb->buffStep;
- sb->filterSAD(maskSizeX, maskSizeY, ptBuff, ptBuff16,
- backStep, subImageSize, offset);
- findMinimumCorrelation_mmx(ptBuff-backStep, i+k, sdata->imDepth8u+offset-backStep,
- sb->corrScore+offset-backStep,
- sb->corrScoreSec+offset-backStep,
- subImageSize);
- }
- }
- // check that there are enough diff. between best and second disp
- compareBestAndSecond(sb->corrScore+offset-backStep,
- sb->corrScoreSec+offset-backStep, minDiff,
- sdata->UNDEFINED_DEPTH,
- sdata->imDepth8u+offset-backStep, subImageSize);
- }
- }