-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeature_extraction.cpp
129 lines (100 loc) · 3.81 KB
/
feature_extraction.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "feature_extraction.h"
int extractFeauture(Ptr<FeatureDetector>& detector , Ptr<DescriptorExtractor>& dextractor ,
const string& directory_path , const string& destination_dirctory_path ){
DIR* dirp ;
dirent* dep;
FileStorage fs;
Mat descriptors;
vector <string> images_container;
//fill the container with Image names ,I use the vecotor container to easy the parallesim .
dirp = opendir(directory_path.c_str() );
if (dirp == NULL){
cerr << "Cannot open " << directory_path << endl ;
return -2;
}
string image_name;
while ((dep = readdir(dirp)) != NULL) {
if ( (strcmp(dep->d_name ,".") == 0) ||(strcmp(dep->d_name ,"..") == 0) ){
continue ;
}
image_name.assign( dep->d_name );
images_container.push_back(image_name) ;
}
closedir(dirp) ;
//detect and descripe the whole features from Images container (984 img) .
#pragma omp parallel for num_threads(4) schedule(dynamic)
for ( size_t i = 0 ; i < images_container.size() ; i++ ){
string filepath = directory_path + "/" +images_container[i] ;
Mat desc ;
Mat img = imread(filepath ,CV_LOAD_IMAGE_GRAYSCALE) ;
vector <KeyPoint> keypoints;
// if ( !img.data ){
// cerr << "Cannot read image" << filepath << endl ;
// return -2 ;
// }
cout << "Extract Features from img " << i+1 << "...."<<endl ;
detector->detect(img, keypoints);
dextractor->compute(img, keypoints, desc);
#pragma omp critical
{
descriptors.push_back(desc);
}
}
//store the whole unclustered descriptors into the <destination_directory_path> as yaml file .
string destination = destination_dirctory_path +"/descriptors-984img.yaml.gz" ;
fs.open(destination, FileStorage::WRITE);
if ( !fs.isOpened() ){
cerr << "Cannot open Filestorage " << endl ;
return -2 ;
}
cout << "Number of Desctriptors Extracted = " << descriptors.rows <<" each with dimintion = "
<< descriptors.cols << endl;
cout << "Writing Descrtiptors to File ..." << endl;
fs <<"Training-Images" << descriptors ;
fs.release();
cout << "Write has been sucssfully done " << endl ;
return 0 ;
}
int extractFeauture(Ptr<FeatureDetector>& detector , Ptr<DescriptorExtractor>& dextractor ,
const string& training_dir , Mat& unclustered_desctriptors ){
DIR* dirp ;
dirent* dep;
vector <string> images_container;
string image_name;
//fill the container with Image names ,I use the vecotor container to easy the parallesim .
dirp = opendir(training_dir.c_str() );
if (dirp == NULL){
cerr << "Cannot open " << training_dir << endl ;
return -2;
}
while ((dep = readdir(dirp)) != NULL) {
if ( (strcmp(dep->d_name ,".") == 0) ||(strcmp(dep->d_name ,"..") == 0) ){
continue ;
}
image_name.assign( dep->d_name );
images_container.push_back(image_name) ;
}
closedir(dirp) ;
//detect and descripe the whole features from Images container (984 img) .
#pragma omp parallel for num_threads(4) schedule(dynamic)
for ( size_t i = 0 ; i < images_container.size() ; i++ ){
string filepath = training_dir + "/" +images_container[i] ;
Mat desc ;
Mat img = imread(filepath ,CV_LOAD_IMAGE_GRAYSCALE) ;
vector <KeyPoint> keypoints;
// if ( !img.data ){
// cerr << "Cannot read image" << filepath << endl ;
// return -2 ;
// }
cout << "Extract Features from img " << i+1 << "...."<<endl ;
detector->detect(img, keypoints);
dextractor->compute(img, keypoints, desc);
#pragma omp critical
{
unclustered_desctriptors.push_back(desc);
}
}
cout << "Number of Desctriptors Extracted = " << unclustered_desctriptors.rows <<" each with dimintion = "
<< unclustered_desctriptors.cols << endl;
return 0 ;
}