#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 00046 //you have to start with the smallest image (top of the pyramid) 00047 00048 HxImageRep ix,iy,it; //derivatives on x, y and time 00049 HxImageRep ix_1,iy_1,it_1; //derivatives on x, y and time 00050 HxImageRep ix_2,iy_2,it_2; //derivatives on x, y and time 00051 00052 ix_2 = im1_2.generalizedConvolutionK1d(1, kernel,"mul", "addAssign", HxImageRep::ARITH_PREC); 00053 iy_2 = im1_2.generalizedConvolutionK1d(2, kernel,"mul", "addAssign", HxImageRep::ARITH_PREC); 00054 it_2 = HxSub(im2_2,im1_2); 00055 00056 00057 HxImageRep res2 = HxOFOneScale(ix_2,iy_2,it_2); 00058 00059 res2 = HxScale(res2, 2, 2, 1, LINEAR, 1); 00060 res2 = HxMulVal(res2,2); 00061 00062 ix_1 = im1_1.generalizedConvolutionK1d(1, kernel,"mul", "addAssign", HxImageRep::ARITH_PREC); 00063 iy_1 = im1_1.generalizedConvolutionK1d(2, kernel,"mul", "addAssign", HxImageRep::ARITH_PREC); 00064 //estimate now the image at time t+1 by motion in smallar image 00065 it_1 = HxSub(im2_2,HxAdd(im1_1,res2)); 00066 00067 HxImageRep res1 = HxOFOneScale(ix_1,iy_1,it_1); 00068 00069 res1 = HxScale(res1, 2, 2, 1, LINEAR, 1); 00070 res1 = HxMulVal(res1,2); 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 00077 /* 00078 HxWriteFile(ix,"ix.tif"); 00079 HxWriteFile(iy,"iy.tif"); 00080 HxWriteFile(it,"it.tif"); 00081 */ 00082 00083 HxImageRep res = HxOFOneScale(ix,iy,it); 00084 00085 //export now the two components to vizualize with Hieu's program 00086 /* 00087 HxImageRep u = HxProjectRange(res,1); 00088 HxImageRep v = HxProjectRange(res,2); 00089 00090 // u = HxAbs(u); 00091 // v = HxAbs(v); 00092 double umin = HxPixMax(u).HxScalarDoubleValue().x(); 00093 double vmin = HxPixMax(v).HxScalarDoubleValue().x(); 00094 00095 u = HxAddVal(u,fabs(umin)); 00096 v = HxAddVal(v,fabs(vmin)); 00097 00098 double umax = HxPixMax(HxAbs(u)).HxScalarDoubleValue().x(); 00099 double vmax = HxPixMax(HxAbs(v)).HxScalarDoubleValue().x(); 00100 HxWriteFile(HxImageAsByte(HxMulVal(u,255.0/umax)),"u1.tif"); 00101 HxWriteFile(HxImageAsByte(HxMulVal(v,255.0/vmax)),"v1.tif"); 00102 00103 */ 00104 return res; 00105 } |