clear all;
close all;
cnn_setenv;

%template library
mCNN.TemGroup='global_temlib';
%input image
InputImg=lbmp2cnn('piclib/avergra2.bmp');

max(max(InputImg))
%displays the input
subplot(1,3,1)
cnnshow(InputImg);

for it=1:10
	%check pixels in for directions, can be extended to eight neigbhours
	for d=1:4
		%difference in first direction
		mCNN.INPUT1 = 1 .* InputImg;
		mCNN.STATE = 1 .* InputImg; 
		mCNN.Boundary = 2;
		mCNN.TimeStep = 1;
		mCNN.IterNum = 10;
		loadtem(strcat('DIFF',num2str(d)));
		runtem;
		DiffUp=1 .* mCNN.OUTPUT;
		
		
		%shift up the difference
		mCNN.INPUT1 = 1 .* DiffUp;
		mCNN.STATE = 1 .* zeros(size(DiffUp)); 
		mCNN.Boundary = 2;
		mCNN.TimeStep = 1;
		mCNN.IterNum = 10;
		loadtem('INC');
		runtem;
		Increased=1 .* mCNN.OUTPUT;
		subplot(1,3,2)
		cnnshow(Increased);

		%multiply the difference
		mCNN.INPUT1 = 1 .* Increased;
		mCNN.STATE = 1 .* zeros(size(DiffUp));  
		mCNN.Boundary = 2;
		mCNN.TimeStep = 1;
		mCNN.IterNum = 10;
		loadtem('MULT');
		runtem;
		OK=1 .* mCNN.OUTPUT;
		
		%add the difference to the original image
		Output=OK+InputImg;
		Output(Output>1)=1; 
		subplot(1,3,3)
		cnnshow(Output);
		
		InputImg=1*Output;
	end
end
