3 – Image Contours

Edge detection algorithms are often used to detect the boundaries of objects. But, after performing edge detection you’ll often be left with sets of edges that highlight not only object boundaries but also interesting features and lines. And to do image segmentation, you’ll want only complete closed boundaries that marked distinct areas and objects in an image. One technique that’s useful for this is called, Image Contouring. Image contours, are continuous curves that follow the edges along a perceived boundary. So contours can be used for image segmentation and they can also provide a lot of information about the shape of an object boundary. In OpenCV contours are best detected when there’s a white object against a black background. So before we can identify contours in an image, we’ll first need to create a binary threshold that image, that has black and white pixels that distinguish different objects in an image. We’ll then use the edges of these objects to form contours. This binary image is often produced by a simple threshold as shown here, or by a Canny edge detector. Let’s go through this simple image contour an example. Here, I’ve read in the image of a hand that you’ve already seen. Hand recognition is actually an ongoing computer vision challenge where it’s useful for example, to recognize and interpret sign language and in recognizing a variety of other gestures. So let’s perform image contouring on this hand. First, I’ll convert this image to grayscale. Then, I’ll use a binary threshold inverted to show the hand as way instead of the background to produce a binary image. This threshold takes in our grayscale image and isolates the white pixel values and it turns them black with an inverse threshold. Then I’ll display this binary image. Our next step will be to find and draw the contours. To find the contours, I’ll be using the CV function, findContours. This takes in our binary image, then a contour retrieval mode which I’ll have as a tree, and third, is our contour approximation method which I’ll put as a simple chain and the outputs are list of contours in the hierarchy. The hierarchy is useful if you have many contours nested within one another. The hierarchy defines their relationship to one another and you can learn more about this in text. Then once we have a list of detected contours I’ll display them on a copy of our image. To draw the contours, I’ll use an OpenCV function called, drawContours. This takes in the copy over image followed by our list of contours then which contours to display. Negative one means all of the contours. And finally the color and size I want the contours to be or have them be a thin green line and I’ll display the output. You can see there’s one contour that nicely defines the boundaries an outline of the hand. You can also see that it creates a complete closed boundary. And in this case it also separates the image into this foreground object and a background. And from this contour, I can extract lots of information about the shape of the hand including its area, the center of the shape, its perimeter, and its bounding rectangle. All of these shape traits are called, Contour Features. Next, you’ll get a chance to practice coding and learn a little more about contour features and why they are useful.

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