How to draw particular or multiple contours of objects


I can't seem to find a way to draw more than one contour of objects.

Input Image:

<a href="https://i.stack.imgur.com/lMm7v.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/lMm7v.png" data-original="https://i.stack.imgur.com/lMm7v.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>


import cv2 import numpy as np #import image img = cv2.imread('img.png', 0) #Thresh ret, thresh = cv2.threshold(img, 200, 255, cv2.THRESH_BINARY) #Finding the contours in the image _, contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #Convert img to RGB and draw contour img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB) cv2.drawContours(img, contours, 0, (0,0,255), 2) #save output img cv2.imwrite('output_img.png', img)


<a href="https://i.stack.imgur.com/w0z3h.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/w0z3h.png" data-original="https://i.stack.imgur.com/w0z3h.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

Only the larger object contour is drawn. How would I draw both contours?


Change the third parameter to -1(third argument is index of contours) in your <a href="https://docs.opencv.org/3.1.0/d4/d73/tutorial_py_contours_begin.html" rel="nofollow">drawContours</a> which will draw all contours in your image:

cv2.drawContours(img, contours, -1, (0,0,255), 2)

If you want to draw only two contours and the first two contours are of the white objects use:

cnt1 = contours[0] cnt2 = contours[1] cv2.drawContours(img, [cnt1, cnt2], -1, (0,0,255), 2)


  • Index was outside the bounds of the array
  • How to duplicate a Firebase child with many records without downloading and uploading the data?
  • “No rule to make target” Error When Using make -j with cmake
  • What is the best way to determine what JRadioButton is selected?
  • alert message on submit if text box is empty using PHP
  • How to check performance of mysql query?
  • PYQT QSplitter issue
  • How to debug parameterized SQL query
  • Can I retrieve the stored value x in a hashset given an object y where x.Equals(y)
  • Sceneform Collisions
  • How can I change never notify in 'user account control settings'? [duplicate]
  • Spring Boot 2 not serializing LocalDateTime
  • XPath in PHP Removes HTML Tags
  • Get Last Item in nested database firebase android
  • new in Xcode 6.3/iOS 8.3: using self alloc for convenience constructor causes build error
  • Error Obtaining Lock (org.neo4j.kernal.StoreLockException)
  • How can I convert a null-terminated string in a byte buffer to a string in Go?
  • Spark streaming is not working in Standalone cluster deployed in VM
  • unable to pass array from controller to view in laravel
  • How would you markup a building plan/map using semantic HTML?
  • Inno Setup: Checking existence of a file in 32-bit System32 (Sysnative) folder
  • Android BottomNavigationView with Badge [closed]
  • Count of values within specified range of value in each row using data.table
  • ASP.Net MVC entity framework submit model, then open new model in edit page
  • how to transform dataframe that contains list in every row of each column
  • NodeJS - installing local module
  • Joining across databases with dbplyr
  • How exactly do you use json_decode to pass a javascript array to php?
  • Python sum values in tuple in a list in a dictionary?
  • Access to database zend framework
  • java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/SpringDispatcher-ser