Introduction and conversion of MNIST Databases

Source: Internet
Author: User

Introduction and conversion of MNIST Databases

MNIST database Introduction: MNIST is a handwritten digital database with 60000 training samples and 10000 test samples. It is a subset of NIST databases.

MNIST database official website is: http://yann.lecun.com/exdb/mnist/, such. Download four files and decompress them. After decompression, it is found that these files are not in the standard image format. These image data are stored in binary files. The width and height of each sample image are 28*28.

The following code converts a jpg image to a normal one:

# Include <iostream>
# Include <fstream>

# Include "opencv2/core. hpp"
# Include "opencv2/highgui. hpp"
# Include "opencv2/imgproc. hpp"

Using namespace std;

Int ReverseInt (int I)
{
Unsigned char values, ch2, ch3, and methane;
Latency = I & 255;
Ch2 = (I> 8) & 255;
Ch3 = (I >>16) & 255;
Methane = (I> 24) & 255;
Return (int) Forward <24) + (int) ch2 <16) + (int) ch3 <8) + methane;
}

Void read_Mnist (string filename, vector <cv: Mat> & vec)
{
Ifstream file (filename, ios: binary );
If (file. is_open ()){
Int magic_number = 0;
Int number_of_images = 0;
Int n_rows = 0;
Int n_cols = 0;
File. read (char *) & magic_number, sizeof (magic_number ));
Magic_number = ReverseInt (magic_number );
File. read (char *) & number_of_images, sizeof (number_of_images ));
Number_of_images = ReverseInt (number_of_images );
File. read (char *) & n_rows, sizeof (n_rows ));
N_rows = ReverseInt (n_rows );
File. read (char *) & n_cols, sizeof (n_cols ));
N_cols = ReverseInt (n_cols );

For (int I = 0; I <number_of_images; ++ I ){
Cv: Mat tp = cv: Mat: zeros (n_rows, n_cols, CV_8UC1 );
For (int r = 0; r <n_rows; ++ r ){
For (int c = 0; c <n_cols; ++ c ){
Unsigned char temp = 0;
File. read (char *) & temp, sizeof (temp ));
Tp. at <uchar> (r, c) = (int) temp;
}
}
Vec. push_back (tp );
}
}
}

Void read_Mnist_Label (string filename, vector <int> & vec)
{
Ifstream file (filename, ios: binary );
If (file. is_open ()){
Int magic_number = 0;
Int number_of_images = 0;
Int n_rows = 0;
Int n_cols = 0;
File. read (char *) & magic_number, sizeof (magic_number ));
Magic_number = ReverseInt (magic_number );
File. read (char *) & number_of_images, sizeof (number_of_images ));
Number_of_images = ReverseInt (number_of_images );

For (int I = 0; I <number_of_images; ++ I ){
Unsigned char temp = 0;
File. read (char *) & temp, sizeof (temp ));
Vec [I] = (int) temp;
}
}
}

String GetImageName (int number, int arr [])
{
String str1, str2;

For (int I = 0; I <10; I ++ ){
If (number = I ){
Arr [I] ++;
Char limit [10];
Sprintf (histogram, "% d", arr [I]);
Str1 = std: string (separator );

If (arr [I] <10 ){
Str1 = "0000" + str1;
} Else if (arr [I] <100 ){
Str1 = "000" + str1;
} Else if (arr [I] <1000 ){
Str1 = "00" + str1;
} Else if (arr [I] <10000 ){
Str1 = "0" + str1;
}

Break;
}
}

Char ch2 [10];
Sprintf (ch2, "% d", number );
Str2 = std: string (ch2 );

Str2 = str2 + "_" + str1;

Return str2;
}

Int main ()
{
// Reference: http://eric-yuan.me/cpp-read-mnist/
// Test images and test labels
// Read MNIST image into OpenCV Mat vector
String filename_test_images = "D: // Download/t10k-images-idx3-ubyte/t10k-images.idx3-ubyte ";
Int number_of_test_images = 10000;
Vector <cv: Mat> vec_test_images;

Read_Mnist (filename_test_images, vec_test_images );

// Read MNIST label into int vector
String filename_test_labels = "D:/Download/t10k-labels-idx1-ubyte/t10k-labels.idx1-ubyte ";
Vector <int> vec_test_labels (number_of_test_images );

Read_Mnist_Label (filename_test_labels, vec_test_labels );

If (vec_test_images.size ()! = Vec_test_labels.size ()){
Cout <"parse MNIST test file error" <endl;
Return-1;
}

// Save test images
Int count_digits [10];
For (int I = 0; I <10; I ++)
Count_digits [I] = 0;

String save_test_images_path = "D:/Download/MNIST/test_images /";

For (int I = 0; I <vec_test_images.size (); I ++ ){
Int number = vec_test_labels [I];
String image_name = GetImageName (number, count_digits );
Image_name = save_test_images_path + image_name + ". jpg ";

Cv: imwrite (image_name, vec_test_images [I]);
}

// Train images and train labels
// Read MNIST image into OpenCV Mat vector
String filename_train_images = "D:/Download/train-images-idx3-ubyte/train-images.idx3-ubyte ";
Int number_of_train_images = 60000;
Vector <cv: Mat> vec_train_images;

Read_Mnist (filename_train_images, vec_train_images );

// Read MNIST label into int vector
String filename_train_labels = "D:/Download/train-labels-idx1-ubyte/train-labels.idx1-ubyte ";
Vector <int> vec_train_labels (number_of_train_images );

Read_Mnist_Label (filename_train_labels, vec_train_labels );

If (vec_train_images.size ()! = Vec_train_labels.size ()){
Cout <"parse MNIST train file error" <endl;
Return-1;
}

// Save train images
For (int I = 0; I <10; I ++)
Count_digits [I] = 0;

String save_train_images_path = "D:/Download/MNIST/train_images /";

For (int I = 0; I <vec_train_images.size (); I ++ ){
Int number = vec_train_labels [I];
String image_name = GetImageName (number, count_digits );
Image_name = save_train_images_path + image_name + ". jpg ";

Cv: imwrite (image_name, vec_train_images [I]);
}

Return 0;
}

This article permanently updates the link address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.