首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >选择静态ROI

选择静态ROI
EN

Stack Overflow用户
提问于 2018-11-25 19:40:35
回答 1查看 2.1K关注 0票数 0

我遵循本教程https://www.pyimagesearch.com/2018/07/30/opencv-object-tracking/,您选择ROI的方法是暂停视频和S,然后在要跟踪的对象上创建一个窗口

我需要帮助的是:

  1. 我想选择对象而不暂停视频,也不选择窗口,我的意思是选择窗口是静态的,我只需单击左键来跟踪。
  2. 每次单击另一个对象时,它都会删除前一个对象并跟踪新对象。
  3. 通过鼠标轮控制选择窗口的大小这里是我现在使用的代码

代码语言:javascript
复制
# import the necessary packages
from imutils.video import VideoStream
from imutils.video import FPS
import argparse
import imutils
import time
import cv2
import serial

arduino=serial.Serial('com51', 115200)
# Serial write section

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", type=str,
	help="path to input video file")
ap.add_argument("-t", "--tracker", type=str, default="kcf",
	help="OpenCV object tracker type")
args = vars(ap.parse_args())
# extract the OpenCV version info
(major, minor) = cv2.__version__.split(".")[:2]
 
# if we are using OpenCV 3.2 OR BEFORE, we can use a special factory
# function to create our object tracker
if int(major) == 3 and int(minor) < 3:
	tracker = cv2.Tracker_create(args["tracker"].upper())
 
# otherwise, for OpenCV 3.3 OR NEWER, we need to explicity call the
# approrpiate object tracker constructor:
else:
	# initialize a dictionary that maps strings to their corresponding
	# OpenCV object tracker implementations
	OPENCV_OBJECT_TRACKERS = {
		"csrt": cv2.TrackerCSRT_create,
		"kcf": cv2.TrackerKCF_create,
		"boosting": cv2.TrackerBoosting_create,
		"mil": cv2.TrackerMIL_create,
		"tld": cv2.TrackerTLD_create,
		"medianflow": cv2.TrackerMedianFlow_create,
		"mosse": cv2.TrackerMOSSE_create
	}
 
	# grab the appropriate object tracker using our dictionary of
	# OpenCV object tracker objects
	tracker = OPENCV_OBJECT_TRACKERS[args["tracker"]]()
 
# initialize the bounding box coordinates of the object we are going
# to track
initBB = None
# if a video path was not supplied, grab the reference to the web cam
if not args.get("video", False):
	print("[INFO] starting video stream...")
	vs = VideoStream(src=0).start()
	time.sleep(1.0)
 
# otherwise, grab a reference to the video file
else:
	vs = cv2.VideoCapture(args["video"])
 
 #if vs.isOpened(): 
    # get vs property 
#width2 = vs.get(3)
#height2 = vs.get(4)


