Hello everyone
Nowadays I am struggling with basic colour detection method. As you now, Basically we are using HSV colour space to detect which colourful marker we want to detect or track. But I am working on a cap which is wearable to Human Head. The cap has a 2 different colourful marker first one is blue second is red. I have no problem to detect blue one but When cap wearing from a person who has a little red face my red colour detects function only detect a face. I want to eliminate that I also tried to create an if statement to classify contours because of the red marker has [10* 2 = 20 cm] small area.
area.
The first function is below for red point detection.
def red(croped):
redLower = (4, 100, 100)
redUpper = (179, 255, 255)
#hsv=croped
hsv = cv2.cvtColor(croped, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, redLower, redUpper)
output = cv2.bitwise_and(hsv, hsv, mask = mask)
cv2.imshow("images", np.hstack([hsv, output]))
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
c = max(cnts, key=cv2.contourArea)
extLeft = tuple(c[c[:, :, 0].argmin()][0])
extRight = tuple(c[c[:, :, 0].argmax()][0])
extTop = tuple(c[c[:, :, 1].argmin()][0])
extBot = tuple(c[c[:, :, 1].argmax()][0])
cv2.drawContours(image, [c], -1, (0, 255, 255), 2)
cv2.circle(image, extLeft, 6, (0, 0, 255), -1)
cv2.circle(image, extRight, 6, (0, 255, 0), -1)
cv2.circle(image, extTop, 6, (255, 0, 0), -1)
cv2.circle(image, extBot, 6, (255, 255, 0), -1)
# show the images
images
#cv2.imshow("images", np.hstack([image, output]))
return extLeft,extRight,extTop,extBot
https://paste.ubuntu.com/p/dSpJKVrvFW/
Second one is a different way to detect red areas I apply 2 hsv filter for hole range.
def hsv(croped):
img_hsv=cv2.cvtColor(croped, cv2.COLOR_BGR2HSV)
# lower mask (0-10)
lower_red = np.array([0,50,50])
upper_red = np.array([10,255,255])
mask0 = cv2.inRange(img_hsv, lower_red, upper_red)
# upper mask (170-180)
lower_red = np.array([170,50,50])
upper_red = np.array([180,255,255])
mask1 = cv2.inRange(img_hsv, lower_red, upper_red)
# join my masks
mask = mask0+mask1
# set my output img to zero everywhere except my mask orjinal görüntü
output_img = croped.copy()
output_img[np.where(mask==0)] = 0
# or your HSV image, which I *believe* is what you want # hsv görüntüsü
output_hsv = img_hsv.copy()
output_hsv[np.where(mask==0)] = 0
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
c = max(cnts, key=cv2.contourArea)
print(c)
# determine the most extreme points along the contour
extLeft = tuple(c[c[:, :, 0].argmin()][0])
extRight = tuple(c[c[:, :, 0].argmax()][0])
extTop = tuple(c[c[:, :, 1].argmin()][0])
extBot = tuple(c[c[:, :, 1].argmax()][0])
cv2.drawContours(croped, [c], -1, (0, 255, 255), 2)
return extLeft,extRight,extTop,extBot
https://paste.ubuntu.com/p/s6f5WrXqZW/
I am waiting your feedback , I found new colour space on web, called OHTA but I am not sure about it.