8 – Hough Line Detection

In this notebook, I’ve read in and made a copy of an image of a hand holding a mobile phone. Now, let’s say we want to isolate this screen area. By using the Hough transform, we should be able to detect the lines that form the screen boundary. To perform Hough line detection, I’ll first have to detect the edges in the image. That means that I’ll convert this image to grayscale, then I’ll define thresholds and apply a Canny edge detector. And it’ll display the output. As you can see, we’ve detected a lot of smaller sort of discontinuous edges. And I’ll use a Hough transform to piece these together and form continuous lines. To find lines using a Hough transform, I’ll use the OpenCV function Hough lines P. This first takes in our binary image and then many other variables, and I’ll define all those parameters above the function. Rho and theta are our Hough variables, which define the resolution of the detection. I’ll set this as one pixel and one degree. Then we have a minimum threshold to detect a line. It’s the minimum number of Hough space intersections it takes to find a line. Then we have variables for the minimum line length and the gap between discontinuous line segments. And you’ll get a chance to play around with these values soon too. Then I pass these values in order to our Hough lines P function. It also takes in this empty array, which you can learn more about in-text. Now this function returns all the detected Hough lines. Each line is actually an array of four points, x1 y1 and x2 y2. These are just the two endpoints of each line. And our next step will be to make a copy of our image, and then iterate over our lines and plot each one on our image. Here I’m passing in the points that make a line, the color and the size. So let’s run this and plot our image. Here you can see it creates a number of lines around the screen and on the hand. And they actually might want to filter out some of these shorter lines. So I’ll go back up and increase our minimum line length to 100. And I’ll run this again. These lines better identify the phone screen exclusively. Finding continuous lines like this can be useful in helping form boundaries and in noticing details about object structure. In many cases, you’ll have to decide between using image contours or Hough space to find object boundaries. Next you’ll get some more practice with Hough space and learn about other segmentation techniques.

%d 블로거가 이것을 좋아합니다: