[2017-08-25] 将MNIST数据集转换成图片形式并保存

先开个头
clear;
clc;
close all;

%% Open files
image_file_name = 'train-images-idx3-ubyte';
label_file_name = 'train-labels-idx1-ubyte';
 
TrainImageData = fopen(image_file_name,'r');
TrainLabelData = fopen(label_file_name,'r');

TrainImages_Info = fread(TrainImageData,16,'uint8');
TrainLabels_Info = fread(TrainLabelData,8,'uint8');

TrainMagicNum = ((TrainImages_Info(1)*256 + TrainImages_Info(2))*256 + TrainImages_Info(3))*256 + TrainImages_Info(4);
TrainImageNum = ((TrainImages_Info(5)*256 + TrainImages_Info(6))*256 + TrainImages_Info(7))*256 + TrainImages_Info(8);
TrainImageRow = ((TrainImages_Info(9)*256 + TrainImages_Info(10))*256 + TrainImages_Info(11))*256 + TrainImages_Info(12);
TrainImageCol = ((TrainImages_Info(13)*256 + TrainImages_Info(14))*256 + TrainImages_Info(15))*256 + TrainImages_Info(16);

ImageBuf = zeros(1,TrainImageRow*TrainImageCol);
sum0 = 0;
sum1 = 0;
sum2 = 0;
sum3 = 0;
sum4 = 0;
sum5 = 0;
sum6 = 0;
sum7 = 0;
sum8 = 0;
sum9 = 0;

%% Extract the Training Images
for iter = 1:TrainImageNum
    ImageBuf = fread(TrainImageData,TrainImageRow*TrainImageCol,'uint8');
    Label = fread(TrainLabelData,1,'uint8');
    Image = reshape(ImageBuf,[TrainImageRow,TrainImageCol]);
    Image = Image';
    Image = uint8(Image);
    if Label == 0
        sum0 = sum0 + 1;
        %imshow(Image);
        imwrite(Image,strcat('.\My_Train\0\','Image_',num2str(sum0),'.png'),'png');
        %pause(0.1);
    elseif Label == 1
        sum1 = sum1 + 1;
        %imshow(Image);
        imwrite(Image,strcat('.\My_Train\1\','Image_',num2str(sum1),'.png'),'png');
        %pause(0.1);
    elseif Label == 2
        sum2 = sum2 + 1;
        %imshow(Image);
        imwrite(Image,strcat('.\My_Train\2\','Image_',num2str(sum2),'.png'),'png');
        %pause(0.1);
    elseif Label == 3
        sum3 = sum3 + 1;
        %imshow(Image);
        imwrite(Image,strcat('.\My_Train\3\','Image_',num2str(sum3),'.png'),'png');
        %pause(0.1);
    elseif Label == 4
        sum4 = sum4 + 1;
        %imshow(Image);
        imwrite(Image,strcat('.\My_Train\4\','Image_',num2str(sum4),'.png'),'png');
        %pause(0.1);
    elseif Label == 5
        sum5 = sum5 + 1;
        %imshow(Image);
        imwrite(Image,strcat('.\My_Train\5\','Image_',num2str(sum5),'.png'),'png');
        %pause(0.1);
    elseif Label == 6
        sum6 = sum6 + 1;
        %imshow(Image);
        imwrite(Image,strcat('.\My_Train\6\','Image_',num2str(sum6),'.png'),'png');
        %pause(0.1);
    elseif Label == 7
        sum7 = sum7 + 1;
        %imshow(Image);
        imwrite(Image,strcat('.\My_Train\7\','Image_',num2str(sum7),'.png'),'png');
        %pause(0.1);
    elseif Label == 8
        sum8 = sum8 + 1;
        %imshow(Image);
        imwrite(Image,strcat('.\My_Train\8\','Image_',num2str(sum8),'.png'),'png');
        %pause(0.1);
    else
        sum9 = sum9 + 1;
        %imshow(Image);
        imwrite(Image,strcat('.\My_Train\9\','Image_',num2str(sum9),'.png'),'png');
        %pause(0.1);
    end
    disp(['Processing Image: ', num2str(iter)])
end

%% Close Files
fclose(TrainImageData);
fclose(TrainLabelData);

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值