当前位置:网站首页>Opencv learning notes (II): reading MNIST datasets
Opencv learning notes (II): reading MNIST datasets
2022-06-13 12:11:00 【MirrorYuChen】
1.mnist brief introduction
Dataset home page link :
MNIST handwritten digit database, Yann LeCun, Corinna Cortes and Chris Burges
http://yann.lecun.com/exdb/mnist/ There are four documents :
train-images-idx3-ubyte: Training set image 60000 Zhang Size 28x28 grayscale
train-labels-idx1-ubyte: Training set label 60000 individual [0,9]
t10k-images-idx3-ubyte: Test set image 10000 Zhang Size 28x28 grayscale
t10k-labels-idx1-ubyte: Test set label 10000 individual [0,9]For image files , The internal data format is as follows :
magic number:magic Count
image number: Number of images
image width : The width of the image
image height: Height of the image
image data1 : Image data 1
.
.
.
image dataN : Image data NFor label files , The internal data format is as follows :
magic number:magic Count
label number: Number of tags
label1 : label 1
.
.
.
labelN : label N2.mnist Data analysis
In fact, it is based on the internal arrangement order of the file , Read the corresponding contents in turn , The specific code is as follows :
#include "opencv2/opencv.hpp"
#include <iostream>
#ifndef uchar
# define uchar unsigned char
#endif
int ReadNumber(FILE* fp, int len) {
uchar* buffer = new uchar[len];
fread(buffer, len, 1, fp);
int result = 0;
for (int i = 0; i < len; ++i) {
int temp = buffer[i];
result += temp << ((len - 1 - i) * 8);
}
delete[] buffer;
return result;
}
void ReadImage(FILE* fp, int len, uchar* buffer) {
fread(buffer, len, 1, fp);
}
int main() {
const char* trainImgFile = "./train-images.idx3-ubyte";
const char* trainLabelFile = "./train-labels.idx1-ubyte";
FILE* imgIn = fopen(trainImgFile, "rb");
if (imgIn == NULL) {
std::cout << "open: " << trainImgFile << "failed." << std::endl;
return -1;
}
fseek(imgIn, 0, 0);
int imgMagic = ReadNumber(imgIn, 4);
int imgNum = ReadNumber(imgIn, 4);
int imgWidth = ReadNumber(imgIn, 4);
int imgHeight = ReadNumber(imgIn, 4);
std::cout << "magic: " << imgMagic << " imgNum: " << imgNum << " imgWidth: " <<
imgWidth << " imgHeight: " << imgHeight << std::endl;
if (imgMagic != 2051) {
std::cout << "error image magic number: " << imgMagic << std::endl;
fclose(imgIn);
return -1;
}
int imgSize = imgWidth * imgHeight;
uchar* buffer = new uchar[imgSize];
for (int i = 0; i < imgNum; ++i) {
ReadImage(imgIn, imgSize, buffer);
cv::Mat img = cv::Mat(imgHeight, imgWidth, CV_8UC1, buffer);
cv::imwrite("train/image/" + std::to_string(i) + ".jpg", img);
}
delete[] buffer;
fclose(imgIn);
FILE* labelIn = fopen(trainLabelFile, "rb");
int labelMagic = ReadNumber(labelIn, 4);
int labelNum = ReadNumber(labelIn, 4);
if (labelMagic != 2049) {
std::cout << "error label magic number: " << labelMagic << std::endl;
fclose(labelIn);
return -1;
}
for (int i = 0; i < labelNum; ++i) {
int label = ReadNumber(labelIn, 1);
std::cout << "i: " << i << " label: " << label << std::endl;
}
fclose(labelIn);
return 0;
}
Let's talk about the analysis here Number Why do you write like this , Why should we introduce a int Type of temp, Mainly uchar The shift operation will be truncated , So here we assign a value to int Then, perform the shift operation .
Reference material :
边栏推荐
- Machine learning (II) - logical regression theory and code explanation
- Seccloud and trend technology jointly release the overall solution of container cloud platform and GPU resource pooling
- kubernetes 部署 ActiveMQ
- Camunda timer events example demo (timer events)
- (一)爬取Best Sellers的所有分类信息:爬取流程
- 论文翻译 | PointNet: Deep Learning on Point Sets for 3D Classification and Segmentation
- Text error correction -- crisp model
- Projet de développement web, développement d'une page Web
- 004、torchserve 调用LR二分类预测
- 基于STM32F103——SIM900A发送短信+串口打印
猜你喜欢

Envoyer un SMS - système de carte d'accès intelligent basé sur stm32f103 + as608 module d'empreintes digitales + clé matricielle 4x4 + sim900a

Web development video tutorial, web development teaching

Analysis of different dimensions of enterprise evaluators: enterprise evaluation of Gansu Power Investment Capital Management Co., Ltd

基于三维GIS技术的行业发展及研究现状

web開發項目,web單頁開發

SMS sending + serial port printing based on stm32f103-sim900a

Web development project, web single page development

2022年二建《公路》科目答案已出,请收好

Interview questions MySQL database

CPU的分支预测
随机推荐
Lucene从入门到实战
Kubernetes问题整理
Analysis of DuPont analysis method: financial analysis of the New Retail Group Co., Ltd
机器学习(二)—逻辑回归理论与代码详解
LVGL库入门教程01-移植到STM32(触摸屏)
Adaptation of multi system docking and application of packaging mode
[tcapulusdb knowledge base] Introduction to tcapulusdb tcapsvrmgr tool (III)
Problems encountered in using the Pluto table of the flutter plug-in
Machine learning (III) - LDA (linear discriminant analysis) theory and code explanation
[tcapulusdb knowledge base] Introduction to tcapulusdb tcapsvrmgr tool (I)
Web developer, web development background development
Interview shock 56: what is the difference between clustered index and non clustered index?
Envoyer un SMS - système de carte d'accès intelligent basé sur stm32f103 + as608 module d'empreintes digitales + clé matricielle 4x4 + sim900a
【TcaplusDB知识库】TcaplusDB-tcapulogmgr工具介绍(一)
陈宏智:字节跳动自研万亿级图数据库ByteGraph及其应用与挑战
燃油方案和产品业务建模
What if the second construction fails to pass the post qualification examination? This article tells you
二建资格后审没通过怎么办?这篇文章告诉你
即构推出行业首个数据流录制PaaS方案,低成本复刻头部大厂录制能力
web开发项目,web单页开发