Algorithm for repairing the damaged images of grain structures obtained from the cellular automata and measurement of grain size