-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageRead.h
189 lines (169 loc) · 4.28 KB
/
ImageRead.h
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
#ifndef IMAGE_READ_H
#define IMAGE_READ_H
#include<opencv2/opencv.hpp>
#include<opencv2/core.hpp>
#include<iostream>
#include<string>
#include<vector>
#include<dirent.h>
#include<locale.h>
#include<cstring>
#include"SimpleCNN.h"
using namespace cv;
using namespace std;
namespace img_read {
class Image {
vector<float> rV, gV, bV;
vector<vector<float>> rDV, gDV, bDV;
vector<vector<vector<float>>> channel;
Mat img;
public:
Image() {}
void imageRead(string fileName) {
std::cout << "fileName: " << fileName <<"\n";
img = imread(fileName, 1);
std::cout <<"À̹ÌÁö°ª:" << img.rows << " " << img.cols << "\n";
for (int i = 0; i < img.rows; i++) {
for (int j = 0; j < img.cols; j++) {
float b = (float)img.at<Vec3b>(i, j)[0] / 255.f;
float g = (float)img.at<Vec3b>(i, j)[1] / 255.f;
float r = (float)img.at<Vec3b>(i, j)[2] / 255.f;
rV.push_back(r); gV.push_back(g); bV.push_back(b);
}
rDV.push_back(rV); gDV.push_back(gV); bDV.push_back(bV);
rV.clear(); gV.clear(); bV.clear();
}
channel.push_back(rDV);
channel.push_back(gDV);
channel.push_back(bDV);
}
vector<vector<vector<float>>>& getReadImage() {
return channel;
}
Mat getImageShow() {
return img;
}
void clear() {
rDV.clear();
gDV.clear();
bDV.clear();
channel.clear();
}
};
enum READ {
FAILED = false, SUCESS = true
};
enum class FILENAME_EXTENSION {
JPG, JPEG, PNG, BMP
};
class Directory {
private:
DIR* dir = NULL;
struct dirent* dirent = NULL;
const char* filePath;
const char* fileNameExtension = "";
std::vector<std::string> fileNameCollection;
int fileLength = 0;
img_read::Image imageData;
vector<vector<vector<vector<float>>>> imageSet;
void setExtension(FILENAME_EXTENSION fileExtension) {
switch (fileExtension) {
case FILENAME_EXTENSION::JPG:
this->fileNameExtension = ".jpg";
break;
case FILENAME_EXTENSION::JPEG:
this->fileNameExtension = ".jpeg";
break;
case FILENAME_EXTENSION::PNG:
this->fileNameExtension = ".png";
break;
case FILENAME_EXTENSION::BMP:
this->fileNameExtension = ".bmp";
break;
}
}
void clearfileNameCollection() {
this->fileNameCollection.clear();
}
public:
Directory(const char* filePath) {
setlocale(LC_ALL, "");
this->filePath = filePath;
setExtension(FILENAME_EXTENSION::JPG);
setImageSet();
}
Directory(const char* filePath, FILENAME_EXTENSION fileExtension) {
setlocale(LC_ALL, "");
this->filePath = filePath;
setExtension(fileExtension);
setImageSet();
}
bool read() {
this->dir = opendir(filePath);
if (this->dir) {
while ((this->dirent = readdir(this->dir))) {
if (this->dirent->d_type == DT_REG && strstr(this->dirent->d_name, this->fileNameExtension)) {
printf("%s\n", this->dirent->d_name);
this->fileNameCollection.push_back(this->dirent->d_name);
this->fileLength++;
}
}
closedir(dir);
printf("Found files : %d°³", fileLength);
return READ::SUCESS;
}
else {
printf("Cannot open directory");
return READ::FAILED;
}
}
std::vector<std::string> getFileNameCollection() {
return this->fileNameCollection;
}
vector<vector<vector<vector<float>>>>& getImageSet() {
return this->imageSet;
}
void setImageSet() {
if (this->read()) {
fileNameCollection = this->getFileNameCollection();
for (auto iter = fileNameCollection.begin(); iter != fileNameCollection.end(); iter++) {
imageData.imageRead(*iter);
imageSet.push_back(imageData.getReadImage());
imageData.clear();
}
}
else {
}
}
};
class FileRead { // label read. only int.
private:
std::vector<std::string> label;
std::vector<float> convertStringToFloat(std::vector<std::string> label) {
std::vector<float> labelFloat;
for (int i = 0; i < label.size(); i++) {
labelFloat.push_back(stof(label[i]));
}
return labelFloat;
}
public:
FileRead(std::string fileName) {
this->read(fileName);
}
void read(std::string fileName) {
string str_buf;
fstream fs;
fs.open(fileName, ios::in);
while (!fs.eof()) {
getline(fs, str_buf, '\n');
this->label.push_back(str_buf);
}
this->label.pop_back();
fs.close();
}
std::vector<float> getLabel() {
return convertStringToFloat(this->label);
}
};
}
#endif