![]() ![]() ![]() Otherwise, by process of elimination (in context of this example, of course), we can make the assumption that the shape we are examining is a circle ( Lines 35 and 36).įinally, we return the identified shape to the calling method. If a contour has five vertices, we can label it as a pentagon ( Line 31 and 32). If the aspect ratio is ~1.0, then we are examining a square (since all sides have approximately equal length). To determine which, we compute the aspect ratio of the shape, which is simply the width of the contour bounding box divided by the height ( Lines 23 and 24). If a contour has four vertices, then it must be either a square or a rectangle ( Line 20). We can check the number of entries in this list to determine the shape of an object.įor example, if the approximated contour has three vertices, then it must be a triangle ( Lines 15 and 16). ![]() It’s important to understand that a contour consists of a list of vertices. # otherwise, we assume the shape is a circle # if the shape is a pentagon, it will have 5 vertices Shape = "square" if ar >= 0.95 and ar <= 1.05 else "rectangle" # equal to one, otherwise, the shape is a rectangle # a square will have an aspect ratio that is approximately # bounding box to compute the aspect ratio # compute the bounding box of the contour and use the # if the shape has 4 vertices, it is either a square or Given our approximated contour, we can move on to performing shape detection: # if the shape is a triangle, it will have 3 vertices Note: Interested in a more in-depth look at contour approximation? Be sure to check out the PyImageSearch Gurus course where I discuss computer vision and image processing fundamentals such as contours and connected-component analysis in detail. In order to perform contour approximation, we first compute the perimeter of the contour ( Line 11), followed by constructing the actual contour approximation ( Line 12).Ĭommon values for the second parameter to cv2.approxPolyDP are normally in the range of 1-5% of the original contour perimeter. This leads to a resulting approximated curve that consists of a subset of points that were defined by the original curve.Ĭontour approximation is actually already implemented in OpenCV via the cv2.approxPolyDP method. This algorithm is commonly known as the Ramer-Douglas-Peucker algorithm, or simply the split-and-merge algorithm.Ĭontour approximation is predicated on the assumption that a curve can be approximated by a series of short line segments. In order to perform shape detection, we’ll be using contour approximation.Īs the name suggests, contour approximation is an algorithm for reducing the number of points in a curve with a reduced set of points - thus the term approximation. We then have our detect method on Line 8 which requires only a single argument, c, the contour (i.e., outline) of the shape we are trying to identify. We’ll skip the _init_ constructor here since nothing needs to be initialized. Line 4 starts the definition of our ShapeDetector class. # initialize the shape name and approximate the contourĪpprox = cv2.approxPolyDP(c, 0.04 * peri, True) Open up the shapedetector.py file and insert the following code: # import the necessary packages Let’s go ahead and define our ShapeDetector. The first step in building our shape detector is to write some code to encapsulate the shape identification logic. Inside this module we have shapedetector.py which will store our implementation of the ShapeDetector class.įinally, we have the detect_shapes.py driver script that we’ll use to load an image from disk, analyze it for shapes, and then perform shape detection and identification via the ShapeDetector class.īefore we get started, make sure you have the imutils package installed on your system, a series of OpenCV convenience functions that we’ll be using later in this tutorial: $ pip install imutils Looking for the source code to this post? Jump Right To The Downloads Section OpenCV shape detectionīefore we get started with this tutorial, let’s quickly review our project structure: |- pyimagesearchĪs you can see, we have defined a pyimagesearch module. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |