This question is related to this question: How to remove convexity defects in sudoku square
I was trying to implement nikie\'s answer in Mathematica to OpenCV
Mathematica to OpenCV
if you have source points and end points (you only need 4), you can plug them into cv2.getPerspectiveTransform, and use that result in cv2.warpPerspective. Gives you a nice flat result.