用C ++读取MNIST数据库。所有像素值均为零

时间:2016-11-13 23:43:07

标签: c++

我试图读取测试图像文件并成功读取指定的标题参数(幻数,图像数,行数等),但保持为各个像素值读取零。我创建了一个if条件,程序将打印出一些非零像素值,然后从函数返回。我正在使用小端计算机,因此我必须反转输入。有谁知道为什么所有的像素输入都是零?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <fstream>

void loadDatabase(int NumberOfImages, int DataOfAnImage,std::vector<std::vector<double> >&arr);

int main()
{
    printf("Starting load database......\n");

    std::vector<std::vector<double> > images;
    loadDatabase(60000,784,images);

    printf("We succesfully have loaded the database\n");
return 0;

}
 using namespace std;

// The format of the inout MNIST Database is specefied within their website.
// We are required to flip the the incoming order and then place the images into a 1-D array.
// Reversing input function
int ReverseInt (int i)
{
    unsigned char ch1, ch2, ch3, ch4; // The 32 bit 
    ch1=i&255;
    ch2=(i>>8)&255;
    ch3=(i>>16)&255;
    ch4=(i>>24)&255;
    return((int)ch1<<24)+((int)ch2<<16)+((int)ch3<<8)+ch4;
}

void loadDatabase(int NumberOfImages, int DataOfAnImage,std::vector<std::vector<double> >&arr)
{
    //Creating the appropriatly sized array in C++
    arr.resize(NumberOfImages,vector<double>(DataOfAnImage));
    // Creating the buffer named "file" from the location on the computer
    std::ifstream file ("/Users/images",ios::binary);
    //file.is_open checks to see whether our stream is open and if so we cotinue
    if (file.is_open())
    {
        // Magic number is defined in the MNIST database. The magic number is an integer where the first 2 bytes are zero
        // and the 3rd byte represents the type of code, and the 4th byte determines the dimensions of the matrix/vector;
        int magic_number=0;
        //Number of images is images in the whole testing labels/images file
        int number_of_images=0;
        //Cols and rows
        int n_rows=0;
        int n_cols=0;
        //We read the first integer from the file
        file.read ((char*)&magic_number,sizeof(magic_number)); 
    printf("magic number = %d\n", magic_number);
    //Flip the magic number since the MNIST database is organized as per Big-Endian
        magic_number= ReverseInt(magic_number); 
    printf("reversed magic number = %d\n", magic_number);
     //Read the next integer that represents the number of images
        file.read((char*)&number_of_images,sizeof(number_of_images));
    printf("number of images = %d\n", number_of_images);
        //Again reverse (big-endian -> little-endian)
        number_of_images= ReverseInt(number_of_images); 
    printf("reversed number of images = %d\n", number_of_images);
     //Read rows
        file.read((char*)&n_rows,sizeof(n_rows));
        n_rows= ReverseInt(n_rows);
        //Read cols
        file.read((char*)&n_cols,sizeof(n_cols));
        n_cols= ReverseInt(n_cols);
        // From here forward we access the individual images that are stored in consecutive locations each 32 bits. 
        // The image pixels are placed in a 1-D array.       
    int flag =0;
        for(int i=0;i<number_of_images;++i)
        {
            for(int r=0;r<n_rows;++r)
            {
                for(int c=0;c<n_cols;++c)
                {
                    // The MNIST database only stores in black and white and thus we need only read 1 byte of information per pixel.
                    // The images themselves are arranged in little-endian
                    unsigned char temp = 1; 
                    file.read((char*)&temp,sizeof(temp));
            temp = ReverseInt(temp);
            if(temp !=0)
            {   flag++;
                printf("temp[%d][(%d*%d)+%d] = %d = %x\n", i, n_rows, r, c, temp, temp); 
                if(flag >9)
                    return;
            }
                    arr[i][(n_rows*r)+c]= (double)temp;
           // printf("temp = %d = %x\n", temp, temp); 
           // printf("arr[%d][(%d*%d)+%d]=%x\n", i, n_rows, r, c, arr[i][(n_rows*r)+c]);
                }
            }
        }
    }
return;
}

输出如下:

There are no "temp" non-zero values printed as can be seen in the output

0 个答案:

没有答案
相关问题