Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

slide bar not working and unable to draw contours

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()

slide bar not working and unable to draw contours

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])

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")

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])

"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)

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()

cv.imshow("my_mask",mask) cv.waitKey(0) cv.destroyAllWindows()

slide bar not working and unable to draw contours

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), 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,

`contours, hierarchy =   cv.findContours(    image, cv.findContours(image, mode, method[, contours[, hierarchy[, offset]]]  ) 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]]]"

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()

slide bar not working and unable to draw contours

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, contours, hierarchy = cv.findContours(image, mode, method[, contours[, hierarchy[, offset]]])` 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]]`]
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()