Implemented local optimization of the grid, to cope with non-linear distortion across the image

Change-Id: I5502dcfc64ca80a5de703163d51ee6f2e247f9ac