1. 程式人生 > >[2017-08-25] 將MNIST資料集轉換成圖片形式並儲存

[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);