我想测量被跟踪物体距离图像中心的距离,我对python并不是很好,我也不知道如何得到x,y,w,h来自roiBox
对象。
正如您在下面的代码中看到的那样,算法会打印roiBox
输出 - 左上角的x,y和矩形的w,h。
我需要:
int
' 代码:
# USAGE
# python track.py --video video/sample.mov
# import the necessary packages
import numpy as np
import argparse
import cv2
# initialize the current frame of the video, along with the list of ROI points along with whether or not this is input mode
frame = None
roiPts = []
inputMode = False
def select_ROI(event, x, y, flags, param):
# grab the reference to the current frame, list of ROI points and whether or not it is ROI selection mode
global frame, roiPts, inputMode
# if we are in ROI selection mode, the mouse was clicked, and we do not already have four points, then update the list of ROI points with the (x, y) location of the click and draw the circle
if inputMode and event == cv2.EVENT_LBUTTONDOWN and len(roiPts) < 4:
roiPts.append((x, y))
cv2.circle(frame, (x, y), 4, (0, 255, 0), 2)
cv2.imshow("frame", frame)
def determine_ROI_for_first_time():
global inputMode, roiBox, roiPts, roiHist
# indicate that we are in input mode and clone the frame
inputMode = True
orig = frame.copy()
# keep looping until 4 reference ROI points have been selected; press any key to exit ROI selction mode once 4 points have been selected
while len(roiPts) < 4:
cv2.imshow("frame", frame)
cv2.waitKey(0)
# determine the top-left and bottom-right points
roiPts = np.array(roiPts)
s = roiPts.sum(axis = 1)
tl = roiPts[np.argmin(s)]
br = roiPts[np.argmax(s)]
# grab the ROI for the bounding box and convert it to the HSV color space
roi = orig[tl[1]:br[1], tl[0]:br[0]]
roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
#roi = cv2.cvtColor(roi, cv2.COLOR_BGR2LAB)
# compute a HSV histogram for the ROI and store the bounding box
roiHist = cv2.calcHist([roi], [0], None, [16], [0, 180])
roiHist = cv2.normalize(roiHist, roiHist, 0, 255, cv2.NORM_MINMAX)
return (tl[0], tl[1], br[0], br[1])
def do_camshift():
global frame, roiBox
# convert the current frame to the HSV color space and perform mean shift
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
backProj = cv2.calcBackProject([hsv], [0], roiHist, [0, 180], 1)
# apply cam shift to the back projection, convert the points to a bounding box, and then draw them
(r, roiBox) = cv2.CamShift(backProj, roiBox, termination)
pts = np.int0(cv2.boxPoints(r))
cv2.polylines(frame, [pts], True, (0, 255, 0), 2)
def main():
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video",
help = "path to the (optional) video file")
args = vars(ap.parse_args())
# grab the reference to the current frame, list of ROI points and whether or not it is ROI selection mode
global frame, roiPts, inputMode, roiBox, termination
# if the video path was not supplied, grab the reference to the camera
if not args.get("video", False):
camera = cv2.VideoCapture(0)
# camera = cv2.VideoCapture("/home/idan/Desktop/b.mp4")
# otherwise, load the video
else:
camera = cv2.VideoCapture(args["video"])
# setup the mouse callback
cv2.namedWindow("frame")
cv2.setMouseCallback("frame", select_ROI)
# initialize the termination criteria for cam shift, indicating a maximum of ten iterations or movement by a least one pixel along with the bounding box of the ROI
termination = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
roiBox = None
# keep looping over the frames
while True:
# grab the current frame
(grabbed, frame) = camera.read()
# check to see if we have reached the end of the video
if not grabbed:
break
# handle if the 'i' key is pressed, then go into ROI selection mode
key = cv2.waitKey(1) & 0xFF
if key == ord("i") and len(roiPts) < 4:
roiBox = determine_ROI_for_first_time()
# if the see if the ROI has been computed
print roiBox
if roiBox is not None:
do_camshift()
# show the frame and record if the user presses a key
cv2.imshow("frame", frame)
# wait, if the 'q' key is pressed, stop the loop
if key == ord("q"):
break
# cleanup the camera and close any open windows
camera.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()