%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 

%% input parameters
src_img			= 'L:\array\tcf\30C_E\z001_t001.cam3.tif';
cal_file		= 'L:\array\tcf\calib\linear3_f4.mat';
threshold		= [50 50 40];

%% load image
img				= imread(src_img);
img_demosaic	= demosaic(uint8(img), 'bggr');

%% histogram of brightness
c_edges				= 0 : 256;
img_hist			= zeros(257, 3);
for rgb = 1 : 3
	channel			= img_demosaic(:,:,rgb);
	img_hist(:,rgb) = histc(channel(:), c_edges);
end

%% load calibration
setup			= load(cal_file);
camera			= setup.camera(1);

%% find particles on RGB image
[im_part,im_bright] = rgb_find_particles(camera, img, threshold);
n_part			= cellfun(@(x)size(x,2), im_part);
fprintf('found\n');
fprintf('   %d red\n', n_part(1));
fprintf('   %d green\n', n_part(2));
fprintf('   %d blue\n', n_part(3));

%% histogram
figure(3);
clf;
cmap		= {'r','g','b'};
for rgb = 1 : 3
	plot(c_edges, img_hist(:,rgb), '.-', 'color', cmap{rgb});
	hold on;
end
pbaspect([4/3 1 1]);
xlim([min(threshold), 256]);
xlabel('Brightness');
ylabel('Counts');

%% show demosaic images
figure(1);
clf;
% draw particles with identification
image(camera.i_axis, camera.j_axis, permute(img_demosaic, [2 1 3]));

hold on;
color			= {'r','g','b'};
for rgb = 1 : 3
	%plot(im_part{rgb}(1,:), im_part{rgb}(2,:), 'o', 'color', color{rgb});
end
axis equal tight;
title('demosaiced');
