![]() # The area of contours print("1st Contour Area : ", cv2.contourArea(contours)) # 37544.5 print("2nd Contour Area : ", cv2.contourArea(contours)) # 75.0 print("3rd Contour Area : ", cv2.contourArea(contours)) # 54.0 This shows that the contour detection algorithm forms the hierarchy of the detected boundaries. Why don’t we try several contours here? If you input the first, second and third contours, you’ll get the decreasing values as shown below. To get the area of the contours, we can implement the function cv2.contourArea(). Additional reading resources can be found at the end of this article if you may have an interest. I’d like to focus on the implementation of the image moments here. # The first order of the contours c_0 = contours # image moment M = cv2.moments(c_0) print(M.keys()) If you print the output M as shown below, it’ll return the 24 moments in a dictionary format. We can get the image moment with the function cv2.moments() in OpenCV and it returns 24 different moments. There are three types of moments- spatial moments, central moments, and central normalized moments. # Sort the contours contours = sorted(contours, key = cv2.contourArea, reverse = True) # Draw the contour img_copy = img.copy() final = cv2.drawContours(img_copy, contours, contourIdx = -1, color = (255, 0, 0), thickness = 2 ) plt.imshow(img_copy) With the selected contour line, cv2.drawContours() will depict the bounding line along with the points as shown below. To draw the outer line of the figure, we’ll sort the contours by their area. The returned contour is a list of points consisting of the contour lines. And as you can see above, this function gives the image, the detected contours and their hierarchy as its output. The method cv2.CHAIN_APPROX_SIMPLE returns only the endpoints that are necessary for drawing the contour line. The mode cv2.RETR_TREE finds all the promising contour lines and reconstructs a full hierarchy of nested contours. # Find the contour of the figure image, contours, hierarchy = cv2.findContours( image = thresh, mode = cv2.RETR_TREE, method = cv2.CHAIN_APPROX_SIMPLE) I ask you to find other information from the documentation. mode is the way of finding contours, and method is the approximation method for the detection. # Load the image img = cv2.imread('images/pine_apple.jpg') img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) plt.imshow(img)Ĭontour detection can be implemented by the function cv2.findContours() in OpenCV and there are two important parameters here. Contours, on the other hand, are closed curves which are obtained from edges and depicting a boundary of figures. Edges are points whose values change significantly compared to their neighboring points. ![]() To put it simply, the concept of edges lies in a local range while the concept of contours is at the overall boundary of a figure. ![]() What’s the difference between edges and contours? The two terms are often used interchangeably so it could be a bit confusing. A contour map is the most straightforward example we can think of.īut then you may ask this. A contour line indicates a curved line representing the boundary of the same values or the same intensities. You may be already familiar with the word ‘contour.’ I’ve used this term several times in previous posts. The complete code for this tutorial is available on Github as always. But if it’s not the case, please check the previous parts of this series. This article assumes you’ve been following the previous steps or you already know those concepts. ![]() And after that, we’re going to round up this series with a little bit of fun playing with masking. There is another detection technique called contour detection which is essential in object detection. And in part 3, we discussed several detection algorithms including edge detection. We also talked about how to apply blurring and thresholding with various options. Contour detection and having a little bit of funįrom the first series, we saw how to draw a figure on an image.From feature detection to face detection.The basics of image processing with filtering and gradients.Understanding color models and drawing figures on images.This is the last part of OpenCV tutorial for beginners and the complete set of the series is like follows: And I recommend you also merge fun with studying. It might interest you to note that people enjoyed the parts of the series where I applied the concept to the images in a funny way. In this series of tutorials, many images have been used to demonstrate image processing concepts. As for me, combining studies with a little bit of fun is the best strategy. There are lots of ways out there one can adapt to make learning progress efficiently. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |