%Chapter 8,  Fig 8.7
%Eric Dubois, updated 2019-01-25
%Illustrate a Bayer CFA image
%Data file should be placed in a data directory on the MATLAB path.
clear all, close all;
RGBin = im2double(imread('05.tif'));
figure, imshow(RGBin);
N1 = size(RGBin,1);
N2 = size(RGBin,2);
Z = 0.35;  %set missing components to Z
%downsample the RGB components according to the Bayer pattern
%Upper left pixel is green, pixel to right of it is red, pixel below it is
%blue
%
% G R
% B G
%
%RGB_bayer = zeros(size(RGBin));
RGB_bayer = Z*ones(size(RGBin));
%Write four cosets of RGBin to RGB_bayer
RGB_bayer(1:2:N1,2:2:N2,1) = RGBin(1:2:N1,2:2:N2,1); %red
RGB_bayer(1:2:N1,1:2:N2,2) = RGBin(1:2:N1,1:2:N2,2); %green
RGB_bayer(2:2:N1,2:2:N2,2) = RGBin(2:2:N1,2:2:N2,2); %green
RGB_bayer(2:2:N1,1:2:N2,3) = RGBin(2:2:N1,1:2:N2,3); %blue
figure, imshow(RGB_bayer);
%Create a zoomed in version
L = 8; %zoom in by a factor of L
OR = fix([0.42*N1 0.23*N2]);
subIMG = RGB_bayer(OR(1)+1:OR(1)+N1/L,OR(2)+1:OR(2)+N2/L,:);
figure, imshow(subIMG)
RGB_bayer_sub_int = imresize(subIMG,L,'nearest');
figure, imshow(RGB_bayer_sub_int);