function [emean,estdev,enoise] = cv_est_noise(image_set)
%
% cv_est_noise - estimate the Gaussian noise in an image
% Call: [emean,estdev,enoise] = cv_est_noise(image_set);
% On input:
%     image_set: an m by n by p image set of p images (each m x n)
% On output:
%     emean: m x n matrix of mean pixel values in image_set 
%     estdev: m x n matrix of stdev pixel values
%     enoise: estimate of standard deviation of entire image_set
% Author:
%     Tom Henderson
%     7 January 1999
% Custom Functions Used:
%     none
% Method:
%     described in algorithm on p. 31 of Torre and Mocci, Intro 3D Computer Vision
% Testing:
% > is = randn(50,50,25);
% > [em,es,en] = cv_est_noise(is);
% > en
%
% en =
%
%    0.9923
%
% > mean2(em)
%
% ans =
%
%    0.0088
%
% > mean2(es)
%
% ans =
%
%    0.9923
%
% >
%     

[num_rows,num_cols,num_images] = size(image_set);

im_sum = zeros(num_rows,num_cols);   % mean image
for im = 1:num_images
   im_sum = im_sum + image_set(:,:,im);
end
emean = im_sum/num_images;

im_sum = zeros(num_rows,num_cols);   % standard deviation image
for im = 1:num_images
   im_sum = im_sum + (image_set(:,:,im)-emean).^2;
end
estdev = sqrt(im_sum/(num_images-1));

enoise = mean2(estdev);    % global image standard deviation
