% Program to improve S/N ratio in small factor steps of 2^0.1 = 1.0718 of a
% 16-bit tif photograph (copyright Nyah Willmott)

clear; close all;
vid = VideoWriter('poissonNoiseCroppedRegion.mp4','MPEG-4');
vid.Quality = 100;
vid.FrameRate = 15;
open(vid);
set(0,'defaultfigurecolor',[1 1 1]);
set(gca,'linewidth',7);

I = imread('zurich.tif','PixelRegion',{[1554,1680],[1760,1873]}); % input image
J = rgb2gray(I); % convert to b&w
for i = 16:-0.1:0
    factor = 2^i;
    L = J/factor; % reduce bit range. Reduces each pixel to round(val/factor)
    M = imnoise(L,'poisson');
    N = M*factor;
    imshow(N);
    bits = round(16.4 - i);
    str1 = num2str(bits,'% 2i');
    if (bits >=1 )
        if (bits == 1)
            str2 = ' bit';
        else
            str2 = ' bits';
        end
        strTot = [str1,str2];
        hText = text(1600, 1190, strTot, 'FontSize',16,'Color','white');
    end
    frame = getframe(gcf);
    writeVideo(vid,frame);
end
% Output the movie as an mpg file
close(vid);