Spectre
Spectre

Reputation: 684

Trouble saving and loading mat files

I am trying to combine a large set of images into a .mat file. The images are 512x512, grayscale. There are 31 such images.

I did the following for creating the matfile and saving it:

    fid1 = fopen('C:\Users\...\MATLAB\fileList_IMG.txt','r');
    PATH_IMG = 'C:\Users\...\IMAGES\';
    PATH_MAT = 'C:\Users\...\IMAGES\MATfiles\';
    IMG_DATA = zeros(512, 512, 31);
    while ~feof(fid1)
        folderName = fgetl(fid1);

        for i=1:31
            fileName = sprintf('%s%s\\%s\\%s_%02d.png',PATH_IMG, folderName, folderName, folderName, i);
            tempImg = imread(fileName);
            IMG_DATA(:,:,i) = tempImg(:,:,1);
        end

        save_fileName = sprintf('%s%s', PATH_MAT, folderName);
        save(save_fileName, 'IMG_DATA');
    end

I did the following for loading the matfile back:

     fileName = 'C:\Users\...\IMAGES\MATfiles\balloon.mat';
     NEWIMG_DATA = load(fileName);

Lets say that this is new_IMG_DATA for sake of readability.

I notice that the values are different! Its not like the values were scaled up because values of 170 and 172 in IMG_DATA correspond to 3709 and 3666 in new_IMG_DATA. Both, IMG_DATA and new_IMG_DATA are of double data type. if I force IMG_DATA to uint8 before creating the .mat file, most of the values of new_IMG_DATA are 255.

Here are some samples of IMG_DATA before saving it as a matfile: IMG_DATA(400,400,4:10)

ans(:,:,1) = 133

ans(:,:,2) = 141

ans(:,:,3) = 142

ans(:,:,4) = 145

ans(:,:,5) = 156

ans(:,:,6) = 157

ans(:,:,7) = 158

Here is a sample of new_IMG_DATA after loading it back: NEWIMG_DATA.IMG_DATA(400,400,4:10)

ans(:,:,1) = 16366

ans(:,:,2) = 18216

ans(:,:,3) = 19648

ans(:,:,4) = 19578

ans(:,:,5) = 19203

ans(:,:,6) = 18682

ans(:,:,7) = 17123

I want to able to save and load the .mat file properly. How do I this?

Upvotes: 0

Views: 574

Answers (1)

Spectre
Spectre

Reputation: 684

The answer is in the comments above. ypnos pointed out that the data is of 16 bpp rather than 8 bpp as I had assumed.

Upvotes: 1

Related Questions