Find Contour

void findContours(InputOutputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method, Point offset=Point())

Parameters:
  • image – Source, an 8-bit single-channel image. Non-zero pixels are treated as 1’s. Zero pixels remain 0’s, so the image is treated as binary . You can use compare() , inRange() , threshold() , adaptiveThreshold() , Canny() , and others to create a binary image out of a grayscale or color one. The function modifies the image while extracting the contours.
  • contours – Detected contours. Each contour is stored as a vector of points.
  • hierarchy – Optional output vector, containing information about the image topology. It has as many elements as the number of contours. For each i-th contour contours[i] , the elements hierarchy[i][0] , hiearchy[i][1] , hiearchy[i][2] , and hiearchy[i][3] are set to 0-based indices in contours of the next and previous contours at the same hierarchical level, the first child contour and the parent contour, respectively. If for the contour i there are no next, previous, parent, or nested contours, the corresponding elements of hierarchy[i] will be negative.
  • mode – Contour retrieval mode (if you use Python see also a note below).
    • CV_RETR_EXTERNAL retrieves only the extreme outer contours. It sets hierarchy[i][2]=hierarchy[i][3]=-1 for all the contours.
    • CV_RETR_LIST retrieves all of the contours without establishing any hierarchical relationships.
    • CV_RETR_CCOMP retrieves all of the contours and organizes them into a two-level hierarchy. At the top level, there are external boundaries of the components. At the second level, there are boundaries of the holes. If there is another contour inside a hole of a connected component, it is still put at the top level.
    • CV_RETR_TREE retrieves all of the contours and reconstructs a full hierarchy of nested contours. This full hierarchy is built and shown in the OpenCV contours.c demo.
  • method – Contour approximation method (if you use Python see also a note below).
    • CV_CHAIN_APPROX_NONE stores absolutely all the contour points. That is, any 2 subsequent points (x1,y1) and (x2,y2) of the contour will be either horizontal, vertical or diagonal neighbors, that is, max(abs(x1-x2),abs(y2-y1))==1.
    • CV_CHAIN_APPROX_SIMPLE compresses horizontal, vertical, and diagonal segments and leaves only their end points. For example, an up-right rectangular contour is encoded with 4 points.
    • CV_CHAIN_APPROX_TC89_L1,CV_CHAIN_APPROX_TC89_KCOS applies one of the flavors of the Teh-Chin chain approximation algorithm. 
  • offset – Optional offset by which every contour point is shifted. This is useful if the contours are extracted from the image ROI and then they should be analyzed in the whole image context.

void drawContours(InputOutputArray image, InputArrayOfArrays contours, int contourIdx, const Scalar& color, int thickness=1, int lineType=8, InputArray hierarchy=noArray(), int maxLevel=INT_MAX, Point offset=Point() )

Parameters:
  • image – Destination image.
  • contours – All the input contours. Each contour is stored as a point vector.
  • contourIdx – Parameter indicating a contour to draw. If it is negative, all the contours are drawn.
  • color – Color of the contours.
  • thickness – Thickness of lines the contours are drawn with. If it is negative (for example, thickness=CV_FILLED ), the contour interiors are drawn.
  • lineType – Line connectivity. See line() for details.
  • hierarchy – Optional information about hierarchy. It is only needed if you want to draw only some of the contours (see maxLevel ).
  • maxLevel – Maximal level for drawn contours. If it is 0, only the specified contour is drawn. If it is 1, the function draws the contour(s) and all the nested contours. If it is 2, the function draws the contours, all the nested contours, all the nested-to-nested contours, and so on. This parameter is only taken into account when there is hierarchy available.
  • offset – Optional contour shift parameter. Shift all the drawn contours by the specified \texttt{offset}=(dx,dy) .
  • contour – Pointer to the first contour.
  • externalColor – Color of external contours.
  • holeColor – Color of internal contours (holes)

Steps:

  1. Load an image 
  2. Create a structuring element
  3. Apply erosion or dilation on the image
  4. Show result

Functions:

Example 1:Find Contour OpenCV documentation implementation

-------------
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "iostream"
using namespace cv;
using namespace std;
int main( )
{
    Mat image;
    image = imread("shape.jpg", 1);  
    namedWindow( "Display window", CV_WINDOW_AUTOSIZE );  
    imshow( "Display window", image );
    Mat gray;
    cvtColor(image, gray, CV_BGR2GRAY);
    Canny(gray, gray, 100, 200, 3);
    /// Find contours   
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    RNG rng(12345);
    findContours( gray, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
    /// Draw contours
    Mat drawing = Mat::zeros( gray.size(), CV_8UC3 );
    for( int i = 0; i< contours.size(); i++ )
    {
        Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
        drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, Point() );
    }     

    imshow( "Result window", drawing );
    waitKey(0);                                         
    return 0;
}
-------------

 Result:

 

 

