如何在视频python中找到某个区域的HSV值作为平均值?

问题描述

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

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

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

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

如何将图片转为视频?

完整代码

import cv2
import numpy as np

x_start,y_start,x_end,y_end = 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_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_end = x,y
        cropping = True

    elif event == cv2.EVENT_MOUSEMOVE:
        if cropping == True:
            x_end,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
        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,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_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(),2].min(),0].max(),1].max(),2].max()))

    lower = np.array([hsvRoi[:,2].min()])
    upper = np.array([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()

请帮帮我。

提前致谢。

解决方法

之后

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()

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