forked from yhlleo/imageBinaryDataset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryDataset.cpp
119 lines (102 loc) · 2.74 KB
/
BinaryDataset.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
#include "BinaryDataset.h"
void BinaDataset::images2BinaryFile(
std::string filefolder, std::vector<std::string>& img_list,
std::vector<int>& img_labels, std::string filename )
{
const int size_list = img_list.size();
FILE *fp = fopen( filename.c_str(), "wb" );
if ( fp == NULL )
{
std::cout << "Open error!" << std::endl;
fclose(fp);
return;
}
for ( int idx = 0; idx <size_list; ++idx )
{
std::string currentPath = filefolder + "\\";
currentPath += img_list[idx];
mat2Binary( currentPath, img_labels[idx], fp );
#if 1
std::cout << "image " << idx+1 << " saved." << std::endl;
#endif
}
fclose(fp);
}
void BinaDataset::mat2Binary(
std::string& image_file, int label, FILE*& fp )
{
cv::Mat image = cv::imread( image_file, IMREAD_UNCHANGED );
if ( !image.data )
{
std::cout << "Image " << getFileName(image_file) << " load failed!"
<< std::endl;
}
else
{
if ( image.channels() == 1 )
{
cv::cvtColor(image, image, CV_GRAY2RGB );
}
else
{
cv::cvtColor(image, image, CV_BGR2RGB );
}
cv::Mat image_reshaped;
cv::resize( image, image_reshaped, cv::Size(_iWidth, _iHeight), CV_INTER_LINEAR );
convertMat2Bin(image_reshaped, label, fp);
}
}
void BinaDataset::convertMat2Bin( cv::Mat& image, int label, FILE*& fp )
{
fwrite(&label, sizeof(char), 1, fp);
int pixelCount = image.rows * image.cols;
char* pData = (char*)image.data;
for ( int i=0; i<pixelCount; i++ )
fwrite(&pData[i*3], sizeof(char), 1, fp); // R
for ( int i=0; i<pixelCount; i++ )
fwrite(&pData[i*3+1], sizeof(char), 1, fp); // G
for ( int i=0; i<pixelCount; i++ )
fwrite(&pData[i*3+2], sizeof(char), 1, fp); // B
// delete pData;
}
std::string BinaDataset::getFileName( std::string & filename )
{
int iBeginIndex = filename.find_last_of("\\")+1;
int iEndIndex = filename.length();
return filename.substr( iBeginIndex, iEndIndex - iBeginIndex );
}
std::vector<std::string> BinaDataset::getFileLists( std::string file_folder )
{
file_folder += "/*.*";
const char * mystr=file_folder.c_str();
std::vector<std::string> flist;
std::string lineStr;
std::vector<std::string> extendName;
extendName.push_back("jpg");
extendName.push_back("JPG");
extendName.push_back("bmp");
extendName.push_back("png");
extendName.push_back("gif");
HANDLE file;
WIN32_FIND_DATA fileData;
char line[1024];
wchar_t fn[1000];
mbstowcs( fn, mystr, 999 );
file = FindFirstFile( fn, &fileData );
FindNextFile( file, &fileData );
while(FindNextFile( file, &fileData ))
{
wcstombs( line, (const wchar_t*)fileData.cFileName, 259);
lineStr = line;
// remove the files which are not images
for (int i = 0; i < 4; i ++)
{
if (lineStr.find(extendName[i]) < 999)
{
flist.push_back(lineStr);
break;
}
}
}
return flist;
}