在OpenCV中裁剪实时视频供稿 [英] Cropping live video feed in OpenCV

查看:143
本文介绍了在OpenCV中裁剪实时视频供稿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实时视频源,该源跟踪绿色对象并在对象区域上绘制一个矩形.我很好奇我如何能够裁剪提要以仅显示矩形所包围的区域.

I have a live video feed that tracks green objects and draws a rectangle over the object's area. I'm curious as to how I would be able to crop the feed to only show the area that the rectangle encompasses.

以下是相关部分:

while True: 

    (success, frame) = webcam.read()

    frame = imutils.resize(frame, width = 1000)
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    mask = cv2.inRange(hsv, greenLower, greenUpper)
    mask = cv2.erode(mask, None, iterations=2)
    mask = cv2.dilate(mask, None, iterations=2)

    cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
    center = None

    if len(cnts) > 0:

        c = max(cnts, key=cv2.contourArea)
        ((x, y), radius) = cv2.minEnclosingCircle(c)
        M = cv2.moments(c)
        center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))

    for c in cnts:
        if cv2.contourArea(c) < 500:
            continue

        (x, y, w, h) = cv2.boundingRect(c)
        cv2.rectangle(frame, (x,y), (x+w, y+h), (0, 255, 0), 2)

    pts = deque(maxlen = 32)
    pts.appendleft(center)

    for i in xrange(1, len(pts)):

        if pts[i - 1] is None or pts[i] is None:
            continue

        thickness = int(np.sqrt(args["buffer"] / float(i + 1)) * 2.5)
        cv2.line(frame, pts[i - 1], pts[i], (0, 255, 0), thickness)

    cv2.imshow("Presentation Tracker", frame)

推荐答案

您可能正在寻找的是使用OpenCV Python创建一个感兴趣区域(ROI)".

What you might be looking for is to create a 'Region of Interest(ROI)' using OpenCV Python.

您可以在代码中如下所示:

You can do so in your code as shown:

(x, y, w, h) = cv2.boundingRect(c)
cv2.rectangle(frame, (x,y), (x+w, y+h), (0, 255, 0), 2)
roi = frame[y:y+h, x:x+w]

请注意,(x,y)对应于矩形的左上角. 上面声明的rect内部区域已存储在Mat roi中.

Note that (x,y) corresponds to the top-left point of your rectangle. The area inside the rect declared above has been stored in Mat roi.

这篇关于在OpenCV中裁剪实时视频供稿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