簡體   English   中英

在OpenCV中裁剪實時視頻供稿

[英]Cropping live video feed in OpenCV

我有一個實時視頻源,該源跟蹤綠色對象並在對象區域上繪制一個矩形。 我很好奇如何能夠裁剪提要以僅顯示矩形所包圍的區域。

這是相關的部分:

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)”。

您可以在代碼中執行此操作,如下所示:

(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中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM