Improve the grid detection algorithm

After get the max bin id of the grid detection, by using edges in this
bin, compute more precise grid direction, and find all the edges that
follow the grid direction.