Example 2: Find Largest contour

-------------
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp""
#include "opencv2/imgproc/imgproc.hpp"
#include "iostream"
using namespace cv;
using namespace std;
int main( )
{
    Mat src;
    src = imread("shape.jpg", CV_LOAD_IMAGE_COLOR);
    Mat gray;
    cvtColor(src, gray, CV_BGR2GRAY);
    threshold(gray, gray,200, 255,THRESH_BINARY_INV); //Threshold the gray
    imshow("gray",gray);int largest_area=0;
    int largest_contour_index=0;
    Rect bounding_rect;
    vector<vector<Point>> contours; // Vector for storing contour
    vector<Vec4i> hierarchy;
    findContours( gray, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
    // iterate through each contour.
    for( int i = 0; i< contours.size(); i++ )
    {
        //  Find the area of contour
        double a=contourArea( contours[i],false); 
        if(a>largest_area){
            largest_area=a;cout<<i<<" area  "<<a<<endl;
            // Store the index of largest contour
            largest_contour_index=i;               
            // Find the bounding rectangle for biggest contour
            bounding_rect=boundingRect(contours[i]);
        }
    }
    Scalar color( 255,255,255);  // color of the contour in the
    //Draw the contour and rectangle
    drawContours( src, contours,largest_contour_index, color, CV_FILLED,8,hierarchy);
    rectangle(src, bounding_rect,  Scalar(0,255,0),2, 8,0);
    namedWindow( "Display window", CV_WINDOW_AUTOSIZE );
    imshow( "Display window", src );    
    waitKey(0);                                         
    return 0;
} 

-------------

Result:




Sources:
http://docs.opencv.org/doc/tutorials/imgproc/shapedescriptors/find_contours/find_contours.html
http://harismoonamkunnu.blogspot.in/2013/06/opencv-find-biggest-contour-using-c.html


148 comments:

  1. Hi, i have threshold image. How can i draw bounding box for the threshold object.

    ReplyDelete
    Replies
    1. That means you are at line 13 of Example 2.

      Make sure that after threshold, the object of interest is white and background is black.

      Then you can apply "findContours" as in line 19.
      If you will have only one contour, then you can directly put the index value as "0" in line 30 to find the "bounding_rect".

      Then use line 36 to draw a bounding box around the object.

      Delete
  2. can you explain contour.size() ? and I want to know how many points in each contours,

    ReplyDelete
    Replies
    1. I want to use the contour.size() to find the number of vertices of the contours,have you found the solution ?

      Delete
    2. contours.size() provides the number of contours in the image. Here number of contours means number of segmented regions.

      Therefore, you can not use contours.size() to find the number of vertices.

      You may find a solution of this question by using convex-hull method.

      Delete
  3. And how I can draw bounding box if I use bgs MOG from opencv? Is it the same?

    ReplyDelete
  4. can i please get the same coed in python?

    ReplyDelete
  5. import numpy as np
    import cv2
    im = cv2.imread("shape.jpg")
    imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
    ret,thresh = cv2.threshold(imgray,127,255,0)
    contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

    cv2.drawContours(im,contours,-1,(0,255,0),2) #-1 fills it


    cv2.imshow("Contours",im)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    # Lijo Joseph

    ReplyDelete
    Replies
    1. but for 16 uint images??
      i've an image from a depth camera, i've binarized it with otsu method and now i want find and plot contour area
      thanks

      Delete
    2. I'm getting this error. Could you please help? Thanks

      Traceback (most recent call last):
      File "C:\Python27\contous.py", line 4, in
      imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
      error: ..\..\..\src\opencv\modules\imgproc\src\color.cpp:3414: error: (-215) scn == 3 || scn == 4

      Delete
    3. Get error:
      contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
      ValueError: too many values to unpack (expected 2)

      Delete
  6. Code is working fine. Thank you. I have another question. Is there any way to draw only the largest contour on new image?

    I can draw the largest contour, but new Mat size is in size of old one image.

    Mat mask_image( image.size(), CV_8U, Scalar(0));
    drawContours(mask_image, contours, largest_contour_index, Scalar(255), FILLED);
    plate.copyTo(original_image, mask_image);

    Is there any way to get biggest contour size, then make new Mat in size of this contour and draw an image? Without other area which is black?

    ReplyDelete
  7. Your problem statement is not clear, sorry...

    ReplyDelete
  8. Hi, I wish to draw the contours of certain clicked region only, For example, mouse click on the triangle, and only the contour of triangle is drawn. Is it possible? and How should i implement that?

    ReplyDelete
    Replies
    1. You can use mouse click to do so. Please refer to the following link.
      opencvexamples.blogspot.com/2014/01/select-region-on-mouse-click.html

      Once you get all contours, click on one image region and it will high light based on the mouse position.

      Delete
  9. Thank you very much for this very clear example. You saved my day.. :)

    ReplyDelete
  10. Hi,I want to display the chain codes of the contour in opencv with c++ language, but I can not find function in c++.
    can someone help me?
    Thanks.

    ReplyDelete
  11. Hi how can I extract the properties of a ( or some ) pixel(s) at the center of a contour? and if it is a camera frame and camera is moving how can I track each contour and extract the properties of a ( or some ) pixel(s) at the center of contour?

    ReplyDelete
  12. how can i draw contours only around blue colored objects? Also I need their centroids.

    ReplyDelete
  13. Very nice programs you had these hidden here.

    ReplyDelete
  14. really working program very nice

    ReplyDelete
  15. how can i find out number of child contours inside a parent contour?
    and is there any method wherein i can add box numbers to boxes of chessboard type image?

    ReplyDelete
  16. How come you use the zeros function? What is it's purpose in the first program? Is it to make the whole picture totally black by setting the pixel values in the array to 0? Then the drawing of contours would pop out more?

    ReplyDelete
  17. Are there any C# codes to do this? Specially EmguCv. Can someone help me?
    Thank you!

    ReplyDelete
  18. Hi

    How do i detect a face using contours ?Currently i have the edge image of the face but have no idea on the next step.

    ReplyDelete
  19. I have a image with has lot of different colors. Suppose I draw a polygon with a specific color which is not there in the original image, how do I draw a contour around it and fill it with a specific color?

    Preferably in python.

    ReplyDelete
  20. Hi do you have a detection text using tesseract with OpenCV Thanks in advanced ^_^

    ReplyDelete
  21. how to find the area of the contour with opencv python

    ReplyDelete
  22. Can an6one translate the codes ti android/java... Thanks

    ReplyDelete
    Replies
    1. cv2.contourArea() gives u the area of the contour

      Delete
  23. i am using findContours to detect characters in an image to convert it into text. But the contours are randomly detected. So the order of the charcaters is getting changed. can someone help me understand how are the contours getting detected?

    ReplyDelete
  24. Thanks a lot. Well how can we find the width and length of a contour? For example we have any contour of random shape and I have to find its width and height? Thanks

    ReplyDelete
    Replies
    1. sorry width and length of any random shaped contour...

      Delete
  25. how to compare two image shapes?

    ReplyDelete
    Replies
    1. please send the code for opencv 3.3.0 version

      Delete
  26. hey Every one, how can i do this in java with bufferedImage????????????

    ReplyDelete
  27. Code works fine - all contours will be found.
    I have the following problem with freeing memory:

    std::vector contours;

    cv::findContours (...)

    for (i = 0; i < contours.size(); i++)
    contours[i].clear();

    contours.clear() --> here it crashes with: "exception error at ... (ucrtbased.dll) "

    Do you have an idea? Thanks in advance. Falk.

    ReplyDelete
  28. How can i find the smallest contour??

    ReplyDelete
  29. suppose in an image we have 5 contours, then we will have centroids. how can we compare centroid values??

    ReplyDelete
  30. myTectra the Market Leader in Artificial intelligence training in Bangalore
    myTectra offers Artificial intelligence training in Bangalore using Class Room. myTectra offers Live Online Design Patterns Training Globally.Read More

    ReplyDelete
  31. Very interesting,good job and thanks for sharing such a good blog.your article is so convincing that I never stop myself to say something about it.You’re doing a great job.Keep it up.python programming Training, courses,Classes,Institute in Mumbai

    ReplyDelete
  32. Get Mutual Fund Investment Schemes by Mutual Fund Wala and know about the best investment platform for you, to get profit.
    Mutual Fund Agent

    ReplyDelete
  33. Awesome information, visit our page lifestyle magazine to get the best fashion and lifestyle magazines.
    Lifestyle Magazine India

    ReplyDelete
  34. if you had done love marraige and you are facing so much problems in your life so no need to worry i will tell you the best dua for husband and wife

    ReplyDelete
  35. if you had done love marraige and you are facing so much problems in your life so no need to worry i will tell you the best dua for husband and wife

    ReplyDelete
  36. Thanks for sharing such a great blog Keep posting..
    Python Training in Gurgaon
    Python Course in Gurgaon

    ReplyDelete
  37. Thanks for your efforts for this blog. Visit Kalakutir Pvt Ltd for Caution & Indication Signages and Warehouse Zebra Painting.
    Warehouse Zebra Painting

    ReplyDelete
  38. how do i find the smallest contours? ...give me a source code for c++

    ReplyDelete
  39. This is really fascinating, You are an excessively professional blogger.I have joined your feed and look ahead to in the hunt for extra of your excellent post.Also, I've shared your website in my social networks iTunes Gift Card

    ReplyDelete
  40. Keep it up for more updates for this related blogs, visit OGEN Infosystem for Best Website Designing Company and SEO Services in Delhi, India.
    Website Designing Company

    ReplyDelete
  41. Printing instrumentation Services
    Offset Printing Machine Repair Service offers a extremely trained employees of printing instrumentation technicians to assist drawback solve and fix with regards to any drawback that may arise along with your printing instrumentation. From folders, cutters to merely concerning any offset press that's within the print business.

    ReplyDelete
  42. For IOT Training in Bangalore Visit:IOT TRaining in Bangalore

    ReplyDelete
  43. Thank you for providing such an awesome article and it is very useful blog for others to read.
    Devops Training in Delhi
    Devops Course in Delhi

    ReplyDelete
  44. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful.SAP Training in Bangalore

    ReplyDelete
  45. Amazing article. Your blog helped me to improve myself in many ways thanks for sharing this kind of wonderful informative blogs in live. I have bookmarked more article from this website. If someone wants to know about Security Services Company this is the right place for you..

    Farnek.uk

    ReplyDelete
  46. Amazing article. Your blog helped me to improve myself in many ways thanks for sharing this kind of wonderful informative blogs in live. I have bookmarked more article from this website. If someone wants to know about Security Services Company this is the right place for you..

    Farnek.uk

    ReplyDelete
  47. This comment has been removed by the author.

    ReplyDelete
  48. Skip Hire Near Me offers low skip cost and cheap skip hire prices

    ReplyDelete
  49. The printing press allows us to share large amounts of information quickly and in huge numbers. In fact, the printing press is so significant that it has come to be known as one of the most important inventions of our time. It drastically changed the way society evolved.Best Printing Press Company in DelhiPrinting Press, Catalog Printing, Sticker Printing, Brochure Printing, Pamphlet Printing, Booklet Printing, Printing Services Digital Printing Services, Catalog Printing Services, Magazines Publishing, Books Printing, Web Offset Printing, Print Media, Typesetting Services, Color Printing, Books Binding, Abhi print printing Press has enjoyed the reputation of being one of the frontline names in the Indian printing industry. We are providing all types of printing services

    ReplyDelete
  50. I quite like reading through a post that can make people think. Also, thanks for allowing for me to comment! Providing Us With This Great Knowledge Check Mot History Online

    ReplyDelete
  51. The reason people do this is that they already know that it is difficult for them to be followed up by those whom they are providing information against.assignment expert

    ReplyDelete
  52. Printmaking can be divided into four basic categories

    relief, intaglio, planographic, and stencil.
    Relief printmaking is one of the simplest types of

    printmaking, in which material is carved or taken away from around the protruding design that is to be

    printed so that only the design appears.

    ReplyDelete
  53. Kim Ravida is a lifestyle and business coach who helps women in business take powerful money actions and make solid, productiveIamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder

    ReplyDelete
  54. Amazing Article ! I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
    AWS certification course in Chennai

    ReplyDelete
  55. Want to hire a plot or house please contact with us housing societies are mentioned below just click now;

    Capital Smart City
    Capital Smart City
    Nova City Islamabad

    ReplyDelete
  56. After you make the down payment, your specialist will provide you with a receipt. After a month or 45 days, you will receive the complete booking package from park view lahore payment plan developer.
    Rudn Enclave payment plan
    Blue world city Islamabad payment plan

    ReplyDelete
  57. This blog is very informative the stuff you provide I really enjoyed reading 야설

    ReplyDelete
  58. Your blog was incredible, I'm glad that i found this type of site, after so many hours of searching of a marvelous site like your 일본야동

    ReplyDelete
  59. Very great post. I just stumbled upon your blog and wanted to say that I have truly loved browsing your blog posts. In any case I will be subscribing on your feed and I hope you write again very soon! 야동

    ReplyDelete
  60. It's late finding this act. At least, it's a thing to be familiar with that there are such events exist. I agree with your Blog and I will be back to inspect it more in the future so please keep up your act 야동

    ReplyDelete
  61. You actually make it look so easy with your performance but I find this matter to be actually something which I think I would never comprehend. It seems too complicated and extremely broad for me. I'm looking forward for your next post, I’ll try to get the hang of it! 야설

    ReplyDelete
  62. The Masters Real Estate is one of the most enticing options on the planet of real estate. Our prestigious forum provides the potential solution of all your ambiguities regarding the purchasing of shops, lands, and houses. We have engaged our teams in various cities of Pakistan as well as the online world to make our services accessible. The provision of reasonable price real estate properties by our company is attracting the residents and the foreigners. We have intended to provide luxurious construction projects and houses at affordable prices to our nation.
    For More Visit: Lahore Smart City
    Click Here

    ReplyDelete
  63. This comment has been removed by the author.

    ReplyDelete
  64. Supradyn Tablet is use in very easy in people. this tablet is multivitamin

    ReplyDelete
  65. RUDN Enclave is one of the top and most demanding housing project in twin cities. Developers of the society promised to provide quality living to it's valuable customers. Some of the other most demanding projects are:
    Capital Smart City
    RUDN Enclave
    Nova City
    Lahore Smart City

    ReplyDelete
  66. nice article post.Really thank you! Really Great
    MPM Corner


    Jumma Mubarak

    ReplyDelete
  67. Sounds like the writer is way ahead of his time, he writes with maturity. Every content is precisely written.
    Custom Software Development

    ReplyDelete
  68. One necessities to recollect that Dilaudid is a drug containing Hydrocodone, which is a Schedule II Controlled Substance and has a genuine danger of fixation and misuse - subsequently making it crucial for know these safeguards before they purchase Dilaudid on the web and begin taking it.It is basic to tell your primary care physician about the items you use be it any non-medicine/doctor prescribed medication or any home grown item prior to going through an activity.The prescriptions containing Hydrocodone when joined with liquor, prompts deadly results, and the patient may drop, quit breathing, or endure cardiovascular breakdown, and kick the bucket. Your primary care physician should know in the event that you are adversely affected by Hydrocodone or other narcotics before you purchase Dilaudid online for utilizing. and purchase Dilaudid on the web

    https://www.healthshopcare.com/
    https://storeonline247.com/

    ReplyDelete
  69. I have visited so many sites but I never got the site like yours, amazing posts with informative latest things. Buy Best Kratom Capsules Online

    ReplyDelete
  70. Anzo Marketing is one of the top real estate marketing companies that deal with top housing projects like NOVA City Islamabad. Customer satisfaction is our top priority.

    ReplyDelete
  71. Very interesting,good job and thanks for sharing such a good blog.your article is so convincing that I never stop myself to say something about it.You’re doing a great job.Keep it up.
    scrum-master-training-in-hyderabad

    ReplyDelete
  72. ery interesting,good job and thanks for sharing such a good blog.your article is so convincing that I never stop myself to say something about it.You’re doing a great job.Keep it up.best overseas recruitment agencies in pakistan

    ReplyDelete
  73. This is really fascinating, You are an excessively professional blogger.I have joined your feed and look ahead to in the hunt for extra of your excellent post.Also, I've shared your website in my social networks: manpower recruitment agency

    ReplyDelete
  74. You have some fantastic programs hidden here. I’m impressed by the quality and variety of what you’ve shared. fullstacktrainingcenter

    ReplyDelete
  75. Thank you for this post.icecreamhaven.in Vanilla is the most iconic and versatile ice cream flavor. With its creamy texture and subtle sweetness, it pairs perfectly with almost any topping or dessert.

    ReplyDelete
  76. <a href="https://elrokn-elmethaly.com/%d8%b4%d8%b1%d9%83%d8%a9-%d9%84%d8%ad%d8%a7%d9%85-%d9%88%d8%b5%d9%8a%d8%a7%d

    ReplyDelete
  77. Nice blog and informative,
    keep posting,
    Visit-> SAP Bloggerss, using this link for Commenting, will give quick response on your commenting
    Visit SAP Bloggers - For more Articles on SAP

    ReplyDelete