Ask Your Question
0

tracking multiple objects by color Python OpenCV 2.x wrapper

asked 2014-07-22 11:55:06 -0500

alejandrozuleta gravatar image

Currently i am trying to track multiple objects by color. I've based on OpenCV-Python Tutorial in the official documentation.

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while(1):

    # Take each frame
    _, frame = cap.read()

    # Convert BGR to HSV
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    # define range of blue color in HSV
    lower_blue = np.array([110,50,50])
    upper_blue = np.array([130,255,255])

    # Threshold the HSV image to get only blue colors
    mask = cv2.inRange(hsv, lower_blue, upper_blue)

    # Bitwise-AND mask and original image
    res = cv2.bitwise_and(frame,frame, mask= mask)

    cv2.imshow('frame',frame)
    cv2.imshow('mask',mask)
    cv2.imshow('res',res)
    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break

cv2.destroyAllWindows()

With the above code I am tracking blue colored objects filtering the HSV blue color. I want to simultaneously track Green colored objects and show both blue and Green in 'res' image.

I've added the following code without success

lower_green = np.array([50, 100, 100])
upper_green = np.array([70, 255, 255]) 
green_mask = cv2.inRange(hsv, lower_green, upper_green) # I have the Green threshold image.

I don't know how to add Green mask and mask (blue) in only one 'res' image using bitwise-and. Could you provide me some guidance.

This is the link to the tutorial Link

Thanks in advance.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2014-07-23 04:01:50 -0500

What you are doing wrong seems quite obvious to me. However my python knowledge is limited so I will just explain it in plain text. It seems you are applying your mask function for both green and blue balls on the same input. This is dangerous, since the will result into a double operation on the same data.

What you should do in some sort of pseudo code - you will have to check if it works, haven't got a python implementation running for the moment -

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while(1):

    # Take each frame
    frame = cap.read()
    frame2 = frame.clone()
    frame3 = frame.clone()

    # Convert BGR to HSV
    hsv_blue = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    hsv_green = hsv.clone() //this will make a copy of the HSV image

    # define range of blue color in HSV
    lower_blue = np.array([110,50,50])
    upper_blue = np.array([130,255,255])

    # define range of blue color in HSV
    lower_green = np.array([50, 100, 100])
    upper_green = np.array([70, 255, 255])

    # Threshold the HSV image to get only blue colors
    mask_blue = cv2.inRange(hsv_blue, lower_blue, upper_blue)
    mask_green = cv2.inRange(hsv_green, lower_green, upper_green)

    # Bitwise-AND mask and original image
    res = cv2.bitwise_and(frame, frame, mask_blue)
    res2 = cv2.bitwise_and(frame2, frame2, mask_green)

    cv2.imshow('frame_blue',frame)
    cv2.imshow('mask_blue',mask_blue)
    cv2.imshow('res_blue',res)

    cv2.imshow('frame_green',frame2)
    cv2.imshow('mask_green',mask_green)
    cv2.imshow('res_green',res2)

    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break

cv2.destroyAllWindows()

Check first if these both work seperately, then I will guide you to combining them.

edit flag offensive delete link more

Comments

I test it, and it works. but how to get blue and green masking on one screen ?

sikonek gravatar imagesikonek ( 2017-04-01 14:18:57 -0500 )edit

You just have to combine the masks with an AND operation, as also shown below.

StevenPuttemans gravatar imageStevenPuttemans ( 2017-04-03 04:46:33 -0500 )edit
0

answered 2014-07-25 08:21:56 -0500

alejandrozuleta gravatar image

I needed to show both blue and Green mask in the same output image. So in another forum they helped me.

I just add blue mask and Green mask.

mask = Green_mask + blue_mask

So I used

res = cv2.bitwise_and(frame, frame, mask)

in order to generate image.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

Stats

Asked: 2014-07-22 11:55:06 -0500

Seen: 9,442 times

Last updated: Jul 25 '14