1. 程式人生 > >Opencv + C++轉換mnist資料集檔案為影象

Opencv + C++轉換mnist資料集檔案為影象

mnist資料集是深度學習入門的一個很經典的手寫體數字的資料集,我們跑的第一個深度學習網路往往就是mnist資料集,但是它的資料格式比較特殊,具體的格式及介紹見官網http://yann.lecun.com/exdb/mnist/

今天就寫個程式,把mnist資料集中的資料轉換為影象,這樣我們就可以利用這些資料把影象轉換為像caffe、Tensorflow等要求的格式作為練習,以後對自己的學習任務的樣本也可以這樣做。

廢話不多說了,直接上程式碼,程式碼主要參考的是caffe中的convert_mnist_data.cpp檔案中的轉換方法。

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
using namespace cv;

//把大端資料轉換為我們常用的小端資料
uint32_t swap_endian(uint32_t val)
{
    val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF);
    return (val << 16) | (val >> 16);
}

void readAndSave(const string& mnist_img_path, const string& mnist_label_path)
{
    //以二進位制格式讀取mnist資料庫中的影象檔案和標籤檔案
    ifstream mnist_image(mnist_img_path, ios::in | ios::binary);
    ifstream mnist_label(mnist_label_path, ios::in | ios::binary);
    if(mnist_image.is_open() == false)
    {
        cout<<"open mnist image file error!"<<endl;
        return ;
    }
    if(mnist_label.is_open() == false)
    {
        cout<<"open mnist label file error!"<<endl;
        return ;
    }
    
    uint32_t magic;//檔案中的魔術數(magic number)
    uint32_t num_items;//mnist影象集檔案中的影象數目
    uint32_t num_label;//mnist標籤集檔案中的標籤數目
    uint32_t rows;//影象的行數
    uint32_t cols;//影象的列數
    //讀魔術數
    mnist_image.read(reinterpret_cast<char*>(&magic), 4);
    magic = swap_endian(magic);
    if(magic != 2051)
    {
        cout<<"this is not the mnist image file"<<endl;
        return ;
    }
    mnist_label.read(reinterpret_cast<char*>(&magic), 4);
    magic = swap_endian(magic);
    if(magic != 2049)
    {
        cout<<"this is not the mnist label file"<<endl;
        return ;
    }
    //讀影象/標籤數
    mnist_image.read(reinterpret_cast<char*>(&num_items), 4);
    num_items = swap_endian(num_items);
    mnist_label.read(reinterpret_cast<char*>(&num_label), 4);
    num_label = swap_endian(num_label);
    //判斷兩種標籤數是否相等
    if(num_items != num_label)
    {
        cout<<"the image file and label file are not a pair"<<endl;
    }
    //讀影象行數、列數
    mnist_image.read(reinterpret_cast<char*>(&rows), 4);
    rows = swap_endian(rows);
    mnist_image.read(reinterpret_cast<char*>(&cols), 4);
    cols = swap_endian(cols);
    //讀取影象
    for(int i = 0; i != num_items; i++)
    {
        char* pixels = new char[rows * cols];
        mnist_image.read(pixels, rows * cols);
        char label;
        mnist_label.read(&label, 1);
        Mat image(rows,cols,CV_8UC1);
        for(int m = 0; m != rows; m++)
        {
            uchar* ptr = image.ptr<uchar>(m);
            for(int n = 0; n != cols; n++)
            {
                if(pixels[m * cols + n] == 0)
                    ptr[n] = 0;
                else
                    ptr[n] = 255;
            }
        }
        string saveFile = "./mnist_train/" + to_string((unsigned int)label) + "_" + to_string(i) + ".jpg";
        imwrite(saveFile, image);
    }
}

int main()
{
    //readAndSave("./t10k-images.idx3-ubyte", "./t10k-labels.idx1-ubyte");
    readAndSave("train-images.idx3-ubyte", "train-labels.idx1-ubyte");
    return 0;
}
前面提到的大端模式是指資料的高位元組儲存在記憶體的低地址中,而資料的低位元組儲存在記憶體的高地址中,這樣的儲存模式有點兒類似於把資料當作字串順序處理:地址由小向大增加,而資料從高位往低位放; 小端模式是我們平時常用的模式,比如十進位制的1用二進位制小端模式存放則為0000 0001。 結果如下圖所示: