首页 > 解决方案 > 如何在视频帧中单击鼠标选择单个对象?

问题描述

我正在做一个项目,在该项目中我必须使用鼠标单击它来选择一个对象,然后我必须找到对象和相机之间的距离。

这是我的代码:

import numpy as np
import cv2
from imutils.video import VideoStream
import argparse
import imutils
import time
import datetime


def find_marker(image):
    # convert the image to grayscale, blur it, and detect edges
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (5, 5), 0)
    edged = cv2.Canny(gray, 35, 125)

    # find the contours in the edged image and keep the largest one;
    # we'll assume that this is our piece of paper in the image
    _, cnts, _ = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    c = max(cnts, key=cv2.contourArea)

    # compute the bounding box of the of the paper region and return it
    return cv2.minAreaRect(c)


def distance_to_camera(knownWidth, focalLength, perWidth):
    # compute and return the distance from the maker to the camera
    return (knownWidth * focalLength) / perWidth


# initialize the known distance from the camera to the object, which
#, in this case, is 24 inches
KNOWN_DISTANCE = 24.0

# initialize the known object width, which in this case, the piece of
# paper is 12 inches wide
KNOWN_WIDTH = 11.0

ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video",
    help="path to the (optional) video file")
args = vars(ap.parse_args())

# if the video path was not supplied, grab the reference to the
# camera
if not args.get("video", False):
    vs = VideoStream(src=0).start()
    time.sleep(2.0)

# otherwise, load the video
else:
    vs = cv2.VideoCapture(args["video"])


# loop over the frames from the video stream


frame = vs.read()
marker = find_marker(frame)
# otherwise, load the video

focalLength = (marker[1][0] * KNOWN_DISTANCE) / KNOWN_WIDTH

while (1):
    # grab the frame from the threaded video stream and resize it
    # to have a maximum width of 400 pixels

    frame = vs.read()
    frame = imutils.resize(frame, width=400)

    if frame is None:
        break



    marker = find_marker(frame)
    # image = cv2.imread(frame)


    inches = distance_to_camera(KNOWN_WIDTH, focalLength, marker[1][0])

    # draw a bounding box around the image and display it
    box = np.int0(cv2.boxPoints(marker))
    cv2.drawContours(frame, [box], -1, (0, 255, 0), 2)
    cv2.putText(frame, "%.2fft" % (inches / 12),
                    (frame.shape[1] - 200, frame.shape[0] - 20), cv2.FONT_HERSHEY_SIMPLEX,
                    2.0, (0, 255, 0), 3)
    timestamp = datetime.datetime.now()
    ts = timestamp.strftime("%A %d %B %Y %I:%M:%S%p")
    cv2.putText(frame, ts, (10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX,
                0.35, (0, 0, 255), 1)

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

    # if the `q` key was pressed, break from the loop
    if key == ord("q"):
        break

# do a bit of cleanup
cv2.destroyAllWindows()
vs.stop()

在这段代码中,我能够找到物体距离,但问题是它一次检测多个物体并给出距离。

我想要的是一次选择一个对象并使用鼠标单击功能在其上制作方形 ROI。

有人知道怎么做吗?

标签: pythonnumpyopencv

解决方案


您可以使用 setMouseCallback 函数来检测 OpenCV 中图像上的鼠标点击。首先创建一个命名窗口,然后使用该图像调用 setMouseCallback 函数和检测鼠标点击的函数。while 循环将持续运行,直到您在键盘上选择“c”来中断并完成收集点。Xpt 和 Ypt 返回您单击图像位置的 x 和 y 像素坐标列表。如果您在错误的位置单击,请按“r”,它将重置列表并创建一个新图像以单击。

clone = image.copy()
cv2.namedWindow('image you want to click on')
cv2.setMouseCallback('image you want to click on', click_and_extract_points)

while 1:
    cv2.imshow('image', image)
    key = cv2.waitKey(1) & 0xFF

    if key == ord('r'):  # if mouse clicks done incorrectly, press 'r'. Resets arrays and map image.
        Xpt = []
        Ypt = []
        image = clone.copy()

    elif key == ord('c'):
        break

def click_and_extract_points(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDBLCLK:
        Xpt.append(x)
        Ypt.append(y)
        print(Xpt, Ypt)

推荐阅读