how to detect 2 other objects? use camshift

asked 2019-07-23 21:07:04 -0600

porori0407 gravatar image

updated 2019-07-23 21:18:28 -0600

supra56 gravatar image

hi all.

I'm newbie in opencv. i have just 1 question.

i want to tracking 2 or more(3++) objects.

i only make just 1 tracking object source code.

then, how to add more detecting?

plz tell me how to add.

this is my code:

import cv2
import numpy as np

col, width, row, height = -1,-1,-1,-1

frame = None

frame2 = None
frame3 = None

inputmode1 = False
inputmode2 = False

rectangle1 = False
rectangle2 = False

trackWindow1 = None
trackWindow2 = None

roi_hist = None

def onMouse(event, x, y, flags, param):
    global col, width, row, height, frame, frame2, frame3, inputmode1, inputmode2
    global rectangle1, roi_hist, trackWindow1, trackWindow2, rectangle2

    if inputmode1:

        # 마우스클릭 이벤트
        if event == cv2.EVENT_LBUTTONDOWN:
            # 그리기 활성화
            rectangle1 = True
            col, row = x,y

        elif event == cv2.EVENT_MOUSEMOVE:

            if rectangle1:             
                frame = frame2.copy()
                cv2.rectangle(frame,(col, row), (x,y),(0,255,0),2)
                cv2.imshow('frame',frame)

        elif event == cv2.EVENT_LBUTTONUP:

            inputmode1 = False
            rectangle1 = False
            cv2.rectangle(frame,(col,row),(x,y),(0,255,0),2)
            height, width = abs(row-y),abs(col-x)
            trackWindow1 = (col, row, width, height)
            roi = frame[row:row+height, col:col+width]
            roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
            roi_hist = cv2.calcHist([roi],[0], None, [180],[0,180])
            cv2.normalize(roi_hist, roi_hist, 0 , 255, cv2.NORM_MINMAX)

    if inputmode2:

        if event == cv2.EVENT_LBUTTONDOWN:
            # 그리기 활성화
            rectangle2 = True
            col, row = x,y

        elif event == cv2.EVENT_MOUSEMOVE:
            if rectangle2:               
                frame = frame3.copy()

                cv2.rectangle(frame,(col, row), (x,y),(255,0,0),2)
                cv2.imshow('frame',frame)    
        elif event == cv2.EVENT_LBUTTONUP:           
            inputmode2 = False
            rectangle2 = False
            cv2.rectangle(frame,(col,row),(x,y),(255,0,0),2)
            height, width = abs(row-y),abs(col-x)
            trackWindow2 = (col, row, width, height)
            roi = frame[row:row+height, col:col+width]
            roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
            roi_hist = cv2.calcHist([roi],[0], None, [180],[0,180])
            cv2.normalize(roi_hist, roi_hist, 0 , 255, cv2.NORM_MINMAX)
    return
def CamShift():
    global frame, frame2, frame3, inputmode1,inputmode2,trackWindow1, trackWindow2, roi_hist

    try:
        cap = cv2.VideoCapture(1)

    except Exception as e:
        print(e)
        return

    ret, frame = cap.read()
    cv2.namedWindow('frame')


    cv2.setMouseCallback('frame',onMouse,param=(frame,frame2, frame3)) 
    termination = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)

    while True:
        ret, frame = cap.read()

        if not ret:
            break

        if trackWindow1 is not None:

            hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)

            dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1)
            cv2.imshow('dst',dst)

            ret, trackWindow1 = cv2.CamShift(dst, trackWindow1, termination)

            pts = cv2.boxPoints(ret)
            pts = np.int0(pts)

            cv2.polylines(frame,[pts],True,(0,255,0),2)

        elif trackWindow2 is not None:

            hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)

            dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1)
            cv2.imshow('dst',dst)

            ret, trackWindow2 = cv2.CamShift(dst, trackWindow2, termination)

            pts = cv2.boxPoints(ret)
            pts = np.int0(pts)

            cv2.polylines(frame,[pts],True,(255,0,0),2)

        cv2.imshow('frame',frame)

        k = cv2.waitKey(1)
        if k == 27:
            break

        if k == ord('1'):
            print("please select figure #1")
            inputmode1 = True

            frame2 = frame.copy()

            cv2.imshow('frame',frame)
            cv2.waitKey(0)

        if k == ord('2'):
            print("please select figure #2")
            inputmode2 = True

            frame3 ...
(more)
edit retag flag offensive close merge delete

Comments

Please do not duplicate. Be patient. Wait until for someone who can help you.

supra56 gravatar imagesupra56 ( 2019-07-23 21:21:32 -0600 )edit
1

there is a logical problem in your code : if you got trackWindow1 and trackWindow2 active you cannot track two windows because :

      if trackWindow1 is not None:
 .....
      elif trackWindow2 is not None:

why elif?

LBerger gravatar imageLBerger ( 2019-07-24 00:03:44 -0600 )edit