Non-rigid face recognition-practical and non-rigid Face Recognition
Object-Oriented Design
Like face detection and face recognition, Face Tracking consists of two parts: Data and algorithms. Algorithms train models by pre-storing (offline) data, and then perform certain operations on new (online) data. Therefore, using object-oriented design is a good choice.
In opencv 2.x, you can easily introduce the XML/YAML file storage type, which greatly simplifies the organization of offline data tasks for the algorithm. The following uses an hypothetical class to demonstrate this function.
- Custom class foo
1 // foo. h 2/* 3 defines a serialization function in the following code, which can be serialized by read and write I/O functions. 4. The FileStorage class supports two types of data structures that can be serialized. 5. For the sake of simplicity, all classes in this chapter will adopt ing. Each of the variables used for storage will create a FileNode: MAP type FileNode object. 6. You need to assign a unique key to each element in the variable. To maintain consistency, use the variable name as the label 7 */8 9 # include <opencv2/opencv. hpp> 10 # include <iostream> 11 using namespace cv; 12 using namespace std; 13 14 class foo {15 public: 16 int a, B; 17 void write (FileStorage & fs) const {// serialized storage custom data type 18 assert (fs. isOpened (); 19 fs <"{" <"a" <"B" <B <"}"; // create FileNode :: MAP type object 20} 21 void read (const FileNode & node) {// read data 22 assert (node. type () = FileNode: MAP); 23 node ["a"]> a; node ["B"]> B; 24} 25 };
- To enable FileStorage class serialization to work properly, you also need to define the write and read functions.
1 template<class T> 2 void 3 write(FileStorage& fs, 4 const string&, 5 const T& x) 6 { 7 x.write(fs); 8 } 9 //==============================================================================10 template<class T>11 void 12 read(const FileNode& node, 13 T& x,14 const T& d)15 {16 if(node.empty())x = d; else x.read(node);17 }
- To make it easy to save and load user-defined classes that adopt serialization, The load_ft and save_ft functions are defined using modular functions.
1 template <class T> 2 T load_ft (const char * fname) {3 T x; FileStorage f (fname, FileStorage: READ ); 4 f ["ft object"]> x; f. release (); return x; // define the labels associated with the object to be ft object 5} 6 // ========================== ========================================================== ================= 7 template <class T> 8 void save_ft (const char * fname, const T & x) {9 FileStorage f (fname, FileStorage: WRITE); 10 f <"ft object" <x; f. release (); 11}
- Define the above in ft. hpp
1/* 2 ft. hpp 3 is used to load and save object data 4 */5 6 # ifndef _ FT_FT_HPP _ 7 # define _ FT_FT_HPP _ 8 # include <opencv2/opencv. hpp> 9 // ======================================== ========================================================== === 10 // to make it easy to save and load user-defined classes that adopt serialization, the load_ft and save_ft functions are defined using modular functions 11 template <class T> 12 T load_ft (const char * fname) {13 T x; FileStorage f (fname, FileStorage: READ ); 14 f ["ft object"]> x; f. release (); return x; // define the labels associated with the object as ft object15} 16 // ============================ ========================================================== =============== 17 template <class T> 18 void save_ft (const char * fname, const T & x) {19 FileStorage f (fname, FileStorage: WRITE); 20 f <"ft object" <x; f. release (); 21} 22 // ========================================== ========================================================== === 23 // to enable FileStorage class serialization to work properly, you also need to define the write, read function 24 template <class T> 25 void 26 write (FileStorage & fs, 27 const string &, 28 const T & x) 29 {30 x. write (fs ); 31} 32 // ========================================== ========================================================== === 33 template <class T> 34 void 35 read (const FileNode & node, 36 T & x, 37 const T & d) 38 {39 if (node. empty () x = d; else x. read (node ); 40} 41 // ========================================== ========================================================== ===42 # endifFt. hpp
- There is a problem with the main function. An error is always reported when the xml file is stored, and the yaml file can be accessed normally.
1/* 2 main. cpp 3 test opencv file storage 4 */5 6 # include "opencv_hotshots/ft. hpp "7 # include" foo. h "8 9 int main () {10 foo A; // initialize the custom object A11. a = 1;. B = 2; 12 save_ft <foo> ("foo. yaml ", A); // Save the custom object to foo. yaml13 foo B = load_ft <foo> ("foo. yaml "); // read object 14 cout <B. a <"," <B. B <endl; 15 16 system ("pause"); 17 return 0; 18}
- Program running result