# initialize the FPS throughput estimator
fps = None
# loop over frames from the video stream
while True:
	# grab the current frame, then handle if we are using a
	# VideoStream or VideoCapture object
	frame = vs.read()
	frame = frame[1] if args.get("video", False) else frame
	
	# check to see if we have reached the end of the stream
	if frame is None:
		break
 
	# resize the frame (so we can process it faster) and grab the
	# frame dimensions
	#frame = imutils.resize(frame, width=1280)
	(H, W) = frame.shape[:2]
	# check to see if we are currently tracking an object
	if initBB is not None:
		# grab the new bounding box coordinates of the object
		(success, box) = tracker.update(frame)
 
		# check to see if the tracking was a success
		if success:
			(x, y, w, h) = [int(v) for v in box]
			cv2.rectangle(frame, (x, y), (x + w, y + h),
				(0, 255, 0), 2)                                
		# update the FPS counter
		fps.update()
		fps.stop()
		#fixing the x,y tracker box center
		x2=int(x+w/2)
		y2=int(y+h/2)
		#the offsets for the x,y tracking from the center
		sox = str(x2 - (W/2))
		soy = str((H/2) - y2)
		#sending the offsets to arduino
		arduino.write('x'.encode())
		arduino.write(sox.encode())
		#print ("offset X value sent: ")
		#print (sox)
		#time.sleep(0.01)
		arduino.write('y'.encode())
		arduino.write(soy.encode())
		#print ("offset Y value sent : ")
		#print (soy)
		#time.sleep(0.01)
		cv2.line(frame, (int(W/2), int(H/2)), (x2, y2), (0, 255, 0), 1)
		cv2.line(frame, (int(W), int(H/2)), (0, int(H/2)), (0, 0, 0), 2)
		cv2.line(frame, (int(W/2), int(H)), (int(W/2), 0), (0, 0, 0), 2)
		#cv2.line(frame, (320, 240), (x2, y2), (0, 255, 0), 1)
		
		# initialize the set of information we'll be displaying on
		# the frame
		info = [
		      
			#("Tracker", args["tracker"]),
			#("X = ",str(x)),
			#("Y = ",str(y)),
			("offset X = ",sox), 
			("offset y = ",soy),
			#("width = ",W),
			#("height = ",H),
			("FPS", "{:.2f}".format(fps.fps())),
			
		]
 
		# loop over the info tuples and draw them on our frame
		for (i, (k, v)) in enumerate(info):	
			text = "{}: {}".format(k, v)
			cv2.putText(frame, text, (10, H - ((i * 20) + 20)),
				cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
				
				
	         
         

# show the output frame
	cv2.imshow("Frame", frame)
	key = cv2.waitKey(1) & 0xFF
	
 
	# if the 's' key is selected, we are going to "select" a bounding
	# box to track
	
	
	    
	if key == ord("s"):
		# select the bounding box of the object we want to track (make
		# sure you press ENTER or SPACE after selecting the ROI)
		initBB = cv2.selectROI("Frame", frame, fromCenter=False,
			showCrosshair=True)
			
 
		# start OpenCV object tracker using the supplied bounding box
		# coordinates, then start the FPS throughput estimator as well
		tracker.init(frame, initBB)
		fps = FPS().start()
		
	# if the `q` key was pressed, break from the loop
	elif key == ord("q"):
		break
# if we are using a webcam, release the pointer
if not args.get("video", False):
	vs.stop()
 
# otherwise, release the file pointer
else:
	vs.release()
 

# close all windows
cv2.destroyAllWindows()

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-26 02:17:53

您需要对代码进行一些更改。首先,在代码顶部添加这个回调函数,以便在单击时通知openCV该做什么。

代码语言:javascript
复制
mouse_click = False
tracker_location = (0,0)
def click_track(event, x, y, flags, param):
    global mouse_click, tracker_location
    # if the left mouse button was clicked, change flag
    # Click is sensed as a button up
    if event == cv2.EVENT_LBUTTONUP:
        mouse_click = True
        tracker_location = (x,y)

然后,在启动while循环之前,添加此选项以启动显示窗口,并具有鼠标单击的能力。

代码语言:javascript
复制
# Create a window for click detection 
cv2.namedWindow("Frame")
cv2.setMouseCallback("Frame", click_track)

最后,在select ROI曾经出现过的位置,用静态ROI替换它,该静态ROI只在鼠标单击标志为true时才运行。

代码语言:javascript
复制
if mouse_click == True:
    # select the bounding box of the object we want to track (make
    # sure you press ENTER or SPACE after selecting the ROI)
    h , w, ch = frame.shape

    # initial ROI size
    ROI_size_x = 0.1 * w
    ROI_size_y = 0.1 * h

    # Adjust the size when object is near image border
    if( tracker_location[0] + ROI_size_x >= w):
        ROI_size_x = w - tracker_location[0]
    if( tracker_location[1] + ROI_size_y >= h):
        ROI_size_y = h - tracker_location[1]
    if( tracker_location[0] - ROI_size_x < 0):
        tracker_location[0] = 0
    if( tracker_location[1] - ROI_size_y < 0):
        tracker_location[1] = 0

    initBB = ( tracker_location[0] - ROI_size_x, tracker_location[1] - ROI_size_y, ROI_size_x * 2,ROI_size_y * 2)

您可以用任何您喜欢的值来更改静态ROI的大小。目前其设定为中心图像大小的20%。

这是整个代码

代码语言:javascript
复制
# import the necessary packages
from imutils.video import VideoStream
from imutils.video import FPS
import argparse
import imutils
import time
import cv2
import serial

mouse_click = False
tracker_location = (0,0)
def click_track(event, x, y, flags, param):
    global mouse_click, tracker_location

    tracker_location = (x,y)
    # if the left mouse button was clicked, change flag
    # Click is sensed as a button up
    if event == cv2.EVENT_LBUTTONUP:
        mouse_click = True



arduino=serial.Serial('com51', 115200)
# Serial write section



# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", type=str,
    help="path to input video file")
ap.add_argument("-t", "--tracker", type=str, default="kcf",
    help="OpenCV object tracker type")
args = vars(ap.parse_args())
# extract the OpenCV version info
(major, minor) = cv2.__version__.split(".")[:2]

# if we are using OpenCV 3.2 OR BEFORE, we can use a special factory
# function to create our object tracker
if int(major) == 3 and int(minor) < 3:
    tracker = cv2.Tracker_create(args["tracker"].upper())

# otherwise, for OpenCV 3.3 OR NEWER, we need to explicity call the
# approrpiate object tracker constructor:
else:
    # initialize a dictionary that maps strings to their corresponding
    # OpenCV object tracker implementations
    OPENCV_OBJECT_TRACKERS = {
        "csrt": cv2.TrackerCSRT_create,
        "kcf": cv2.TrackerKCF_create,
        "boosting": cv2.TrackerBoosting_create,
        "mil": cv2.TrackerMIL_create,
        "tld": cv2.TrackerTLD_create,
        "medianflow": cv2.TrackerMedianFlow_create,
        "mosse": cv2.TrackerMOSSE_create
    }

    # grab the appropriate object tracker using our dictionary of
    # OpenCV object tracker objects
    tracker = OPENCV_OBJECT_TRACKERS[args["tracker"]]()


