I want in the x-ray image below to (by using Python):
- identify the rotation of the (imperfect) rectangle block
- rotate the image so that it is in vertical (portrait form)
- remove by cropping the remaining white space
I guess this partly the reverse of this question where the tools are most likely identical with the addition of a corner detector. I'm not entirely sure of how to best approach this and it seems like a problem that someone has solved.
This can be done using the Python bindings to the
OpenCV
library. The following code has been adapted from something I had already, so it can probably be further optimised and improved.The image you have given is not only rotated, but it is also not rectangular, as such the script works in two main stages. Firstly it determines the rotation on the image and rotates and crops it around a minimum rectangle. It then stretches the resulting image to fit the resulting rectangle.
Initial threshold image
Initial bounding rectangle
Rotated and cropped image
Polygon to stretch from
Final cropped image
Thanks go to this StackOverflow answer for the
subimage
function.Tested on Python 2.7 and OpenCV 3.0