How can the location of the paper rectangle be detected as a rotated in these images using opencv? The big problem is the background which makes it hard to extract only the paper.
I manage to get parts of the paper edges with hough lines but there are also some false lines for example.
Here are the images where I need the paper to be detected. As you can see, the camera position stays the same.
- Example image 1
- Example image 2
- Example image 3
- Example image 4
- Example image 5
I’ve tried these things:
- Simple Threshholding -> Result: Since parts of the background have the same color as the paper, i cant get the outline of the paper
- Hough Lines -> Result: Depending on how you change the parameters, too many false lines are detected or too view lines of the actual paper edges
- Contour detection and Shape Approximation -> Result: A proper threshholding would be needed, to work with contours
- Feature Matching -> Result: Simply not enough good features to match
- Shifting a rect over the images -> Takes way to long and is not precise, since the rect is a little distorted, and not perfectly square
The best results I got was a thresholding with otsu, then an edge detection with canny, extracting contours and then houghLinesP:
import cv2
import numpy as np
img = cv2.imread("path/to/image", cv2.IMREAD_GRAYSCALE)
_, binary = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
edges = cv2.Canny(contours, 50, 150)
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 100, minLineLength=45, maxLineGap=8)