function y=reindexImage(I)

% Converts rgb image back into surface colors in indexed format.

R=I(:,:,1);
G=I(:,:,2);
B=I(:,:,3);
rgb = zeros(4,6,3);
for i=1:4
  Ystart = 50 + 225*(i-1);
  Ystop = Ystart + 50;
  for j=1:6
    Xstart = 50 + 200*(j-1);
    Xstop = Xstart + 50;
%    Image(:,:,1) = R(Ystart:Ystop,Xstart:Xstop);
%    Image(:,:,2) = G(Ystart:Ystop,Xstart:Xstop);
%    Image(:,:,3) = B(Ystart:Ystop,Xstart:Xstop);
%    figure; image(Image);
    avgR = mean(mean(R(Ystart:Ystop,Xstart:Xstop)));
    avgG = mean(mean(G(Ystart:Ystop,Xstart:Xstop)));
    avgB = mean(mean(B(Ystart:Ystop,Xstart:Xstop)));
    rgb(i,j,1) = avgR;
    rgb(i,j,2) = avgG;
    rgb(i,j,3) = avgB;
  end
end

L = size(rgb,1)*size(rgb,2);
newRGB(:,1) = reshape(flipud(rot90(rgb(:,:,1))),L,1);
newRGB(:,2) = reshape(flipud(rot90(rgb(:,:,2))),L,1);
newRGB(:,3) = reshape(flipud(rot90(rgb(:,:,3))),L,1);

y=newRGB;

