-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPRJ2-3.cpp
222 lines (189 loc) · 8.22 KB
/
PRJ2-3.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*Project 2-3 is designed for comparing the images in the image categories in the main DATASET folder and checks whether the matches result with the same categories.
In order for this program to work the user has to have a DATASET folder within the project and image categories within that folder.*/
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <vector>
#include <string>
#include <stdio.h>
#include <dirent.h>
#include <iostream>
using namespace cv;
using namespace std;
vector<pair<string, vector<Mat> > >* getHistogramFromDirectories();
Mat getHistogram(Mat *image);
float getHistIntersectionValue(Mat current, Mat compare);
float getChiSquareValue(Mat current, Mat compare);
#define QUANTUM 1
//QUANTUM value is controlled from here
#define GRID 1
//GRID 1 is just 1 cell and 4 is a 2x2 grid
#define COMPARE_METHOD 1
//CMPARE_METHOD 1 is hist intersection and 2 is chi square
int main(){
vector<pair<string, vector<Mat > > >* histograms = getHistogramFromDirectories();
vector<pair<string, vector<Mat > > >* test = new vector<pair<string, vector<Mat > > >();
vector<pair<string, vector<Mat > > >* trainning = new vector<pair<string, vector<Mat > > >();
vector<pair<string,int> > *results = new vector<pair<string, int> >();
//Dividing histograms into 2 different containiers as test and trainning
for (int i= 0 ; i<histograms->size(); i++) {
test->push_back(pair<string, vector<Mat> >(histograms->at(i).first,vector<Mat>()));
trainning->push_back(pair<string, vector<Mat> >(histograms->at(i).first,vector<Mat>()));
for (int j = 0; j < histograms->at(i).second.size(); j++) {
if(j < histograms->at(i).second.size()/2){
trainning->at(i).second.push_back(histograms->at(i).second.at(j));
}else
test->at(i).second.push_back(histograms->at(i).second.at(j));
}
}
//Finding the categories of histograms
int indexRow = 0;
for (vector<pair<string, vector<Mat > > >::iterator i = test->begin(); i != test->end(); i++) {
results->push_back(pair<string, int>(i->first,0));
int indexCol = 0;
for (vector<Mat>::iterator j = i->second.begin(); j != i->second.end(); j++) {
string category;
float min = 100000000000000;
//finding the closest min value to 0
for (vector<pair<string, vector<Mat > > >::iterator t = trainning->begin(); t!=trainning->end(); t++) {
float temp=0;
for (vector<Mat>::iterator k = t->second.begin(); k != t->second.end(); k++) {
if (COMPARE_METHOD==1) {
temp = getHistIntersectionValue(*j,*k);
}else if(COMPARE_METHOD==2){
temp = getChiSquareValue(*j,*k);
}
if(temp<min){
min = temp;
category = t->first;
}
}
}
if(category==i->first)
{
results->at(indexRow).second += 1;
//In case we have a hit in the grid formation. We skip to the next image
if(GRID ==4){
int temp =indexCol%4;
j +=3-temp;
indexCol+= 3 - temp;
if (indexCol >= i->second.size()) {
break;
}
}
}
indexCol++;
}
indexRow++;
}
//Printing the results
int totalSize = 0;
int totalCorrect = 0;
for (int i = 0; i < results->size(); i++) {
totalCorrect += results->at(i).second;
float size;
if(GRID==1){
size = (double)results->at(i).second/test->at(i).second.size();
totalSize += test->at(i).second.size();
}else if(GRID==4){
size = (double)results->at(i).second/(test->at(i).second.size()/4);
totalSize += test->at(i).second.size()/4;
}
cout << results->at(i).first << " " << size*100.0 << "%"<< endl;
}
cout << "Overall "<< ((float)totalCorrect/totalSize)*100.0 <<"%";
return 0;
}
vector<pair<string, vector<Mat> > > *getHistogramFromDirectories(){
vector<pair<string, vector<Mat > > >* histograms = new vector<pair<string, vector< Mat > > >();
const char* PATH = "DATASET";
vector<string>* categories = new vector<string>();
DIR *firstLevel = opendir(PATH);
struct dirent *entry = readdir(firstLevel);
while (entry != NULL)
{
if (entry->d_type == DT_DIR && entry->d_name[0] != '.')
categories->push_back(entry->d_name);
entry = readdir(firstLevel);
}
closedir(firstLevel);
for (int i = 0; i < categories->size(); i++) {
string secondPath = "";
secondPath+=PATH;
secondPath+= "/" + categories->at(i) + "/";
DIR *secondLevel = opendir(secondPath.c_str());
entry = readdir(secondLevel);
vector<Mat> *categorieHistograms = new vector<Mat>();
Mat picture;
Mat histogram;
while (entry != NULL)
{
if (entry->d_type != DT_DIR && entry->d_name[0] != '.'){
picture = imread(secondPath+entry->d_name,CV_LOAD_IMAGE_COLOR);
if(GRID ==1){
//Getting the histogram of the image
histogram = getHistogram(&picture);
categorieHistograms->push_back(histogram);
}else if(GRID==4){
//Dividing the image into 4 and getting the histograms of it
Mat leftUp = picture( Rect(0, 0, picture.cols/2, picture.rows/2) );
categorieHistograms->push_back(getHistogram(&leftUp));
Mat rightUp = picture( Rect(picture.cols/2, 0, picture.cols/2, picture.rows/2) );
categorieHistograms->push_back(getHistogram(&rightUp));
Mat leftDown = picture( Rect(0, picture.rows/2, picture.cols/2, picture.rows/2) );
categorieHistograms->push_back(getHistogram(&leftDown));
Mat rightDown = picture( Rect(picture.cols/2, picture.rows/2, picture.cols/2, picture.rows/2) );
categorieHistograms->push_back(getHistogram(&rightDown));
}
}
entry = readdir(secondLevel);
}
histograms->push_back(pair<string, vector<Mat > >(categories->at(i), *categorieHistograms));
closedir(secondLevel);
}
return histograms;
}
Mat getHistogram(Mat *image){
int numPixels = image->rows*image->cols;
int numberOfChannels = 3;
Mat channels[3];
Mat histogram = Mat::zeros(1, numberOfChannels*(256/QUANTUM), CV_32FC1);
split(*image, channels);
for (int i = 0; i < numberOfChannels; i++) {
for (int j = 0; j < channels[0].rows; j++) {
for (int k = 0; k < channels[0].cols; k++) {
//Finding the histogram bin and increasing it by 1
int density = channels[i].at<uchar>(j,k);
int location =(int)(i+1)*(density/(double)QUANTUM);
histogram.at<float>(0,location) += 1;
}
}
}
//Normalizing histogram
for (int i = 0; i<histogram.cols ; i++) {
histogram.at<float>(0,i) /= numPixels*numberOfChannels;
}
return histogram;
}
float getHistIntersectionValue(Mat current, Mat compare){
//Applying histogram intersection formula
float temp = 1;
for (int t = 0 ; t<current.cols; t++) {
temp -= MIN(current.at<float>(0,t),compare.at<float>(0,t));
}
return temp;
}
float getChiSquareValue(Mat current, Mat compare){
//Applying chi square formula
float temp = 0;
for (int t = 0 ; t<current.cols; t++) {
float x = current.at<float>(0,t);
float y = compare.at<float>(0,t);
float numerator = pow(x - y, 2.0);
float denominator = x+ y;
if (denominator==0) {
continue;
}
temp += numerator/(denominator*2);
}
return temp;
}