├── main.cpp ├── BinaryDataset.h ├── README.md └── BinaryDataset.cpp /main.cpp: -------------------------------------------------------------------------------- 1 | #include "BinaryDataset.h" 2 | 3 | void main() 4 | { 5 | std::string filefolder = "C:\\Samples\\train"; 6 | BinaDataset binData; 7 | std::vector fileLists = binData.getFileLists(filefolder); // load file name 8 | 9 | const int size_list = fileLists.size(); 10 | std::cout << "image count: " << size_list << std::endl; 11 | 12 | std::vector image_labels(size_list, 0); // generate lables, here are all 0 13 | 14 | std::string binfile = "C:\\Samples\\train.bin"; 15 | binData.images2BinaryFile( filefolder, fileLists, image_labels, binfile ); 16 | } 17 | -------------------------------------------------------------------------------- /BinaryDataset.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Generate a binary format dataset of images for the 3 | * convolutional neural network(CNN) based on tensorflow. 4 | * 5 | * dataform: 6 | * - data: a nx3072 array of uint8s. Each row of the 7 | * array stores a 32x32 colour image. The first 1024 8 | * entries contain the red channel values, the next 1024 9 | * the green, and the final 1024 the blue. The image is 10 | * stored in row-major order, so that the first 32 entries 11 | * of the array are the red channel values of the first 12 | * row of the image. 13 | * - labels: a list of n numbers in the range 0-9. The 14 | * number at index i indicates the label of the ith image 15 | * in the array data. 16 | * Each file contains n such 3073-byte. 17 | * 18 | * Editor: Yahui Liu. 19 | * Data: 2016-03-03 20 | * Email: yahui.cvrs@gmail.com 21 | * Address: Computer Vision and Remote Sensing(CVRS) Lab, Wuhan University. 22 | */ 23 | 24 | #ifndef BINARY_DATASET_H 25 | #define BINARY_DATASET_H 26 | #pragma once 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | #include "cv.h" 35 | #include "highgui.h" 36 | 37 | using namespace std; 38 | using namespace cv; 39 | 40 | class BinaDataset 41 | { 42 | public: 43 | BinaDataset() 44 | { 45 | _iHeight = 32; 46 | _iWidth = 32; 47 | } 48 | ~CrackBinaDataset(){} 49 | 50 | public: 51 | void images2BinaryFile( std::string filefolder, std::vector& img_list, 52 | std::vector& img_labels, std::string filename ); 53 | 54 | void mat2Binary( std::string& image_file, int label, FILE*& fp ); 55 | 56 | void convertMat2Bin( cv::Mat& image, int label, FILE*& fp ); 57 | 58 | // cv::Mat imageReshape( cv::Mat& input ); 59 | 60 | std::string getFileName( std::string & filename ); 61 | 62 | std::vector getFileLists( std::string file_folder ); 63 | 64 | public: 65 | int _iHeight; 66 | int _iWidth; 67 | }; 68 | #endif // BINARY_DATASET_H 69 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # imageBinaryDataset 2 | 3 | 前一篇博客:[C/C++ 图像二进制存储与读取](http://blog.csdn.net/yhl_leo/article/details/50782792)中,已经讲解了如何利用C/C++的方法存储与读取二进制图像文件,本文继续讲述如何根据[CIFAR-10](http://www.cs.toronto.edu/~kriz/cifar.html)的格式制作自己的数据集。 4 | 5 | 所述博文与代码均已同步至GitHub:[yhlleo/imageBinaryDataset](https://github.com/yhlleo/imageBinaryDataset) 6 | 7 | 主要代码文件有三个: 8 | 9 | - `BinaryDataset.h` 10 | - `BinaryDataset.cpp` 11 | - `main.cpp` 12 | 13 | 以`main.cpp`给出的一个小demo为例,首先指定一个原数据图片所在的文件夹: 14 | 15 | ```c 16 | std::string filefolder = "C:\\Samples\\train"; 17 | ``` 18 | 19 | 然后,自动获得该文件下的所有图片文件名: 20 | 21 | ```c 22 | std::vector fileLists = binData.getFileLists(filefolder); // load file name 23 | ``` 24 | 25 | 这里有一点需要说明一下,`getFileLists()`是按照文件名升序顺序读取(大家都知道,文件名为字符串,comparable),文件命名最好不要以`1, 2, ..., 11, ...`这种方式存储,因为这么存,你就会发现`1`之后的文件可能不是你想的`2, 3, 4, ...`,而是`11, 12, 13, ...`。 26 | 27 | 如果你想按照顺序的某一堆数据是一种类别(我是这么做的,因为便于产生对应的`labels`),建议使用等宽零位补齐的方式命名,例如:`00001, 0002, ..., 0011, ...`,那么文件读取的顺序就会如我们所设定。 28 | 29 | 总结一下实现方法(仅供参考): 30 | 31 | - 采集样本的时候可以先类别存于不同的文件夹,命名就随意吧,如果是使用一些抠图软件,也不用纠结一个一个手工修改成自己想要的命名(这么做工作量很大,真的很蛋疼。。。); 32 | - 每一类数据整理好后,依次将每一类的数据,用程序读取并另存一份(读取使用`getFileLists()`,反正是一类的,也无所谓先后顺序): 33 | ```c 34 | for ( int i=0; i image_labels(size_list, 0); // generate lables, here are all 0 52 | ``` 53 | 54 | 当然,你也可以用`image_labels.push_back()`把所有的`labels`设置,但是熟悉`vector`的话,就会明白使用初始化长度,比那种做法更加高效(可以阅读本人的博客: [C++ 容器(一):顺序容器简介](http://blog.csdn.net/yhl_leo/article/details/47759729))。然后就相应地修改某些索引区间内的`label`值: 55 | 56 | ```c 57 | for ( int i=0; i& img_list, 5 | std::vector& img_labels, std::string filename ) 6 | { 7 | const int size_list = img_list.size(); 8 | 9 | FILE *fp = fopen( filename.c_str(), "wb" ); 10 | if ( fp == NULL ) 11 | { 12 | std::cout << "Open error!" << std::endl; 13 | fclose(fp); 14 | return; 15 | } 16 | 17 | for ( int idx = 0; idx BinaDataset::getFileLists( std::string file_folder ) 85 | { 86 | file_folder += "/*.*"; 87 | const char * mystr=file_folder.c_str(); 88 | std::vector flist; 89 | std::string lineStr; 90 | std::vector extendName; 91 | extendName.push_back("jpg"); 92 | extendName.push_back("JPG"); 93 | extendName.push_back("bmp"); 94 | extendName.push_back("png"); 95 | extendName.push_back("gif"); 96 | 97 | HANDLE file; 98 | WIN32_FIND_DATA fileData; 99 | char line[1024]; 100 | wchar_t fn[1000]; 101 | mbstowcs( fn, mystr, 999 ); 102 | file = FindFirstFile( fn, &fileData ); 103 | FindNextFile( file, &fileData ); 104 | while(FindNextFile( file, &fileData )) 105 | { 106 | wcstombs( line, (const wchar_t*)fileData.cFileName, 259); 107 | lineStr = line; 108 | // remove the files which are not images 109 | for (int i = 0; i < 4; i ++) 110 | { 111 | if (lineStr.find(extendName[i]) < 999) 112 | { 113 | flist.push_back(lineStr); 114 | break; 115 | } 116 | } 117 | } 118 | return flist; 119 | } 120 | --------------------------------------------------------------------------------