Python 感兴趣区域-基于稀疏光流的目标检测

Python 感兴趣区域-基于稀疏光流的目标检测,python,opencv,object-detection,boundary,opticalflow,Python,Opencv,Object Detection,Boundary,Opticalflow,我正在python中使用。使用YOLOv3,我提取了所需对象的边界框 现在我想给SOF算法提供这些边界,以便只跟踪我想要跟踪的内容 我发现有一个“”函数可以为跟踪找到角点。此函数需要一个“mask”参数,其中表示:“mask–可选感兴趣区域。如果图像不是空的(它需要具有CV_8UC1类型和与图像相同的大小),则指定检测角点的区域。” 这个“面具”就是我要找的 我的问题是:创建遮罩的参数是什么?我的意思是,它需要宽度,高度,x,y或者别的什么?有人能帮忙吗 YOLOv3探测器给出的边界框参数为:浮

我正在python中使用。使用YOLOv3,我提取了所需对象的边界框

现在我想给SOF算法提供这些边界,以便只跟踪我想要跟踪的内容

我发现有一个“”函数可以为跟踪找到角点。此函数需要一个“mask”参数,其中表示:“mask–可选感兴趣区域。如果图像不是空的(它需要具有CV_8UC1类型和与图像相同的大小),则指定检测角点的区域。”

这个“面具”就是我要找的

我的问题是:创建遮罩的参数是什么?我的意思是,它需要宽度,高度,x,y或者别的什么?有人能帮忙吗

YOLOv3探测器给出的边界框参数为:浮动x、y、宽度、高度。其中x和y从左上角开始

编辑: 这就是我一直试图做的

# Initialize the mask with all black pixels
mask = np.zeros_like(first_frame)
# Get the coordinates and dimensions of the detect_box
x = 82
y = 69
w = 35
h = 55
# Set the selected rectangle within the mask to white
mask[y:y+h, x:x+w] = 255
prev = cv.goodFeaturesToTrack(prev_gray, mask = mask, **feature_params)
但我得到了这个错误…:

PS C:\Users\YodhResearch\Desktop\GIULIO FERRARI FOLDER\Giulio _ CSV\Py Script\sparse python> python .\sparseopt.py 
Traceback (most recent call last):
  File ".\sparseopt.py", line 32, in <module>
    prev = cv.goodFeaturesToTrack(prev_gray, mask = mask, **feature_params)
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\featureselect.cpp:365: error: (-215:Assertion failed) _mask.empty() || (_mask.type() == CV_8UC1 && _mask.sameSize(_image)) in 
function 'cv::goodFeaturesToTrack'

np.zeros_like
将创建一个带零的浮点矩阵。从错误消息中可以看出,掩码应为CV_8UC1类型(单通道无符号8位类型)。您可以使用
.astype()
转换任何numpy矩阵类型。在调用goodFeaturesToTrack之前,您需要在此处添加行-mask=mask.astype(np.uint8)。您可以添加输入图像吗?mask是一个与输入图像大小相同的图像,在您不想处理的地方为0,在您想处理的地方为非零。所以,把你检测到的用白色填充的盒子画成一张黑色的图片,然后高兴起来。谢谢你的提示,谢谢你米卡的解释。我已经找到了路,我将插入问题。再次感谢,祝你有一个愉快的一天。
PS C:\Users\YodhResearch\Desktop\GIULIO FERRARI FOLDER\Giulio _ CSV\Py Script\sparse python> python .\sparseopt.py 
Traceback (most recent call last):
  File ".\sparseopt.py", line 32, in <module>
    prev = cv.goodFeaturesToTrack(prev_gray, mask = mask, **feature_params)
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\featureselect.cpp:365: error: (-215:Assertion failed) _mask.empty() || (_mask.type() == CV_8UC1 && _mask.sameSize(_image)) in 
function 'cv::goodFeaturesToTrack'
width  = cap.get(cv.CAP_PROP_FRAME_WIDTH)   # float
height = cap.get(cv.CAP_PROP_FRAME_HEIGHT)  # float
print("w = %f" % width)
print("h = %f" % height)
# Initialize the mask with all black pixels
mask = [[0]*int(width)]*int(height)
mask = np.asarray(mask)
mask = mask.astype(np.uint8) 

print( np.shape(mask) )
# Get the coordinates and dimensions of the detect_box
x = 82
y = 69
w = 35
h = 55
# Set the selected rectangle within the mask to white
mask[y:y+h, x:x+w] = 255

prev = cv.goodFeaturesToTrack(prev_gray, mask = mask, **feature_params)
# Creates an image filled with zero intensities with the same dimensions as the frame - for later drawing purposes
mask = np.zeros_like(first_frame)