如何在视频帧中单击鼠标选择单个对象?



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

这是我的代码:

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。

有人知道如何做到这一点吗?

您可以使用 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)

您可以使用 opencv 提供的内置函数:

roi = cv2.selectROI(image)

它创建/显示"ROI选择"窗口,显示帧。使用鼠标选择边界框,将鼠标指针从左上角拖动到右下角。

该函数有几个不需要的参数:

selectROI(windowName, img[, showCrosshair[, fromCenter]]) -> retval
  • 参数windowName选择过程所在的窗口的名称 显示。
  • 参数img图像以选择 ROI。
  • 参数showCrosshair选择矩形的真正十字准线是否为 显示。
  • 参数fromCenter选择中心是否与初始鼠标匹配 位置。在相反的情况下,选择矩形的一角将 对应于初始鼠标位置。

如果选择被取消,则返回选定的 ROI 或空矩形。

控制:使用spaceenter完成选择,使用键c取消选择。

重载函数不需要windowName

selectROI(img[, showCrosshair[, fromCenter]]) -> retval

最新更新