Ask Your Question
0

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):
    pass

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

img = np.zeros((512,512,3),np.uint8)
cv.rectangle(img,(100,100),(277,307),(155,0,0),-1,)
#cv.circle(img,(450,450),50,(155,0,0),-1)
#img = cv.imread("orange.jpg")
hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
cv.namedWindow("my_mask")
cv.createTrackbar("LH","my_mask",0,255,empty)
cv.createTrackbar("LS","my_mask",0,255,empty)
cv.createTrackbar("LV","my_mask",0,255,empty)
cv.createTrackbar("UH","my_mask",255,255,empty)
cv.createTrackbar("US","my_mask",255,255,empty)
cv.createTrackbar("UV","my_mask",255,255,empty)
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
#print(len(countours))
#print(countours[2])
#cv.drawContours(img,[countours],-1,(0,255,255),5)
cv.imshow("original",img)
cv.imshow("my_mask",mask)

cv.waitKey(0)

cv.destroyAllWindows()
edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
1

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 : https://docs.opencv.org/trunk/d9/d8b/... to understand hierarchy

Also check this documentation for more details: https://docs.opencv.org/3.3.1/d4/d73/...

edit flag offensive delete link more

Question Tools

1 follower

Stats

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

Seen: 263 times

Last updated: Oct 16 '19