【问题标题】:Unable to draw lines using OpenCV无法使用 OpenCV 画线
【发布时间】:2020-05-19 16:26:45
【问题描述】:

我创建了一个输入到 ImageAI 检测器的 per_frame 函数。我想在满足距离标准的质心之间画一条线(如果距离> = find_dist_ratio(x1,y1))。应在符合条件的所有对象的质心之间绘制线条,我尝试更改它并最终没有错误地得到它,但线条没有出现在输出视频中。谢谢您的帮助!

def dist_func(counting, output_objects_array,output_objects_count):
     a =[]
     ret, frame = camera.read()
     for d in output_objects_array:
         x1 = d['box_points'][0]
         y1 = d['box_points'][1]
         x2 = d['box_points'][2]
         y2 = d['box_points'][3]
         centroid = (int((x1 + x2) / 2), int((y1 + y2) / 2))
         a.append(centroid)
     for i in range(len(a)):
         for j in range(i+1, len(a)):
            distance = euc_dist(a[i],a[j])
            if distance >= find_dist_ratio(x1, y1):
                print('close enough')
                x, y = a[i]
                X, Y = a[j]
                cv2.line(frame, (x, y), (X, Y), (255, 0, 0), 5)

【问题讨论】:

  • 尝试打印出 bbox 坐标并确保它们是您期望的格式。许多库会按图像大小对边界框进行归一化,因此您可能需要按图像宽度缩放 x 并按图像高度缩放 ​​y。您也可以尝试从 (0,0) 到 (frame.shape[1], frame.shape[0]) 画一条线,以确保您的线图按预期工作

标签: python opencv object-detection video-processing imageai


【解决方案1】:

这听起来可能很愚蠢,但是在您的代码中,我看不出您是否真的在显示框架。如果 x 和 y 变量是正确的(从小写/大写)

请参阅文档中的此示例:

# Create a black image
img = np.zeros((512,512,3), np.uint8)
# Draw a diagonal blue line with thickness of 5 px
cv.line(img,(0,0),(511,511),(255,0,0),5)

为了显示此处绘制的线条,您还应该放置(绘制后)

cv2.imshow("Line draw", img)

Drawing functions 在文档中

【讨论】:

    猜你喜欢
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多