Ask Your Question

slide bar not working and unable to draw contours

asked 2019-10-15 08:52:57 -0600

MingCheng gravatar image

updated 2019-10-15 08:58:50 -0600

supra56 gravatar image

Hi, I am a beginner to openCV, and wanted trying to implement a slide bar and draw a contours around the masked image. My code is shown as below:

May I also ask that: 1. Sometimes, there is a square bracket with values e.g. [-1], [0] behind the function cv.findContours(mask,cv.RETR_TREE,cv.CHAIN_APPROX_NONE), what are they these square brackets with values is used for? 2. I found contours, hierarchy = cv.findContours(image, mode, method[, contours[, hierarchy[, offset]]]) in the openCV official website. I understand that we can pass in image,mode,and method to the function, but what is the meanning of [, contours[, hierarchy[, offset]]]

Thank you for reading.

import numpy as np
import cv2 as cv
def empty(x):

#lower_blue = np.array([1,80,53])
#upper_blue = np.array([200,205,255])

img = np.zeros((512,512,3),np.uint8)
#img = cv.imread("orange.jpg")
hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
LH = cv.getTrackbarPos("LH", "my_mask")
LS = cv.getTrackbarPos("LS", "my_mask")
LV = cv.getTrackbarPos("LV", "my_mask")
UH = cv.getTrackbarPos("UH", "my_mask")
US = cv.getTrackbarPos("US", "my_mask")
UV = cv.getTrackbarPos("UV", "my_mask")
#lower = np.array([LH,LS,LV])
#upper = np.array([UH,US,UV])
lower = np.array([0,100,100])
upper = np.array([255,255,255])
mask = cv.inRange (hsv, lower, upper)
countours = cv.findContours(mask,cv.RETR_TREE,cv.CHAIN_APPROX_NONE) #it returns a turples, every element of turples starts with "array", it is verified by using len()
#Barea = cv.contourArea


edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2019-10-16 12:57:39 -0600

eyshi gravatar image

.1. Sometimes, there is a square bracket with values e.g. [-1], [0] behind the function

the square bracket with number states which contour you are considerin to use, as it always results in multiple contours, so using '[]' with a number you an decide which contour you want, starting from 0.

2. what is the meanning of [, contours[, hierarchy[, offset]]]

contours –Contours is a list, or tree of lists of points. The points describe each contour, that is, a vector that could be drawn as an outline around the parts of the shape based on it's difference from a background.

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. Hierarchy shows how the shapes relate to each other, layers as such - if shapes are on top of each other this can be determined here. See this : to understand hierarchy

Also check this documentation for more details:

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2019-10-15 08:52:57 -0600

Seen: 218 times

Last updated: Oct 16 '19