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

C++ 在C+中读取MNIST数据库+;。所有像素值均为零,c++,C++,我正在尝试读取测试图像文件,并成功读取指定的头参数(幻数、图像数、行数等),但保持读取单个像素值的零。我创建了一个if条件,程序将打印出一些非零像素值,然后从函数返回。我在一台小小的endian计算机上工作,因此我必须反转输入。有人知道为什么所有的像素输入都是零吗 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> #include <v

我正在尝试读取测试图像文件,并成功读取指定的头参数(幻数、图像数、行数等),但保持读取单个像素值的零。我创建了一个if条件,程序将打印出一些非零像素值,然后从函数返回。我在一台小小的endian计算机上工作,因此我必须反转输入。有人知道为什么所有的像素输入都是零吗

#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;
}
#包括
#包括
#包括
#包括
#包括
#包括
void loadDatabase(int NumberOfImages、int dataofimages、std::vector和arr);
int main()
{
printf(“启动加载数据库…”\n);
矢量图像;
loadDatabase(60000784,图像);
printf(“我们已成功加载数据库\n”);
返回0;
}
使用名称空间std;
//inout MNIST数据库的格式在其网站中指定。
//我们需要翻转输入的订单,然后将图像放入一维阵列中。
//反向输入功能
int反向输入(int i)
{
无符号字符ch1、ch2、ch3、ch4;//32位
ch1=i&255;
ch2=(i>>8)和255;
ch3=(i>>16)和255;
ch4=(i>>24)和255;

return((int)ch1欢迎使用堆栈溢出。请尽快阅读和页面。当输出为简单文本时,不要发布指向图像的链接。复制“n”将文本粘贴到问题中,并像编写代码一样缩进。
temp=ReverseInt(temp)
删除此项-反转一个只有一个字节大的值没有任何意义。您获取一个
无符号字符,其值在[0,255]范围内。您将其转换为整数,在同一范围内-因此低阶字节为[0-255],其余为零。您反转该整数-现在高阶字节为[0-255]剩下的是零。最后,将该整数转换回
无符号字符
,它采用低阶字节-始终为零。删除该位代码确实有效,并且确实有意义。谢谢。我还将阅读关于以及如何要求页面向前移动。欢迎使用堆栈溢出。请阅读和页面很快。当输出为简单文本时,不要发布到图像的链接。复制并粘贴文本到问题中,并像编写代码一样缩进它。
temp=ReverseInt(temp);
删除此项-反转一个只有一个字节大的值是没有意义的。您获取一个
无符号字符,其值范围为[0,255]。将其转换为相同范围内的整数-因此低阶字节为[0-255],其余为零。将该整数反转-现在高阶字节为[0-255]剩下的是零。最后,将该整数转换回无符号字符
,它采用低阶字节-始终为零。删除该位代码确实有效,而且确实有意义。谢谢。我还将阅读关于以及如何询问页面前进。