I need to find the coordinates of the corners of a rectangle plate in a picture, using Python and OpenCv.
This is what the original picture looks like:
Now I use the following script for edge detection:
import cv2
import numpy as np
import matplotlib.pyplot as plt
image_original = cv2.imread('c:/python_test/camera_pics/Basler_acA640-300gm__22354308__20210211_135725420_36.tiff', cv2.IMREAD_COLOR)
image_gray = cv2.cvtColor(image_original, cv2.COLOR_BGR2GRAY)
filtered_image = cv2.Canny(image_gray, threshold1=20, threshold2=200)
cv2.imwrite('c:/python_test/test.bmp',filtered_image)
And then it looks like this:
Now I am trying to find the corners of the plate, but I have no idea how I can do this. These corners can either be the corners of the inner rectangle or the outer rectangle. I edited the picture below in Paint to show which corners I mean.
Can you help me in writing a script to find the coordinates of the corners of this plate?
This is an uneasy task, especially for the two lower corners of the upper face. Contrast is low (even inexistent at places) and the image is cluttered.
The convex hull will indeed give you the two top corners (provided the screws are always detectable). You can also find the bottom corners of the lower face by finding significant angles in the convex hull.
You can somewhat approximate the locations of the corners of the upper face knowing the thickness of the plate and its projected height compared to true height.
As the long edges are not so badly detected, you may consider the long, continuous line segments (in the 8-connectedness sense), and try to match them to the convex hull and to the estimated corners.