# Create a window for click detection 
cv2.namedWindow("Frame")
cv2.setMouseCallback("Frame", click_track)


# initialize the bounding box coordinates of the object we are going
# to track
initBB = None
# if a video path was not supplied, grab the reference to the web cam
if not args.get("video", False):
    print("[INFO] starting video stream...")
    vs = VideoStream(src=0).start()
    time.sleep(1.0)

# otherwise, grab a reference to the video file
else:
    vs = cv2.VideoCapture(args["video"])

 #if vs.isOpened(): 
    # get vs property 
#width2 = vs.get(3)
#height2 = vs.get(4)


# initialize the FPS throughput estimator
fps = None
# loop over frames from the video stream
while True:
    # grab the current frame, then handle if we are using a
    # VideoStream or VideoCapture object
    frame = vs.read()
    frame = frame[1] if args.get("video", False) else frame

    # check to see if we have reached the end of the stream
    if frame is None:
        break

    # resize the frame (so we can process it faster) and grab the
    # frame dimensions
    #frame = imutils.resize(frame, width=1280)
    (H, W) = frame.shape[:2]
    # check to see if we are currently tracking an object
    if initBB is not None:
        # grab the new bounding box coordinates of the object
        (success, box) = tracker.update(frame)

        # check to see if the tracking was a success
        if success:
            (x, y, w, h) = [int(v) for v in box]
            cv2.rectangle(frame, (x, y), (x + w, y + h),
                (0, 255, 0), 2)                                
        # update the FPS counter
        fps.update()
        fps.stop()
        #fixing the x,y tracker box center
        x2=int(x+w/2)
        y2=int(y+h/2)
        #the offsets for the x,y tracking from the center
        sox = str(x2 - (W/2))
        soy = str((H/2) - y2)
        #sending the offsets to arduino
        arduino.write('x'.encode())
        arduino.write(sox.encode())
        #print ("offset X value sent: ")
        #print (sox)
        #time.sleep(0.01)
        arduino.write('y'.encode())
        arduino.write(soy.encode())
        #print ("offset Y value sent : ")
        #print (soy)
        #time.sleep(0.01)
        cv2.line(frame, (int(W/2), int(H/2)), (x2, y2), (0, 255, 0), 1)
        cv2.line(frame, (int(W), int(H/2)), (0, int(H/2)), (0, 0, 0), 2)
        cv2.line(frame, (int(W/2), int(H)), (int(W/2), 0), (0, 0, 0), 2)
        #cv2.line(frame, (320, 240), (x2, y2), (0, 255, 0), 1)

        # initialize the set of information we'll be displaying on
        # the frame
        info = [

            #("Tracker", args["tracker"]),
            #("X = ",str(x)),
            #("Y = ",str(y)),
            ("offset X = ",sox), 
            ("offset y = ",soy),
            #("width = ",W),
            #("height = ",H),
            ("FPS", "{:.2f}".format(fps.fps())),

        ]

        # loop over the info tuples and draw them on our frame
        for (i, (k, v)) in enumerate(info): 
            text = "{}: {}".format(k, v)
            cv2.putText(frame, text, (10, H - ((i * 20) + 20)),
                cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)


    # select the bounding box of the object we want to track (make
    # sure you press ENTER or SPACE after selecting the ROI)
    h , w, ch = frame.shape

    # initial ROI size
    ROI_size_x = 0.1 * w
    ROI_size_y = 0.1 * h

    # Adjust the size when object is near image border
    if( tracker_location[0] + ROI_size_x >= w):
        ROI_size_x = w - tracker_location[0]
    if( tracker_location[1] + ROI_size_y >= h):
        ROI_size_y = h - tracker_location[1]
    if( tracker_location[0] - ROI_size_x < 0):
        tracker_location[0] = 0
    if( tracker_location[1] - ROI_size_y < 0):
        tracker_location[1] = 0

    initBB = ( tracker_location[0] - ROI_size_x, tracker_location[1] - ROI_size_y, ROI_size_x * 2,ROI_size_y * 2)
    #initBB = cv2.selectROI("Frame", frame, fromCenter=False,
    #   showCrosshair=True)

    if mouse_click == True:
        # start OpenCV object tracker using the supplied bounding box
        # coordinates, then start the FPS throughput estimator as well
        tracker = OPENCV_OBJECT_TRACKERS[args["tracker"]]()
        tracker.init(frame, initBB)
        fps = FPS().start()
        mouse_click = False

    #drawing the initial box
    cv2.rectangle(frame,int(initBB[0],initBB[1]),int(initBB[2],initBB[3]),(0,255,255),2)

    # show the output frame
    cv2.imshow("Frame", frame)
    key = cv2.waitKey(1) & 0xFF



    # if the `q` key was pressed, break from the loop
    if key == ord("q"):
        break
# if we are using a webcam, release the pointer
if not args.get("video", False):
    vs.stop()

# otherwise, release the file pointer
else:
    vs.release()


# close all windows
cv2.destroyAllWindows()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53471173

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档