After preparing the dataset, next task is loading the dataset. We will implement a function in Matlab to load the dataset. You can use this function to load other dataset as well.  Let’s name it ‘load_database.m’. You can copy the code from here –

%Author Nuruzzaman Faruqui
%You are free to use, modify or distribute this code
function output_value = load_database();
persistent loaded;
persistent numeric_Image;
if(isempty(loaded))
    all_Images = zeros(10304,40);
    for i=1:40
        cd(strcat('s',num2str(i)));
        for j=1:10
            image_Container = imread(strcat(num2str(j),'.pgm'));
            all_Images(:,(i-1)*10+j)=reshape(image_Container,size(image_Container,1)*size(image_Container,2),1);
        end
        display('Doading Database');
        cd ..
    end
    numeric_Image = uint8(all_Images);
end
loaded = 1;
output_value = numeric_Image;

In the first line, I have declared the name of the function ‘load_database()’ It doesn’t take any input. That is why I have used empty parenthesis. But it returns numeric form of images. The images to be returned will be stored in a variable named ‘ouput_value’.

After that I have taken two more variables named ‘loaded’ and ‘numeric_Images’. These variables are persistent type.

“ The persistent variables are local to the function in which they are declared; yet their values are retained in memory between calls to the function. Persistent variables are similar to global variables because MATLAB creates permanent storage for both. They differ from global variables in that persistent variables are known only to the function in which they are declared. This prevents persistent variables from being changed by other functions or from the MATLAB command line – source: http://matlab.izmiran.ru/help/techdoc/ref/persistent.html ] “

After that, in the ‘if’ condition, I have checked if the ‘loaded’ variable is empty. If it is empty only then we will load the dataset. The persistent variables permanently stores the data. And we need to load the dataset only once. That is why it is important to check if the variable is empty or not at the beginning.

We have 40 images. Each of them have 92 x 112 = 10304 pixels. That is why we have to take 10304 zeros for 40 times. Later the pixel values of the images will replace these ‘zeros’. Then inside the ‘for loop’ using strcat function, I am concatenating the names of the folders (s1, s2, s3 and so on), names of the images (1, 2, 3, and so on) and the extension of the images (I used .pgm images). (If you do not know how ‘strcat function’ works, watch this short explanation – What is strcat function in Matlab?)

After that using ‘imread’ function, I’m loading the images. It is necessary to reshape the images. After loading the images I have used ‘reshape’ function to convert the images into single column matrix. I also used ‘size’ function which gives the size of the rows and columns of the images.

After that I have converted the images into 8 bit unsigned integer to reduce the memory usage. Using ‘uint8’ function, we can directly convert data into 8 bit unsigned integer in Matlab. You might have noticed I used ‘loaded = 1’ before the last line. It is to prevent the function from further loading the dataset. There is an ‘if’ condition before the ‘for loop’. It will becomes false when the ‘loaded’ variable has a value. Finally this function returns the images in 8 bit unsigned integer form.

<< Previous Step                   Next Step >>