-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPRJ6.cpp
183 lines (140 loc) · 5.72 KB
/
PRJ6.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
/*Project 6 is again an extension of Project 5. In the project the goal is to find the centers within the PCA space by using gauissian quantifier.
It uses Mean Shift alogrithm to find these centers */
#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> > >* getHistogramFromDirectoriesForMean();
Mat getHistogramForMean(Mat *image);
float getGaus(double dist, float sigma);
#define QUANTUM 8
int main(){
vector<pair<string, vector<Mat > > >* histograms = getHistogramFromDirectoriesForMean();
float sigma = 0.03;
float threshold = 0.00007;
Mat *dataForPCA = new Mat();
for (int i= 0 ; i<histograms->size(); i++) {
for (int j = 0; j < histograms->at(i).second.size(); j++) {
dataForPCA->push_back(histograms->at(i).second.at(j));
}
}
PCA pca(*dataForPCA, cv::Mat(), CV_PCA_DATA_AS_ROW, 0.85);
for (int i = 0; i < dataForPCA->rows; i++) {
Mat point = pca.project(dataForPCA->row(i));
dataForPCA->row(i) = point;
}
Mat means;
Mat average;
Mat current = dataForPCA->row(0);
int i = 1;
while (i < dataForPCA->rows) {
average = current.clone();
float totalWeight= 0;
for (int y = 0; y < dataForPCA->rows; y++) {
if(i==y)
continue;
Mat second = dataForPCA->row(y);
float dist = norm(second, current, NORM_L2);
float weight = getGaus(dist, sigma);
totalWeight += weight;
average += second * weight;
}
average = average/totalWeight;
double dist = norm(average, current, NORM_L2);
if(dist < threshold){
bool maxima = true;
for (int t = 0; t < means.rows; ++t) {
Mat currentMean = means.row(t);
double distBetweenMeans = norm(average ,currentMean, NORM_L2);
if (distBetweenMeans < 0.00001) {
currentMean = (currentMean + average) / 2.0;
maxima = false;
break;
}
}
if(maxima)
means.push_back(average);
current = dataForPCA->row(i++);
}else
current = average;
}
cout << "Number of center:" <<means.rows;
return 0;
}
float getGaus(double dist, float sigma){
float numerator = exp(-1.0 * pow(dist,2) / (2.0 * pow(sigma, 2)));
float denominator = (sigma * sqrt(2.0 * CV_PI));
return numerator/denominator;
}
vector<pair<string, vector<Mat> > > *getHistogramFromDirectoriesForMean(){
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;
while (entry != NULL)
{
if (entry->d_type != DT_DIR && entry->d_name[0] != '.'){
picture = imread(secondPath+entry->d_name,CV_LOAD_IMAGE_COLOR);
Mat *wholeImage = new Mat();
for (int x = 0; x <= picture.cols- picture.cols/4 ;x +=picture.cols/4) {
for (int y = 0; y <= picture.rows - picture.rows/4; y +=picture.rows/4) {
Mat image = picture( Rect(x, y, picture.cols/4, picture.rows/4) );
if(wholeImage->empty())
*wholeImage = getHistogramForMean(&image);
else
hconcat(*wholeImage, getHistogramForMean(&image), *wholeImage);
}
}
categorieHistograms->push_back(*wholeImage);
}
entry = readdir(secondLevel);
}
histograms->push_back(pair<string, vector<Mat > >(categories->at(i), *categorieHistograms));
closedir(secondLevel);
}
return histograms;
}
Mat getHistogramForMean(Mat *image){
int numPixels = image->rows*image->cols;
int numberOfChannels = 3;
Mat histogram = Mat::zeros(1, numberOfChannels*(256/QUANTUM), CV_32FC1);
for(int row = 0; row < image->rows; ++row) {
uchar* p = image->ptr(row);
for(int col = 0; col < image->cols*3; ++col) {
//Finding the histogram bin and increasing it by 1
int density = *p++;
int location =(int)(col/image->cols+1)*(density/(double)QUANTUM);
float* pHistogram = histogram.ptr<float>(0,location);
*pHistogram += 1;
}
}
//Normalizing histogram
float* p = histogram.ptr<float>(0);
for(int col = 0; col < histogram.cols; ++col) {
//Finding the histogram bin and increasing it by 1
*p /= numPixels*numberOfChannels*16;
*p++;
}
return histogram;
}