tensorflow development of mnist document parsing

  mnist itself is a digital handwritten image, information synthesis after normalization documents, sub-training set and test set two sections, each section contains pictures and tag two documents, as used herein, written in C ++ Demo document decoding, and by opencv will the image data is stored as regular documents. Source Download: https://github.com/zacSuo/mnist

  • t10k-images.idx3-ubyte
  • t10k-labels.idx1-ubyte
  • train-images.idx3-ubyte
  • train-labels.idx1-ubyte

Instructions for use

Environmental requirements

  • visual studio 2012 or later
  • opencv 2.4 and above

    Configuration

    Before starting the first through the project properties, set the path of the local opencv.

Results of the

  In the two documents directory folder, respectively to save the test set (test_images) and the training set (train_images) pictures into it.

Process Description

Decoded image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
int (std::string filename, std::vector<cv::Mat> &vec)
{
int number_of_images = 0;
std::ifstream file(filename, std::ios::binary);
if (file.is_open()) {
int magic_number = 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);
}

file.close();
}
return number_of_images;
}

Value corresponding to a decoded image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void read_Mnist_Label(std::string filename, std::vector<int> &vec)
{
std::ifstream file(filename, std::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;
}

file.close();
}
}

生成图像文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int decodeDatabase(std::string filenameImages,std::string filenameLabels,std::string imagesPathSave)
{

std::vector<cv::Mat> vec_train_images;
int number_of_train_images = read_Mnist(filenameImages, vec_train_images);


std::vector<int> vec_train_labels(number_of_train_images);
read_Mnist_Label(filenameLabels, vec_train_labels);

if (vec_train_images.size() != vec_train_labels.size()) {
std::cout << "parse MNIST train file error" << std::endl;
return -1;
}

// save images
int count_digits[10];
std::fill(&count_digits[0], &count_digits[0] + 10, 0);

for (int i = 0; i < vec_train_images.size(); i++) {
int number = vec_train_labels[i];
std::string image_name = GetImageName(number, count_digits);
image_name = imagesPathSave + image_name + ".jpg";

cv::imwrite(image_name, vec_train_images[i]);
}
return 0;
}

每个数值各选20张图像合并成一个图像文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
int mergeSomeImages()
{
std::string images_path = ".././mnistFile/train_images/";
int width = 28 * 20;
int height = 28 * 10;
cv::Mat dst(height, width, CV_8UC1);

for (int i = 0; i < 10; i++) {
for (int j = 1; j <= 20; j++) {
int x = (j-1) * 28;
int y = i * 28;
cv::Mat part = dst(cv::Rect(x, y, 28, 28));

std::string str = std::to_string(j);
if (j < 10)
str = "0000" + str;
else
str = "000" + str;

str = std::to_string(i) + "_" + str + ".jpg";
std::string input_image = images_path + str;

cv::Mat src = cv::imread(input_image, 0);
if (src.empty()) {
fprintf(stderr, "read image error: %sn", input_image.c_str());
return -1;
}

src.copyTo(part);
}
}

std::string output_image = images_path + "result.png";
cv::imwrite(output_image, dst);
return 0;
}

Original link large column  https://www.dazhuanlan.com/2019/08/25/5d61bcc0916e4/

Guess you like

Origin www.cnblogs.com/chinatrump/p/11415219.html