// Algorithm.cpp: implementation of the CAlgorithm class. // ////////////////////////////////////////////////////////////////////// #include #include #include "Algorithm.h" #include "OFPyramid.h" #include "ImagePyramid.h" ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CAlgorithm::CAlgorithm() { FirstSeq = new CImagePyramid(); SecondSeq = new CImagePyramid(); OpticalFlowPyramid = new COFPyramid(); } CAlgorithm::~CAlgorithm() { FirstSeq->~CImagePyramid(); SecondSeq->~CImagePyramid(); OpticalFlowPyramid->~COFPyramid(); } //in1, in2, out, depth, gausssize, Min, Rate CAlgorithm::CAlgorithm(char *FileName1, char *FileName2,char *Out,char *OutColor, int Depth, int GaussSize, int Minimum, int Rate) { FirstSeq = new CImagePyramid(FileName1, Depth, GaussSize); SecondSeq = new CImagePyramid(FileName2, Depth, GaussSize); OpticalFlowPyramid = new COFPyramid(FirstSeq->Pyramid[0]->GetSizeX(),FirstSeq->Pyramid[0]->GetSizeY(), Depth); COFPyramid *OFTemp; OFTemp = new COFPyramid(FirstSeq->Pyramid[0]->GetSizeX(),FirstSeq->Pyramid[0]->GetSizeY(), Depth); Repare = new CGaussMatrix(3, 0.0, 3.0); int i, j, s, x, y, rep_x, rep_y, origrep_x, origrep_y, sx, sy; double tmp[3][3], min, sum; int color1, color2; for (s = Depth-1; s >= 0; s--){ for (i = 0; i < FirstSeq->Pyramid[s]->GetSizeX(); i++){ for (j = 0; j < FirstSeq->Pyramid[s]->GetSizeY(); j++){ //create distance matrix origrep_x = OpticalFlowPyramid->Pyramid[s]->GetXY_X(i,j); origrep_y = OpticalFlowPyramid->Pyramid[s]->GetXY_Y(i,j); //red for (x=0; x<3;x++){ for (y=0; y<3;y++){ sum = 0.0; for (sx=0; sx<3;sx++){ for (sy=0; sy<3;sy++){ color1 = (FirstSeq->Pyramid[s]->GetPixel(i+x-2+sx+origrep_x, j+y-2+sy+origrep_y) & 0x0000FF) + (FirstSeq->Pyramid[s]->GetPixel(i+x-2+sx+origrep_x, j+y-2+sy+origrep_y) & 0x00FF00) + (FirstSeq->Pyramid[s]->GetPixel(i+x-2+sx+origrep_x, j+y-2+sy+origrep_y) & 0xFF0000); color2 = (SecondSeq->Pyramid[s]->GetPixel(i+origrep_x-1+sx, j+origrep_x-1+sy) & 0x0000FF) + (SecondSeq->Pyramid[s]->GetPixel(i+origrep_x-1+sx, j+origrep_x-1+sy) & 0x00FF00) + (SecondSeq->Pyramid[s]->GetPixel(i+origrep_x-1+sx, j+origrep_x-1+sy) & 0xFF0000); sum +=abs (color1-color2); //(FirstSeq->Pyramid[s]->GetPixel(i+x-2+sx+origrep_x, j+y-2+sy+origrep_y) & 0x0000FF) - (SecondSeq->Pyramid[s]->GetPixel(i+origrep_x-1+sx, j+origrep_x-1+sy) & 0x0000FF)); } } tmp[x][y] = 1.0+sum; } } //repare temp matrix with Gauss matrix for (x=0; x<3;x++){ for (y=0; y<3;y++){ tmp[x][y] *= (1-Repare->GetXY(x-1, y-1)); } } //find the smallest absolute value rep_x=0; rep_y=0; //r min = tmp[0][0]; for (x=0; x<3;x++){ for (y=0; y<3;y++){ if (tmp[x][y] <= min){ rep_x=x; rep_y=y; min = tmp[x][y]; } } } OpticalFlowPyramid->Pyramid[s]->SetXY(i,j, origrep_x + rep_x-1,origrep_y+ rep_y-1); } printf("*"); } //smooth //hp double nx, ny; if (s<=2){ for (i = 0; i < FirstSeq->Pyramid[s]->GetSizeX(); i++){ for (j = 0; j < FirstSeq->Pyramid[s]->GetSizeY(); j++){ nx=0.0; ny=0.0; for (x=0; x<3;x++){ for (y=0; y<3;y++){ nx += OpticalFlowPyramid->Pyramid[s]->GetXY_X(i+x-1,j+y-1); ny += OpticalFlowPyramid->Pyramid[s]->GetXY_Y(i+x-1,j+y-1); } } nx /= 9; ny /= 9; OFTemp->Pyramid[s]->SetXY(i,j, (int)Round(nx), (int)Round(ny)); } } for (i = 0; i < FirstSeq->Pyramid[s]->GetSizeX(); i++){ for (j = 0; j < FirstSeq->Pyramid[s]->GetSizeY(); j++){ OpticalFlowPyramid->Pyramid[s]->SetXY(i,j, OFTemp->Pyramid[s]->GetXY_X(i,j), OFTemp->Pyramid[s]->GetXY_Y(i,j)); } } } if (s>0){ for (i = 0; i < FirstSeq->Pyramid[s-1]->GetSizeX(); i++){ for (j = 0; j < FirstSeq->Pyramid[s-1]->GetSizeY(); j++){ origrep_x = OpticalFlowPyramid->Pyramid[s]->GetXY_X(i/2,j/2); origrep_y = OpticalFlowPyramid->Pyramid[s]->GetXY_Y (i/2,j/2); OpticalFlowPyramid->Pyramid[s-1]->SetXY(i,j, origrep_x*2, origrep_y*2); } } } printf("\n%d th step done...\n", s); } OpticalFlowPyramid->Pyramid[0]->SaveToFile(Out, Rate, Minimum); OpticalFlowPyramid->Pyramid[0]->SaveToOFFile("out.of"); OpticalFlowPyramid->Pyramid[0]->SaveToColorFile(OutColor, 2); } double CAlgorithm::Round(double Num) { if ((Num - (int)Num) > 0.5){ return (int) (Num + 1.0); }else{ return (int)(Num); } }