mnist文書の解析のtensorflow開発

  正規化ドキュメント、サブトレーニングセットとテストは、2つのセクションを設定した後のデジタル手書き画像、情報の合成である自体をmnist、各セクションには、画像が含まれており、本明細書で使用される、二つの文書にタグを付ける、C ++デモ文書復号に書き込まれ、OpenCVのによって意志画像データは、通常の文書として保存されます。ソースのダウンロード:https://github.com/zacSuo/mnist

  • t10k-images.idx3-UBYTE
  • t10k-labels.idx1-UBYTE
  • 列車images.idx3-UBYTE
  • 列車labels.idx1-UBYTE

説明書

環境要件

  • Visual Studioの2012年以降
  • OpenCVの2.4以上

    コンフィギュレーション

    プロジェクトのプロパティを介して第1を開始する前に、地元のOpenCVのパスを設定します。

結果

  2つのドキュメントディレクトリのフォルダでは、それぞれテストセット(test_images)とトレーニングセット(train_images)そこに画像を保存します。

プロセスの説明

復号画像

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 :: 文字列のファイル名、のstd :: ベクトル <CV ::マット>&VEC)
{ INT NUMBER_OF_IMAGES = 0std :: はifstreamのファイル(ファイル名、STD ::イオス::バイナリ)もし(file.is_open()){ int型 magic_number = 0int型 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)。

以下のためにint型私= 0 ; iがNUMBER_OF_IMAGESを<; ++ i)が{
CV ::マットTP = CV ::マット::ゼロ(n_rows、n_cols、CV_8UC1)。INT R = 0 ; R <n_rows; ++ R){ ためINT C = 0 ; C <n_cols、C ++){ 符号なしチャー TEMP = 0 file.read((CHAR *)&一時、はsizeof(TEMP)); tp.at <UCHAR>(R、C)=(INT)TEMP。 } } vec.push_back(TP)。 }










file.close();
} 戻り NUMBER_OF_IMAGESと、}


復号された画像に対応する値

1 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 read_Mnist_Label STD :: 文字列のファイル名、のstd :: ベクトル < 整数 >&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 :: 文字列 output_image = images_path + "result.png"
CV ::関数imwrite(output_image、DST); リターン0 ; }


オリジナルリンク大列  https://www.dazhuanlan.com/2019/08/25/5d61bcc0916e4/

おすすめ

転載: www.cnblogs.com/chinatrump/p/11415219.html