Perspective transformation

that might be tricky, yes. I don’t think OpenCV has anything ready-made for your situation.

you might have to come up with an algorithm to fit a parallelogram around your contour.

I could think of something fairly ad-hoc but viable…

  • take the convex hull

then either:

  • come up with an algorithm that splits contours into (mostly) straight runs and curved runs (OpenCV needs that still, halcon has it already)
  • see if the hull contour has four straight runs that pair up to be somewhat parallel

or:

  • turn it dense and equally spaced
  • try to fit a pair of lines onto the contour for various angles (hypotheses), allowing for outliers (those sides of the parallelogram that aren’t currently considered, and see how many points are inliers. find the best hypothesis lines.

for the line fitting algorithm, that could be done by projecting the points onto an axis (for a given angle) and seeing where the points form clusters.

for the rectification of scans, also document layout analysis, something like that is a common step to rotate the picture to be axis-aligned.