Line detection PR #2 follow-up.

This PR includes #2 rebased on top of current HEAD + a commit that cleans up the code and adds an improved adaptive thresholding routine.
The similar functionality is being develped in the pending text direction PR. I think, when that's done, it can be reused for just lines as well.