-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeatureCompute.cpp
189 lines (154 loc) · 7.56 KB
/
featureCompute.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
Compute the features of each image in the specified directory and store them in a CSV file.
To run:
make featureCompute
./featureCompute <directory path> <feature type> <csv file>
Feature Types:
1. Baseline matching
2. Single histogram
3. Multi histogram
4. Texture and Color
5. Deep Network embeddings
6. Compare DNN embeddings and Classic features
7. Custom Design
a. Color histogram to get combined color histogram from all channels instead of single channels like Task 2 and 3
Extensions:
1. DNN with color histogram
2. DNN with magnitude
*/
/*
Difference between computeHistogram (Task 2,3,4) and colorHistogram (Task 7)
1. computeHistogram:This function computes a histogram for an input image. It generates a histogram based on the intensity values of the pixels in the image.
This means it considers all color channels (usually three channels for RGB images) and computes a single histogram representing the distribution of intensity values across the entire image.
2. computeColorHistogram: This function specifically computes a color histogram for an input image.
Unlike computeHistogram, which considers intensity values across all color channels, computeColorHistogram separates the image into its color channels (usually Red, Green, and Blue for RGB images) and computes a histogram for each channel individually.
This means it generates separate histograms representing the distribution of each color channel's intensity values.
In summary, the main difference lies in the type of information captured by each histogram:
computeHistogram captures overall intensity distribution of the image.
computeColorHistogram captures individual color channel intensity distributions.
*/
#include<iostream> // Input-output stream operations
#include<fstream> // File stream operations
#include<opencv2/opencv.hpp> // OpenCV library for computer vision tasks
#include<vector> // Standard template library for vectors
#include "features.hpp" // Custom header file for feature extraction
#include<dirent.h> // Directory handling
#include "csv_util.h" // Custom header file for CSV file handling
using namespace std;
int main(int argc, char *argv[]){
char dirname[256]; // Stores the directory path
char buffer[256]; // Buffer for constructing file paths
char csvfileName[256]; // Stores the name of the CSV file to write feature vectors
int featureType; // Type of feature to be computed
// File and directory handling variables
fstream csvFile;
FILE *fp;
DIR *dirp;
struct dirent *dp;
int i;
// Checking for sufficient command line arguments
if(argc < 4){
cout<<"Usage: "<<argv[1]<<"<directory path> <feature type> <csv file>\n";
exit(-1);
}
// Retrieving command line arguments
strcpy(dirname, argv[1]); // Directory path
cout<<"Processing directory: "<< dirname<<"\n";
// Open the directory
dirp = opendir(dirname);
if(dirp==NULL){
cout<<"Cannot open directory: "<< dirname<<"\n";
exit(-1);
}
// Get feature type
featureType = atoi(argv[2]); // Feature type for computation
// Get the csv file name
strcpy(csvfileName, argv[3]); // CSV file name for storing feature vectors
// Read feature vectors from file
vector<char *> filenames; // Vector to store filenames
vector<vector<float>> data; // Vector to store feature vectors
char resNet[256]; // File name for feature vectors
strcpy(resNet, "ResNet18_olym.csv"); // Assuming default name for feature vector file
if(read_image_data_csv(resNet, filenames, data)!=0){ // Reading feature vectors
cout<<"Error: Unable to read feature vector file"<<endl;
exit(-1);
}
char completeFN[256]; // Stores complete file name
vector<float> featureVector; // Vector to store feature vector
vector<float> featureVector_2; // Additional vector for combining features
vector<float> featureVector_3;
vector<float> featureVector_4;
// Loop over all the files in the image file listing
while( (dp = readdir(dirp)) != NULL ){
// Check if the file is an image
if( strstr(dp->d_name, ".jpg") || strstr(dp->d_name, ".png") || strstr(dp->d_name, ".ppm") || strstr(dp->d_name, ".tif") || strstr(dp->d_name, ".jpeg")){
// Build the overall filename
strcpy(buffer, dirname);
strcat(buffer, "/");
strcat(buffer, dp->d_name);
// Read the image
cv::Mat image = cv::imread(buffer);
if(image.empty()){
cout<<"Error: Unable to read image:"<<buffer<<endl;
continue;
}
// Compute the feature for each image based on the specified feature type
switch(featureType){
case 1: // Baseline feature extraction
featureVector = baseline(image);
break;
case 2: // Single Histogram feature extraction
featureVector = singleHM(image);
break;
case 3: // Multi Histogram feature extraction
featureVector = multiHM(image);
break;
case 4: // Feature Histogram feature extraction
featureVector = featureHM(image);
break;
case 7: // Color Histogram feature extraction
featureVector = computeColorHistogram(image);
break;
case 8: // Custom feature extraction
for(size_t i=0; i<filenames.size(); ++i){
strcpy(completeFN, "olympus");
strcat(completeFN, "/");
strcat(completeFN, filenames[i]);
if(strlen(filenames[i])==0){
cout<<"Error: Unable to read image:"<<completeFN<<endl;
continue;
}
if(strcmp(completeFN,buffer) == 0){
featureVector_2 = data[i];
}
}
featureVector = computeColorHistogram(image);
featureVector.insert(featureVector.end(), featureVector_2.begin(), featureVector_2.end());
break;
case 9: // Custom feature extraction
for(size_t i=0; i<filenames.size(); ++i){
strcpy(completeFN, "olympus");
strcat(completeFN, "/");
strcat(completeFN, filenames[i]);
if(strlen(filenames[i])==0){
cout<<"Error: Unable to read image:"<<completeFN<<endl;
continue;
}
if(strcmp(completeFN,buffer) == 0){
featureVector_2 = data[i];
}
}
featureVector = featureHM(image);
featureVector.insert(featureVector.end(), featureVector_2.begin(), featureVector_2.end());
break;
default:
cout<<"Invalid feature type"<<endl;
break;
}
// Append the feature vector to the CSV file
append_image_data_csv(csvfileName, dp->d_name, featureVector, 0);
}
}
cout<<"Terminating\n";
return 0;
}