102 #ifndef _RLOFTOOLS_HPP_
103 #define _RLOFTOOLS_HPP_
107 #include <opencv\highgui.h>
117 const cv::Mat & img2,
123 std::vector<CRPoint> ptList1(img1.size().area());
124 std::vector<CRPoint> ptList2(img1.size().area());
127 for(
int r = 0 ; r < img1.rows; r++)
129 for(
int c = 0 ; c < img1.cols; c++, n++)
131 ptList1[n] =
CRPoint(static_cast<float>(c),static_cast<float>(r));
135 tracker->
run(img1.ptr<uchar>(),
146 if( U.size() != img1.size() || U.type() != CV_32FC1)
149 U.create(img1.size(), CV_32FC1);
152 if( V.size() != img1.size() || V.type() != CV_32FC1)
155 V.create(img1.size(), CV_32FC1);
159 for(
unsigned int n = 0; n < ptList1.size(); n++)
161 cv::Point pt(static_cast<int>(ptList1[n].x), static_cast<int>(ptList1[n].y));
162 U.at<
float>(pt) = ptList2[n].x - ptList1[n].x;
163 V.at<
float>(pt) = ptList2[n].y - ptList1[n].y;
183 const float & max_size = -1,
184 bool use_value =
false,
185 cv::Mat & sat = cv::Mat())
187 if(dx.empty() ||dy.empty())
return;
189 if( dx.depth() != CV_32F && dy.depth() != CV_32F)
190 throw(std::exception(
"FlowToRGB: error dx/dy wrong data type"));
191 if( dx.size() != dy.size())
192 throw(std::exception(
"FlowToRGB: error dx/dy must have equal size"));
194 if( sat.type() != CV_8UC1)
195 throw(std::exception(
"FlowToRGB: error sat must have type CV_8UC1"));
198 const float grad2deg = (float)(90/3.141);
200 double satMaxVal, minVal;
203 cv::minMaxLoc(sat, &minVal, &satMaxVal);
204 satMaxVal = 255.0/satMaxVal;
207 cv::Mat pol(dx.size(), CV_32FC2);
209 float mean_val = 0, min_val = 1000, max_val = 0;
210 float _dx, _dy, angle, value;
212 for(
int r = 0; r < dx.rows; r++)
214 for(
int c = 0; c < dy.cols; c++)
216 _dx = dx.at<
float>(r,c);
217 _dy = dy.at<
float>(r,c);
218 value =sqrt(_dx * _dx + _dy * _dy);
222 angle = 180.f - acos(_dx/ value)* grad2deg;
224 angle = acos(_dx/ value)* grad2deg;
227 max_val = MAX(max_val, value);
228 min_val = MIN(min_val, value);
229 pol.at<cv::Point2f>(r,c) = cv::Point2f(angle,value);
232 mean_val /= dy.size().area();
233 float scale = max_val - min_val;
234 float shift = -min_val;
238 scale = 255.f/max_size;
243 cv::Mat hsv(dx.rows, dx.cols, CV_8UC3);
244 uchar * ptrHSV = hsv.ptr<uchar>();
245 int idx_val = (use_value) ? 2:1;
246 int idx_sat = (use_value) ? 1:2;
249 for(
int r = 0; r < dx.rows; r++, ptrHSV += hsv.step1())
251 uchar * _ptrHSV = ptrHSV;
252 for(
int c = 0; c < dy.cols; c++, _ptrHSV+=3)
254 cv::Point2f vpol = pol.at<cv::Point2f>(r,c);
256 _ptrHSV[0] = cv::saturate_cast<uchar>(vpol.x);
257 _ptrHSV[idx_val] = cv::saturate_cast<uchar>( (vpol.y + shift) * scale);
259 _ptrHSV[idx_sat] = 255;
261 _ptrHSV[idx_sat] = 255- cv::saturate_cast<uchar>(sat.at<uchar>(r,c) * satMaxVal);
271 cv::cvtColor(hsv, rgbFlow32F, CV_HSV2BGR);
272 rgbFlow32F.convertTo(rgbFlow, CV_8UC3);
278 std::string filename1(
"../Doc/ErnstReuter1.png");
279 std::string filename2(
"../Doc/ErnstReuter2.png");
283 img1 = cv::imread(filename1);
284 img2 = cv::imread(filename2);
301 CLocalFlowParameter * parameters[8] = {&PLKParameter,&RLOFParameter, &BEPLKParameter, &BERLOFParameter, &CB_BEPLKParameter, &CB_BERLOFParameter, &CB_PLKParameter, &CB_RLOFParameter};
302 std::string methodName[8] = {
"PLK",
"RLOF",
"BEPLK",
"BERLOF",
"CB_BEPLK",
"CB_BERLOF",
"CB_PLK",
"CB_RLOF"};
303 std::cout <<
"--- RLOFLib Accuracy Test ---" << std::endl;
306 #ifdef _WIN32 || _WIN64
308 fopen_s(&file,
"../Doc/RLOFTest64.dat",
"r");
310 fopen_s(&file,
"../Doc/RLOFTest32.dat",
"r");
315 for(
int n = 0; n < 8; n++)
321 for(
int r = 0; r < U.rows; r++)
322 for(
int c = 0; c < U.cols; c++)
325 fscanf(file,
"%f %f %f ",&u, &v, &b);
326 float mu = U.at<
float>(r,c);
327 float mv = V.at<
float>(r,c);
328 if( sqrt((u - mu) * (u - mu) + (v - mv) * (v - mv)) > 0.01 && b != 0)
332 R /=
static_cast<float>(U.size().area());
334 std::cout << methodName[n] <<
"\t\tTEST OK!" << std::endl;
336 std::cout << methodName[n] <<
"\t\tATTENTION R = "<< R << std::endl;
359 CLocalFlowParameter * parameters[8] = {&PLKParameter,&RLOFParameter, &BEPLKParameter, &BERLOFParameter, &CB_BEPLKParameter, &CB_BERLOFParameter, &CB_PLKParameter, &CB_RLOFParameter};
360 std::string methodName[8] = {
"PLK",
"RLOF",
"BEPLK",
"BERLOF",
"CB_BEPLK",
"CB_BERLOF",
"CB_PLK",
"CB_RLOF"};
362 std::cout <<
"--- RLOFLib Initialisation Test ---" << std::endl;
363 for(
int n = 0; n < 8; n++)
369 std::cout << methodName[n] <<
"\t\tTEST OK!" << std::endl;
371 catch(std::exception & e)
373 std::cout << methodName[n] <<
"\t\tTEST FAILED!" << std::endl << e.what() <<std::endl;
Feature tracker parameter base class This class is the base class for all parameter classes in that l...
void solverType(SolverType val)
RLOF parameter class The Robust Local Optical Flow parameter class contains additional parameters use...
Pyramidal Lucas Kanade parameter class The Pyramidal Lucas Kanade parameter class. Please use a instance of this class to run the PLK version. The parameter class is associated to PLK.
static IFeatureTracker * createFTInstance(RLOFType type=CPU_BEPLK)
PLK/RLOF parameter base class The parameter base class provides basic interfaces to set up the algori...
Cross based Pyramidal Lucas Kanade parameter class The cross based Pyramidal Lucas Kanade parameter c...
virtual void run(const uchar *imgPtr1, unsigned int step1, const uchar *imgPtr2, unsigned int step2, unsigned int nRows, unsigned int nCols, std::vector< CRPoint > &prevPoints, std::vector< CRPoint > &currPoints, const FeatureTrackerContext *context)
Cross based RLOF parameter class The cross based Robust Local Optical Flow parameter class contains i...