-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPCA.cpp
147 lines (99 loc) · 2.85 KB
/
PCA.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
#include "PCA.h"
void PCA_BuildSave(char *image_dir, int blocks)
{
char path[40];
int fcount = 30, descsize;
double *desc;
cv::Mat cinput, coutput;
sprintf(path, "%s/image1", image_dir);
cinput = cv::imread(path);
format_image(cinput, coutput);
Gist_Processor proc(coutput, blocks);
cv::Mat PCAm(fcount, proc.base_descsize, CV_64FC1);
for (int i=0; i < fcount; i++) {
sprintf(path, "%s/image%d", image_dir, i);
printf("Image %d\n", i);
cinput = cv::imread(path);
format_image(cinput, coutput);
proc.Process(coutput);
descsize = proc.Get_Descriptor(&desc,blocks);
for (int d=0; d < descsize; d++) {
//((double *)Row.data)[d] = desc[d];
PCAm.at<double>(i,d) = desc[d];
}
free(desc);
}
cv::PCA pca_obj(PCAm, cv::Mat(), CV_PCA_DATA_AS_ROW, PCA_DIM);
printf("type %d %d %d\n", pca_obj.eigenvectors.type(), CV_64FC1, CV_64FC1);
sprintf(path, "./PCAeigenvectors%d.mat", blocks);
FILE *fd = fopen(path, "w+");
if (!fd) {
printf("%s\n", path);
perror("Error opening file for loading %s\n");
return;
}
fprintf(fd, "%d\n%d\n", pca_obj.eigenvectors.rows, pca_obj.eigenvectors.cols);
for (int j=0; j < pca_obj.eigenvectors.rows; j++) {
for (int i=0; i < pca_obj.eigenvectors.cols; i++) {
fprintf(fd, "%f\n", pca_obj.eigenvectors.at<double>(j,i));
}
}
fclose(fd);
sprintf(path, "./PCAmean%d.mat", blocks);
fd = fopen(path, "w+");
if (!fd) {
perror("Error opening file for loading\n");
return;
}
fprintf(fd, "%d\n%d\n", pca_obj.mean.rows, pca_obj.mean.cols);
for (int j=0; j < pca_obj.mean.rows; j++) {
for (int i=0; i < pca_obj.mean.cols; i++) {
fprintf(fd, "%f\n", pca_obj.mean.at<double>(j,i));
}
}
fclose(fd);
}
cv::PCA *PCA_LoadData(int blocks)
{
char path[40];
int rows, cols;
sprintf(path, "./PCAeigenvectors%d.mat", blocks);
FILE *fd = fopen(path, "r+");
if (!fd) {
perror("Error opening file for loading\n");
return NULL;
}
fscanf(fd, "%d", &rows);
fscanf(fd, "%d", &cols);
cv::Mat eigenvectors(rows, cols, CV_64FC1);
for (int j=0; j < eigenvectors.rows; j++) {
for (int i=0; i < eigenvectors.cols; i++) {
fscanf(fd, "%lf", &(eigenvectors.at<double>(j,i)));
}
}
fclose(fd);
sprintf(path, "./PCAmean%d.mat", blocks);
fd = fopen(path, "r+");
if (!fd) {
printf("blah %s\n", path);
perror("Error opening file for loading huh\n");
return NULL;
}
fscanf(fd, "%d", &rows);
fscanf(fd, "%d", &cols);
cv::Mat mean(rows, cols, CV_64FC1);
for (int j=0; j < mean.rows; j++) {
for (int i=0; i < mean.cols; i++) {
fscanf(fd, "%lf", &(mean.at<double>(j,i)));
}
}
fclose(fd);
cv::PCA *pca_obj = new cv::PCA();
pca_obj->eigenvectors = eigenvectors;
pca_obj->mean = mean;
return pca_obj;
}
void PCA_Free(cv::PCA *p)
{
delete p;
}