我正在研究一个项目,在该项目中,必须使用鼠标单击选择一个对象,然后找到该对象与相机之间的距离。
这是我的代码:
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。
有人知道该怎么做吗?
答案 0 :(得分:0)
您可以使用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)
答案 1 :(得分:0)
您可以使用opencv提供的内置功能:
roi = cv2.selectROI(image)
它创建/显示“ ROI选择”窗口,显示框架。使用鼠标选择边界框,将鼠标指针从左上角拖动到右下角。
该函数有几个不需要的参数:
selectROI(windowName, img[, showCrosshair[, fromCenter]]) -> retval
windowName
进行选择过程的窗口的名称
显示。img
图片以选择投资回报率。showCrosshair
,如果选择矩形的真实十字准线为
显示。fromCenter
,如果选择的真实中心将与初始鼠标匹配
位置。在相反的情况下,选择矩形的一个角将
对应于鼠标的初始位置。返回选择的ROI或如果选择被取消则为空矩形。
控件:使用space
或enter
完成选择,使用键c
取消选择。
重载函数不需要windowName
。
selectROI(img[, showCrosshair[, fromCenter]]) -> retval