【问题标题】:How to find HSV values from a certain area as average in a video, python?如何在视频中找到某个区域的 HSV 值作为平均值,python?
【发布时间】:2021-04-16 09:18:41
【问题描述】:

我想在点击后从区域中提取 HSV 值的下限和上限,然后用鼠标在视频而不是图像中裁剪它。

注意:如果您运行此代码,请按“c”键查看值。

代码的来源:herehere 很好地解释了如何工作,他创建了一个很好的代码来在视频here 中找到它们,但我想用鼠标将它们提取为平均类似于此代码。

我是通过图像完成的,但我想在视频中使用它。

如何将其从图像转换为视频?

完整代码。

import cv2
import numpy as np

x_start, y_start, x_end, y_end = 0, 0, 0, 0
cropping = False
getROI = False
refPt = []



# load the image, clone it, and setup the mouse callback function
image = cv2.imread('exp2.png')
clone = image.copy()




def click_and_crop(event, x, y, flags, param):
    # grab references to the global variables
    global x_start, y_start, x_end, y_end, cropping, getROI

    # if the left mouse button was clicked, record the starting
    # (x, y) coordinates and indicate that cropping is being
    # performed
    if event == cv2.EVENT_LBUTTONDOWN:
        x_start, y_start, x_end, y_end = x, y, x, y
        cropping = True

    elif event == cv2.EVENT_MOUSEMOVE:
        if cropping == True:
            x_end, y_end = x, y

    # check to see if the left mouse button was released
    elif event == cv2.EVENT_LBUTTONUP:
        # record the ending (x, y) coordinates and indicate that
        # the cropping operation is finished
        x_end, y_end = x, y
        cropping = False
        getROI = True


cv2.namedWindow("image")
cv2.setMouseCallback("image", click_and_crop)

# keep looping until the 'q' key is pressed
while True:


    i = image.copy()


    if not cropping and not getROI:
        cv2.imshow("image", image)
        # print 'fff'

    elif cropping and not getROI:
        cv2.rectangle(i, (x_start, y_start), (x_end, y_end), (0, 255, 0), 2)
        cv2.imshow("image", i)
        # print 'bbb'

    elif not cropping and getROI:
        cv2.rectangle(image, (x_start, y_start), (x_end, y_end), (0, 255, 0), 2)
        cv2.imshow("image", image)
        # print 'mmm'

    key = cv2.waitKey(1) & 0xFF

    # if the 'r' key is pressed, reset the cropping region
    if key == ord("r"):
        image = clone.copy()
        getROI = False

    # if the 'c' key is pressed, break from the loop
    elif key == ord("c"):   # please press "c" key to get values.
        break

# if there are two reference points, then crop the region of interest
# from teh image and display it
refPt = [(x_start, y_start), (x_end, y_end)]
# print len(refPt)
if len(refPt) == 2:
    roi = clone[refPt[0][1]:refPt[1][1], refPt[0][0]:refPt[1][0]]
    cv2.imshow("ROI", roi)

    hsvRoi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
    print('min H = {}, min S = {}, min V = {}; max H = {}, max S = {}, max V = {}'.format(hsvRoi[:, :, 0].min(),
                                                                                          hsvRoi[:, :, 1].min(),
                                                                                          hsvRoi[:, :, 2].min(),
                                                                                          hsvRoi[:, :, 0].max(),
                                                                                          hsvRoi[:, :, 1].max(),
                                                                                          hsvRoi[:, :, 2].max()))

    lower = np.array([hsvRoi[:, :, 0].min(), hsvRoi[:, :, 1].min(), hsvRoi[:, :, 2].min()])
    upper = np.array([hsvRoi[:, :, 0].max(), hsvRoi[:, :, 1].max(), hsvRoi[:, :, 2].max()])

    image_to_thresh = clone
    hsv = cv2.cvtColor(image_to_thresh, cv2.COLOR_BGR2HSV)

    kernel = np.ones((3, 3), np.uint8)
    # for red color we need to masks.
    mask = cv2.inRange(hsv, lower, upper)
    mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
    mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)

    cv2.imshow("Mask", mask)
    cv2.waitKey(0)
# close all open windows
    cv2.destroyAllWindows()

请帮帮我。

提前致谢。

【问题讨论】:

    标签: python colors detection hsv


    【解决方案1】:

    之后

    cv2.namedWindow("image")
    cv2.setMouseCallback("image", click_and_crop)
    

    将您的 while True: 替换为:

    video_path = "your_video.mp4"
    cap = cv2.VideoCapture(video_path)
    
    while(cap.isOpened()):
        ret, image = cap.read()
    

    所以现在图像是视频中的帧。

    【讨论】:

    • 感谢您的帮助,您的回答如何在视频中运行 python 代码,而不是我的问题的回答。
    猜你喜欢
    • 2016-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    • 2020-08-18
    • 1970-01-01
    • 2022-10-09
    相关资源
    最近更新 更多