#include "HxImageRep.h"
Go to the source code of this file.
Functions | |
HxImageRep L_HXIMAGEREP | HxOpticalFlowMultiScale (HxImageRep im1, HxImageRep im2) |
implement Lucas and Kanade's method for optical flow detection then go for Bergen - multiscale the output is a HxVec2DDouble image where the displacement x and y values are exported. More... |
|
implement Lucas and Kanade's method for optical flow detection then go for Bergen - multiscale the output is a HxVec2DDouble image where the displacement x and y values are exported.
00017 { 00018 //to compute the image derivatives 00019 //for x and y use the coefficients: 1/12(-1,8,0,-8,1) 00020 int pixelDimensionality=1; 00021 int dimensions=2; 00022 HxSizes sizes(5,1,1); 00023 //the data pointer for Hx should have 3 shorts per pixels? 00024 double data[]={ -1.0/12, 8.0/12, 0, -8.0/12, 1.0/12 }; 00025 00026 HxImageRep kernel = HxMakeFromDoubleData(pixelDimensionality, dimensions, sizes, data); 00027 00028 00029 //create the pyramid images 00030 double sigma = 1.5; 00031 HxImageRep im1_1=HxGauss(im1,sigma,HxImageRep::ARITH_PREC); 00032 HxImageRep im2_1=HxGauss(im2,sigma,HxImageRep::ARITH_PREC); 00033 im1_1 = HxScale(im1_1, 0.5, 0.5, 1, NEAREST, 1); //NEAREST 00034 im2_1 = HxScale(im2_1, 0.5, 0.5, 1, NEAREST, 1); 00035 00036 00037 // sigma = 2.0; 00038 HxImageRep im1_2=HxGauss(im1_1,sigma,HxImageRep::ARITH_PREC); 00039 HxImageRep im2_2=HxGauss(im2_1,sigma,HxImageRep::ARITH_PREC); 00040 // im1_2 = HxScale(im1_2, 0.25, 0.25, 1, LINEAR, 1); 00041 // im2_2 = HxScale(im2_2, 0.25, 0.25, 1, LINEAR, 1); 00042 im1_2 = HxScale(im1_2, 0.5, 0.5, 1, NEAREST, 1); 00043 im2_2 = HxScale(im2_2, 0.5, 0.5, 1, NEAREST, 1); 00044 00045 //you have to start with the smallest image (top of the pyramid) 00046 00047 HxImageRep ix,iy,it; //derivatives on x, y and time 00048 HxImageRep ix_1,iy_1,it_1; //derivatives on x, y and time 00049 HxImageRep ix_2,iy_2,it_2; //derivatives on x, y and time 00050 00051 ix_2 = im1_2.generalizedConvolutionK1d(1, kernel,"mul", "addAssign", HxImageRep::ARITH_PREC); 00052 iy_2 = im1_2.generalizedConvolutionK1d(2, kernel,"mul", "addAssign", HxImageRep::ARITH_PREC); 00053 it_2 = HxSub(im2_2,im1_2); 00054 00055 HxImageRep res2 = HxOFOneScale(ix_2,iy_2,it_2); 00056 00057 res2 = HxScale(res2, 2.0, 2.0, 1, LINEAR, 1); 00058 res2 = HxMulVal(res2,2.0); 00059 00060 ix_1 = im1_1.generalizedConvolutionK1d(1, kernel,"mul", "addAssign", HxImageRep::ARITH_PREC); 00061 iy_1 = im1_1.generalizedConvolutionK1d(2, kernel,"mul", "addAssign", HxImageRep::ARITH_PREC); 00062 //estimate now the image at time t+1 by motion in smallar image 00063 // used to be: it_1 = HxSub(im2_2,HxAdd(im1_1,res2)); 00064 // but size and pixeldimensionalities don't match 00065 it_1 = HxSub(im2_1,HxAdd(im1_1,HxNorm2(res2))); 00066 00067 HxImageRep res1 = HxOFOneScale(ix_1,iy_1,it_1); 00068 00069 res1 = HxScale(res1, 2.0, 2.0, 1, LINEAR, 1); 00070 res1 = HxMulVal(res1,2.0); 00071 00072 ix = im1.generalizedConvolutionK1d(1, kernel,"mul", "addAssign", HxImageRep::ARITH_PREC); 00073 iy = im1.generalizedConvolutionK1d(2, kernel,"mul", "addAssign", HxImageRep::ARITH_PREC); 00074 //estimate now the image at time t+1 by motion in smallar image 00075 // it = HxSub(im2,HxAdd(im1,res1)); 00076 it = HxSub(im2,HxAdd(im1,HxNorm2(res1))); 00077 00078 // HxWriteFile(ix,"ix.tif"); 00079 // HxWriteFile(iy,"iy.tif"); 00080 // HxWriteFile(it,"it.tif"); 00081 00082 HxImageRep res = HxOFOneScale(ix,iy,it); 00083 00084 //export now the two components to vizualize with Hieu's program 00085 /* 00086 HxImageRep u = HxProjectRange(res,1); 00087 HxImageRep v = HxProjectRange(res,2); 00088 00089 // u = HxAbs(u); 00090 // v = HxAbs(v); 00091 double umin = HxPixMax(u).HxScalarDoubleValue().x(); 00092 double vmin = HxPixMax(v).HxScalarDoubleValue().x(); 00093 00094 u = HxAddVal(u,fabs(umin)); 00095 v = HxAddVal(v,fabs(vmin)); 00096 00097 double umax = HxPixMax(HxAbs(u)).HxScalarDoubleValue().x(); 00098 double vmax = HxPixMax(HxAbs(v)).HxScalarDoubleValue().x(); 00099 HxWriteFile(HxImageAsByte(HxMulVal(u,255.0/umax)),"u1.tif"); 00100 HxWriteFile(HxImageAsByte(HxMulVal(v,255.0/vmax)),"v1.tif"); 00101 00102 */ 00103 return res; 00104 } |