Welcome to my week 3 blog post! This week, I focused on finding ways to get the corners found in the images by tweaking the parameters for Hough Line Transform, as well as trying different methods.
Just as last week, I deployed the program I wrote as a web app. You may find it here: https://senior-project.jessexu.me/?file_selection=Week+3
You can now select a different sample image to experiment with in the variable control panel (but I will use the default one throughout this blog post). You can also tweak the parameters for Hough Line Transform there.
Last week, I had a first glance at Hough Line Transform. However, the results I got from it were not impressive. The detected lines were short and piled up, which are no what we wanted. We want to get long and clean lines from the contours, so that we can calculate the corners of the shapes by taking the intersection of the lines.
I soon found the issue in my code that caused a line to be detected multiple times. I was using
cv2.drawContours() to find and draw the contours. However, I drew the contours with a line thickness of 3 to make it look more obvious, which makes Hough Line Transform think that there are multiple short lines there. After changing the line thickness to 1, many repeated lines were eliminated.
# cv2.drawContours() usage:
However, when I was trying to solve this problem, I stumbled upon an algorithm called Canny edge detection. It is conveniently available in OpenCV as the method
cv2.Canny(), it produces almost identical results with the combination of
cv2.drawContours() if not better. So, I decided to use Canny in the end.
After solving the problem in contour extraction, we got better results with Hough Line Transform. However, the results are still far from usable for corner detection. The following picture is about the best I got from tweaking the various parameters for the Hough Line Transform function.
The lines are not piled up anymore, but there are still many short lines that did not merge with the longer lines. It’s a tricky situation: if I turn up the minimum line length parameter, then some line segments don’t get detected and leaving it blank; if I turn up the longest gap parameter, the short gaps in between the shapes gets connected and recognized as a line.
I’ve also looked up the meaning of the parameters and the mechanism behind Hough Line Transform, but I still cannot find the best parameters that could give satisfying results. I believe it’s due to the fact that the lines are not exactly straight and that they have rounded corners.
# My understanding of the parameters
Since I can’t get satisfying results from tweaking Hough Line Transform, I turn to find other corner detection algorithms. I found two that are commonly used: Harris corner detection and Shi-Tomasi corner detection.
The code is quiet simple, the algorithm is also implemented as a method in OpenCV. See code below.
corners = cv2.cornerHarris(img, blockSize=5, ksize=5, k=0.04) # Find the corners with Harris
Sadly, it does not produce satisfying results either. Some corners have multiple points marked as corner, and some corners have no points marked.
Although there are parameters for the
cv2.cornerHarris() that I can tweak, the underlying mechanism of the algorithm is too hard for me to understand since it requires some knowledge in matrix.
So, I decide to try another corner detection algorithm, and I found Shi-Tomasi corner detection. The code is also quiet simple:
corners_s = cv2.goodFeaturesToTrack(img, maxCorners=100, qualityLevel=0.01, minDistance=0) # Find corners
Unfortunately, the results aren’t good either. The mechanism behind it is also quiet complicated that I couldn’t understand.
This week, I tried multiple ways to detect corners of the shapes, but with little success. Next week, I will continue working on finding better solutions to this topic.