How to draw contour in OpenCV

share link

by l.rohitharohitha2001@gmail.com dot icon Updated: Oct 4, 2023

technology logo
technology logo

Solution Kit Solution Kit  

OpenCV stands for "Open-Source Computer Vision Library." This computer vision and image processing library is open-source.


It provides tools for tasks related to computer vision and image analysis. Intel created OpenCV in 1999. It is now a widely used library in computer vision. Contours are lines that connect points of the same color or brightness. Tasks such as object detection and image segmentation rely heavily on computer vision.  

Types of Contours  

  • Circular Contours  
  • Rectangular Contours  
  • Triangular Contours  
  • Polygonal Contours  
  • Elliptical Contours  
  • Curved Contours  
  • Arbitrary Contours  
  • Hierarchical Contours.  

Key points of the essay

  1. OpenCV for Contour Extraction: OpenCV is a computer vision library. It is open-source and used for image processing tasks. The Contours() function in OpenCV is a powerful tool. It can find and extract contours from images.  
  2. Image Preprocessing: Before finding contours, we need to preprocess the image. To convert the image, first make it grayscale. Then, smooth it and set a threshold.  
  3. Threshold: You can find the value in three ways: manual, automatic, or adaptive.  
  4. Contour retrieval & approximation: Estimating how accurate shapes affect accuracy and details.  
  5. Contour Filtering: After you get the shapes, keep the ones that are the right size and length. This helps to keep only the relevant contours.  
  6. Benefits of Contour Extraction: Extracting contours is very important in computer vision. It helps find objects and divide images. It helps analyze shapes.  
  7. Visual Feedback and Experimentation: To see the shapes, look at the steps in between. Try different ways and settings.  


You must find contours to work with OpenCV for computer vision and image processing. Contour detection has many benefits and is important in different applications.  

Fig: Preview of the output that you will get on running this code from your IDE.

Code

In this solution we are using OpenCV Python library of Python.

Instructions


Follow the steps carefully to get the output easily.


  1. Download and Install the Jupyter Notebook on your computer.
  2. Open the terminal and install the required libraries with the following commands.
  3. Create a new Python file on your Notebook.
  4. Copy the snippet using the 'copy' button and paste it into your Python.
  5. Run the current file to generate the output.


I hope you found this useful.


I found this code snippet by searching for 'Contour in Python' in Kandi. You can try any such use case!

Environment Tested


I tested this solution in the following versions. Be mindful of changes when working with other versions.

  1. Jupyter Notebook (anaconda 3) 6.0.1 Version
  2. The solution is created in Python 3.8 Version
  3. OpenCV Python.


Using this solution, we can able to draw contour in openCV with simple steps. This process also facilities an easy way to use, hassle-free method to create a hands-on working version of code which would help us to draw contour in openCV.

Dependent Library


Python doticonstar image 3081 doticonVersion:Currentdoticon
License: Permissive (MIT)

The OpenCV Q&A group is no longer maintained. If you have a problem, go search for it yourself. Google can find most answers.

Support
    Quality
      Security
        License
          Reuse

            OpenCV-Python-Tutorialby makelove

            Python doticon star image 3081 doticonVersion:Currentdoticon License: Permissive (MIT)

            The OpenCV Q&A group is no longer maintained. If you have a problem, go search for it yourself. Google can find most answers.
            Support
              Quality
                Security
                  License
                    Reuse

                      You can search for any dependent library on kandi like 'OpenCV-Python-Tutorial'.

                      FAQ:  

                      1. Which approximation method does OpenCV use to find contour()?  

                      The method parameter in OpenCV's findContours() function determines the contour approximation method. Based on the method parameter, we approximate and store the contour points. The commonly used approximation method is cv2.CHAIN_APPROX_SIMPLE.  

                        

                      2. How does threshold or canny edge detection work with OpenCV to find contour ()?   

                      We can find shapes and objects in pictures using OpenCV. We use thresholding and Canny edge detection. These techniques work well with the fine contours function.  

                        

                      3. What are the different contour retrieval modes available in OpenCV find contour ()?   

                      The mode parameter, contour retrieval mode, determines how to retrieve and organize contours. Depending on your needs, different ways exist to find and study contours.  

                      How you study and use the shapes affects which retrieval mode you choose. There are different modes to choose from, each with its level of detail and organization. You can select the one that fits your application's needs.  

                        

                      4. What is a child contour, and how does it relate to OpenCV find contour ()?   

                      The findContours() function detects and organizes contours hierarchically using the cv2.RETR_TREE mode.  

                      1. External Contours (Parent Contours): External contours are the main outlines in the picture. They show the edges of things. No other contour contains them, and people consider them parent contours.  
                      2. Internal Contours (Child Contours): Parent contours enclose internal child contours. They represent holes or internal boundaries within the parent objects.  

                        

                      5. How does the library of functions in OpenCV make using find contour () easier?   

                      OpenCV has a library of functions for finding contours that are powerful and easy to use. These functions are great for detecting contours and analyzing images.  

                      • Image Preprocessing Functions  
                      • Thresholding Techniques  
                      • Edge Detection Functions  
                      • Region of Interest (ROI) Functions  
                      • Drawing and Visualization Functions  
                      • Mathematical and Statistical Functions  
                      • Hierarchy Functions  
                      • Masking and Image Manipulation  

                      Support


                      1. For any support on kandi solution kits, please use the chat
                      2. For further learning resources, visit the Open Weaver Community learning page


                      See similar Kits and Libraries