r = pnmread('red.pgm');
g = pnmread('green.pgm');
b = pnmread('blue.pgm');

% construct the color matrix

a(:,:,1) = b;
a(:,:,2) = g;
a(:,:,3) = r;

% find a uniform 8x8 window
win = ones(8);

% convolve with the color matrix to do the average
cg = conv2(g,win);
cr = conv2(r,win);
cb = conv2(b,win);

% search  the maximum average for green
[val, tmp]=max(cg);
[mg, indJg]=max(val);
[mg, indIg]=max(cg(:,indJg));

% search  the maximum average for red
[val, tmp]=max(cr);
[mr, indJr]=max(val);
[mr, indIr]=max(cr(:,indJr));

% search  the maximum average for blue
[val, tmp]=max(cb);
[mb, indJb]=max(val);
[mb, indIb]=max(cb(:,indJb));

% search the maximum average for red       
tr = r(indIg:indIg+7,indJg:indJg+7);
[tval, tmpr]=max(tr);
[tmr, tindJr]=max(tval);
[tmr, tindIr]=max(tr(:,tindJr));
trmean = mean(mean(tr));
if (tmr-trmean) > 40
   tmr = trmean;
end

% search the max blue in green peak window
tb = b(indIg:indIg+7,indJg:indJg+7);
[tval, tmpb]=max(tb);
[tmb, tindJb]=max(tval);
[tmb, tindIb]=max(tb(:,tindJb));
tbmean = mean(mean(tb));
if (tmb -tbmean) > 40
   tmb = tbmean;
end

% average of 64 pixels
mr = mr/64;
mb = mb/64;
mg = mg/64;
tmr = tmr/64;
tmb = tmb/64;


