changeset 399:c389fae9689a

Added a class to read list of image instead of video. This is controlled by the use of the database-filename and folder-data parameters in the config file.
author Jean-Philippe Jodoin <jpjodoin@gmail.com>
date Mon, 29 Jul 2013 17:12:45 -0400
parents 3399bd48cb40
children 7ef1071e3cc3
files c/InputFrameListModule.cpp c/InputVideoFileModule.cpp c/Parameters.cpp c/feature-based-tracking.cpp c/utils.cpp include/InputFrameListModule.h include/InputFrameProviderIface.h include/InputVideoFileModule.h include/Motion.hpp include/Parameters.hpp trafficintelligence.vcxproj
diffstat 11 files changed, 253 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/c/InputFrameListModule.cpp	Mon Jul 29 17:12:45 2013 -0400
@@ -0,0 +1,73 @@
+#include "InputFrameListModule.h"
+#include <fstream>
+#include <ostream>
+
+#include "opencv2/core/core.hpp"
+#include "opencv2/highgui/highgui.hpp"
+
+InputFrameListModule::InputFrameListModule(const std::string& basePath, const std::string& pictureList)
+: mInit(false)
+, mBasePath(basePath+"/")
+, mCurrentIdx(0)
+{
+	loadFileList(pictureList);
+}
+InputFrameListModule::~InputFrameListModule()
+{
+
+}
+
+
+
+bool InputFrameListModule::getNextFrame(cv::Mat& mat)
+{
+	bool success = false;
+	if(mCurrentIdx < mFileList.size())
+	{
+		const std::string& fileName = mBasePath+mFileList[mCurrentIdx++];
+		mCurrentFrame = cv::imread(fileName);
+
+		if(!mCurrentFrame.empty())
+			success = true;
+		mat = mCurrentFrame;
+	}
+
+	
+	return success;
+}
+
+
+
+
+
+unsigned int InputFrameListModule::getNbFrames()
+{
+	return mFileList.size();
+}
+
+void InputFrameListModule::loadFileList(const std::string& path)
+{
+	std::ifstream inputFile(mBasePath+path.c_str());
+	std::string fileContains;
+	if (inputFile.is_open())
+	{
+
+		std::string str;					
+		while( !inputFile.eof() )
+		{
+			std::getline(inputFile, str);
+			if (str.empty()) 
+				break;
+			if (str.at(0) == '#' ) 
+				continue; /* comment */
+			mFileList.push_back(str);
+		}
+		
+		if(!mFileList.empty())
+		{
+			cv::Mat tmpImg = cv::imread(mBasePath+mFileList[0]);
+			mSize = cv::Size(tmpImg.cols, tmpImg.rows);
+			mInit = true;
+		}
+	}
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/c/InputVideoFileModule.cpp	Mon Jul 29 17:12:45 2013 -0400
@@ -0,0 +1,32 @@
+#include "InputVideoFileModule.h"
+
+InputVideoFileModule::InputVideoFileModule(const std::string& videoPath) 
+: mInit(false)
+, mNumberOfFrame(0)
+{
+	mInit = mVideoCapture.open(videoPath.c_str());
+	double frameCount;
+	frameCount = mVideoCapture.get(CV_CAP_PROP_FRAME_COUNT);	
+	mSize = cv::Size(mVideoCapture.get(CV_CAP_PROP_FRAME_WIDTH), mVideoCapture.get(CV_CAP_PROP_FRAME_HEIGHT));
+	mNumberOfFrame = (unsigned int)frameCount;
+}
+
+InputVideoFileModule::~InputVideoFileModule(void)
+{
+
+}
+
+
+
+
+bool InputVideoFileModule::getNextFrame(cv::Mat& outputPicture)
+{
+	bool success = false;
+	if(mInit)
+	{		
+		mVideoCapture >> outputPicture;				
+		success = !outputPicture.empty();
+	}
+	return success;
+}
+
--- a/c/Parameters.cpp	Mon Jul 29 13:46:07 2013 -0400
+++ b/c/Parameters.cpp	Mon Jul 29 17:12:45 2013 -0400
@@ -35,6 +35,8 @@
     ("video-fps", po::value<float>(&videoFPS), "original video frame rate")
     ("frame1", po::value<unsigned int>(&frame1), "first frame to process")
     ("nframes", po::value<int>(&nFrames), "number of frame to process")
+	("list-filename", po::value<string>(&listFilename), "filename of the list of image files")
+	("folder-data", po::value<string>(&folderData), "folder where video or list file is placed")	
     // feature tracking
     ("max-nfeatures", po::value<int>(&maxNFeatures), "maximum number of features added at each frame")
     ("feature-quality", po::value<float>(&featureQuality), "quality level of the good features to track")
--- a/c/feature-based-tracking.cpp	Mon Jul 29 13:46:07 2013 -0400
+++ b/c/feature-based-tracking.cpp	Mon Jul 29 17:12:45 2013 -0400
@@ -17,6 +17,10 @@
 #include <boost/shared_ptr.hpp>
 #include <boost/foreach.hpp>
 
+#include "InputVideoFileModule.h"
+#include "InputFrameListModule.h"
+
+
 #include <iostream>
 #include <vector>
 #include <ctime>
@@ -78,28 +82,27 @@
   // BruteForceMatcher<Hamming> descMatcher;
   // vector<DMatch> matches;
 
-  VideoCapture capture;
-  Size videoSize;
-  unsigned int nFrames = 0;
-  capture.open(params.videoFilename);
-  if(capture.isOpened()) {
-    videoSize = Size(capture.get(CV_CAP_PROP_FRAME_WIDTH), capture.get(CV_CAP_PROP_FRAME_HEIGHT));
-    nFrames = capture.get(CV_CAP_PROP_FRAME_COUNT);
-    cout << "Video " << params.videoFilename <<
-      ": width=" << videoSize.width <<
-      ", height=" << videoSize.height <<
-      ", nframes=" << nFrames << endl;
-  } else {
-    cout << "Video filename " << params.videoFilename << " could not be opened. Exiting." << endl;
-    exit(0);
+  InputFrameProviderIface* capture = nullptr;
+  if(!params.listFilename.empty() && !params.folderData.empty())
+	  capture = new InputFrameListModule(params.folderData, params.listFilename);
+  else if(!params.videoFilename.empty())
+	 capture = new InputVideoFileModule(params.videoFilename);
+  else
+	  cout << "No valid input parameters";
+
+  if(!capture->isOpen())
+  {
+	  cout << "Video filename " << params.videoFilename << " could not be opened. Exiting." << endl;
+	  exit(0);
   }
-  // if (!capture.isOpened())
-  //   {
-  //     //help(argv);
-  //     cout << "capture device " << argv[1] << " failed to open!" << endl;
-  //     return 1;
-  //   }
-
+  
+  Size videoSize = capture->getSize();
+  unsigned int nFrames = capture->getNbFrames();
+  cout << "Video " << params.videoFilename <<
+	  ": width=" << videoSize.width <<
+	  ", height=" << videoSize.height <<
+	  ", nframes=" << nFrames << endl;
+  
   Mat mask = imread(params.maskFilename, 0);
   if (mask.empty()) {
     cout << "Mask filename " << params.maskFilename << " could not be opened." << endl;
@@ -135,9 +138,9 @@
   
   //capture.set(CV_CAP_PROP_POS_FRAMES, params.frame1);
   for (unsigned int frameNum = params.frame1; (frameNum < lastFrameNum) && !::interruptionKey(key); frameNum++) {
-      capture >> frame;
+      bool success = capture->getNextFrame(frame);
 
-      if (frame.empty() || frame.size() != videoSize)
+      if (!success || frame.empty() || frame.size() != videoSize)
 	break;
 
       if (frameNum%50 ==0)
@@ -243,6 +246,8 @@
   
   trajectoryDB->endTransaction();
   trajectoryDB->disconnect();
+  delete capture;
+
 }
 
 void groupFeatures(const KLTFeatureTrackingParameters& params) {
--- a/c/utils.cpp	Mon Jul 29 13:46:07 2013 -0400
+++ b/c/utils.cpp	Mon Jul 29 17:12:45 2013 -0400
@@ -41,7 +41,7 @@
     getline(f, s);
   }
   
-  if (s[0] == ::commentChar)
+  if (!s.empty() && s[0] == ::commentChar)
     s.clear();
   return s;
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/InputFrameListModule.h	Mon Jul 29 17:12:45 2013 -0400
@@ -0,0 +1,41 @@
+#ifndef INPUT_FRAME_LIST_MODULE_H
+#define INPUT_FRAME_LIST_MODULE_H
+
+#include "InputFrameProviderIface.h"
+#include <string>
+#include <vector>
+
+class InputFrameListModule : public InputFrameProviderIface
+{
+public:
+	InputFrameListModule(const std::string& basePath,const std::string& pictureList);
+	~InputFrameListModule();
+
+
+
+	bool getNextFrame(cv::Mat&);
+	unsigned int getNbFrames();
+	bool isOpen() const { return mInit;}
+
+
+
+
+
+
+
+
+
+
+	virtual const cv::Size& getSize() const { return mSize;}
+private:
+	void loadFileList(const std::string& path);
+	std::vector<std::string> mFileList;
+	int mCurrentIdx;
+	bool mInit;
+	std::string mBasePath;
+	cv::Mat mCurrentFrame;
+	cv::Size mSize;
+
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/InputFrameProviderIface.h	Mon Jul 29 17:12:45 2013 -0400
@@ -0,0 +1,23 @@
+#ifndef INPUT_FRAME_PROVIDER_IFACE_H
+#define INPUT_FRAME_PROVIDER_IFACE_H
+
+#include "opencv2/core/core.hpp"
+#include <string>
+
+
+class InputFrameProviderIface
+{
+public: 
+	
+	virtual ~InputFrameProviderIface(){}
+	virtual bool getNextFrame(cv::Mat&)=0;
+	virtual unsigned int getNbFrames() = 0;	
+	virtual bool isOpen() const = 0;
+	virtual const cv::Size& getSize() const = 0;
+
+
+
+};
+
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/InputVideoFileModule.h	Mon Jul 29 17:12:45 2013 -0400
@@ -0,0 +1,37 @@
+#ifndef INPUT_VIDEO_FILE_MODULE_H
+#define INPUT_VIDEO_FILE_MODULE_H
+
+#include "InputFrameProviderIface.h"
+#include <string>
+#include "opencv2/core/core.hpp"
+#include "opencv2/highgui/highgui.hpp"
+
+class InputVideoFileModule : public InputFrameProviderIface
+{
+public:
+	InputVideoFileModule(const std::string& videoPath);
+	~InputVideoFileModule();
+
+
+	bool isOpen() const { return mInit;}
+	const cv::Size& getSize() const { return mSize;}
+
+
+
+	
+
+	bool getNextFrame(cv::Mat&);
+
+	unsigned int getNbFrames(){		return mNumberOfFrame;}
+
+
+	
+	
+private:
+	cv::Size mSize;
+	cv::VideoCapture mVideoCapture;
+	bool mInit;
+	int mNumberOfFrame;
+};
+
+#endif
\ No newline at end of file
--- a/include/Motion.hpp	Mon Jul 29 13:46:07 2013 -0400
+++ b/include/Motion.hpp	Mon Jul 29 17:12:45 2013 -0400
@@ -5,6 +5,7 @@
 #include <boost/shared_ptr.hpp>
 #include <boost/graph/adjacency_list.hpp>
 
+
 template<typename T> class TrajectoryDBAccess;
 template<typename T> class TrajectoryDBAccessList;
 
@@ -13,7 +14,8 @@
 /** Class for feature data
     positions, velocities and other statistics to evaluate their quality
     before saving. */
-class FeatureTrajectory {
+class FeatureTrajectory 
+{
 public:
   FeatureTrajectory(const unsigned int& frameNum, const cv::Point2f& p, const cv::Mat& homography);
 
@@ -75,10 +77,12 @@
 typedef boost::shared_ptr<FeatureTrajectory> FeatureTrajectoryPtr;
 
 // inlined
-inline std::ostream& operator<<(std::ostream& out, const FeatureTrajectory& ft) {
+inline std::ostream& operator<<(std::ostream& out, const FeatureTrajectory& ft) 
+{
   out << *(ft.positions);
   out << "\n";
   out << *(ft.velocities);
+
   return out;
 }
 
--- a/include/Parameters.hpp	Mon Jul 29 13:46:07 2013 -0400
+++ b/include/Parameters.hpp	Mon Jul 29 17:12:45 2013 -0400
@@ -17,6 +17,11 @@
   bool groupFeatures;
 
   std::string videoFilename;
+
+  std::string listFilename;
+  std::string folderData;
+
+
   std::string databaseFilename;
   std::string homographyFilename;
   std::string maskFilename;
--- a/trafficintelligence.vcxproj	Mon Jul 29 13:46:07 2013 -0400
+++ b/trafficintelligence.vcxproj	Mon Jul 29 17:12:45 2013 -0400
@@ -14,6 +14,8 @@
     <ClCompile Include="..\trajectorymanagementandanalysis\trunk\src\TrajectoryManagementAndAnalysis\src\DBSQLiteAccess.cpp" />
     <ClCompile Include="c\cvutils.cpp" />
     <ClCompile Include="c\feature-based-tracking.cpp" />
+    <ClCompile Include="c\InputFrameListModule.cpp" />
+    <ClCompile Include="c\InputVideoFileModule.cpp" />
     <ClCompile Include="c\Motion.cpp" />
     <ClCompile Include="c\Parameters.cpp" />
     <ClCompile Include="c\utils.cpp" />
@@ -21,6 +23,9 @@
   <ItemGroup>
     <ClInclude Include="include\catch.hpp" />
     <ClInclude Include="include\cvutils.hpp" />
+    <ClInclude Include="include\InputFrameListModule.h" />
+    <ClInclude Include="include\InputFrameProviderIface.h" />
+    <ClInclude Include="include\InputVideoFileModule.h" />
     <ClInclude Include="include\learning.hpp" />
     <ClInclude Include="include\Motion.hpp" />
     <ClInclude Include="include\Parameters.hpp" />