function [img_buffer, num_imgs, img_dim] = LoadImgData(dataset_path, filename_filter, preview_img_flag)
%% Load image, downsample, vectorize, and store in a buffer
%  ECE 792-41 Statistical Foundations for Signal Processing and Machine Learning
%  Prof. Chau-Wai Wong, ECE, NC State University, Sept. 2020

img_width_desired = 20;
img_width_for_visualization = 200;

dir_results = dir([dataset_path filename_filter]);

num_imgs = length(dir_results);
img_buffer = cell(num_imgs, 1);
for i = 1 : num_imgs
  fn = [dataset_path dir_results(i).name];
  img_raw = double(imread(fn)) / 255;  % convert pixel intensity to [0, 1] in double
  img_small = imresize(img_raw, [nan img_width_desired]);  % downsample image to lower computational cost
  img_buffer{i} = img_small(:);  % vectorize each image
  
  if preview_img_flag
    imshow(imresize(img_small, [nan img_width_for_visualization], 'nearest'), [0 1]);
    pause(0.1);
  end
end

img_dim = size(img_small);
