%
% PerfectReflectorPlot.m
%
% Use the output of 100 image series to plot mean [R G B] value for PerfectReflector Algorithm
%
% Hui Li
% 3/7/2000
%

backgroundCol = ['430' '445' '460' '475' '490' '505' '520' '535' '550' '565' '580' '595' '610' '625' '640' '655' '670' '685' '700' '715' '730'];
primeCol = ['435' '460' '485' '510' '535' '560' '585' '610' '635' '660' '685' '710'];

bg = 430:15:730;
pc = 435:25:710;

avgPRR = zeros(12,21);
avgPRG = zeros(12,21);
avgPRB = zeros(12,21);

for j = 1:12
	for i = 1:21
		bmin = (i-1)*3+1;
		bmax = i*3;
		pmin = (j-1)*3+1;
		pmax = j*3;
		PRoutputfn = strcat('./PRoutput/PRoutput',backgroundCol(bmin:bmax),'-',primeCol(pmin:pmax),'.bmp');

		if backgroundCol(bmin:bmax) == primeCol(pmin:pmax)
			im = imread(PRoutputfn,'bmp');
			im = double(im)./255;
			r = im(:,:,1);
			g = im(:,:,2);
			b = im(:,:,3);
		else
			[X,map] = imread(PRoutputfn,'bmp');
			
			% Read into RGB and compute mean
			Y = double(X)+1;
			[r,g,b] = ind2rgb(Y,map);
		end

		avgPRR(j,i) = mean(mean(r));
		avgPRG(j,i) = mean(mean(g));
		avgPRB(j,i) = mean(mean(b));
		
	end
end

avgPR = (avgPRR + avgPRG + avgPRB)./3;
save('avg252PR.mat','avgPRR','avgPRG','avgPRB','avgPR');

avgOutPRR = abs(avgPRR - avgInR);
avgOutPRG = abs(avgPRG - avgInG);
avgOutPRB = abs(avgPRB - avgInB);
avgOutPR = (avgOutPRR + avgOutPRG + avgOutPRB)./3;

% Plot results

load avg252PR.mat;

figure
mesh(bg,pc,avgOutPRR);
title('Mean Red Value Difference of input image and PerfectReflector Output');

figure
mesh(bg,pc,avgOutPRG);
title('Mean Green Value Difference of input image and PerfectReflector Output');

figure
mesh(bg,pc,avgOutPRB);
title('Mean Blue Value Difference of input image and PerfectReflector Output');

figure
mesh(bg,pc,avgOutPR);
title('Mean Overall RGB Value Difference of input image and PerfectReflector Output');

figure
mesh(bg,pc,avgPRR);
title('Mean Red Value of PerfectReflector Output');
xlabel('Background Wavelength');
ylabel('Target Wavelength');
zlabel('R Values');

figure
mesh(bg,pc,avgPRG);
title('Mean Green Value of PerfectReflector Output');
xlabel('Background Wavelength');
ylabel('Target Wavelength');
zlabel('G Values');

figure
mesh(bg,pc,avgPRB);
title('Mean Blue Value of PerfectReflector Output');
xlabel('Background Wavelength');
ylabel('Target Wavelength');
zlabel('B Values');

figure
mesh(bg,pc,avgPR);
title('Mean Gray Value of PerfectReflector Output');
xlabel('Background Wavelength');
ylabel('Target Wavelength');
zlabel('Gray Values');


%figure
%hold on
%plot(bg, avgR(1,:), 'ro-');
%plot(bg, avgR(2,:), 'yo-');
%plot(bg, avgR(3,:), 'go-');
%plot(bg, avgR(4,:), 'bo-');
%plot(bg, avgR(5,:), 'mo-');
%xlabel('Background Wavelength (nm)'), ylabel('Mean R Value Difference of Input-PR');
%title('Mean Red Value Difference of input image and PerfectReflector Output');
%legend('680', '630', '580', '530', '480', 5);
%hold off;

%figure
%hold on
%plot(bg, avgG(1,:), 'ro-');
%plot(bg, avgG(2,:), 'yo-');
%plot(bg, avgG(3,:), 'go-');
%plot(bg, avgG(4,:), 'bo-');
%plot(bg, avgG(5,:), 'mo-');
%xlabel('Background Wavelength (nm)'), ylabel('Mean G Value Difference of Input-PR');
%title('Mean Green Value Difference of input image and PerfectReflector Output');
%legend('680', '630', '580', '530', '480', 5);
%hold off;

%figure
%hold on
%plot(bg, avgB(1,:), 'ro-');
%plot(bg, avgB(2,:), 'yo-');
%plot(bg, avgB(3,:), 'go-');
%plot(bg, avgB(4,:), 'bo-');
%plot(bg, avgB(5,:), 'mo-');
%xlabel('Background Wavelength (nm)'), ylabel('Mean B Value Difference of Input-PR');
%title('Mean Green Value Difference of input image and PerfectReflector Output');
%legend('680', '630', '580', '530', '480', 5);
%hold off;
