如何使用OpenCV在Python中将单个BGR数组转换为HSV数组? [英] How to convert a single BGR array to HSV array in Python with OpenCV?

查看:113
本文介绍了如何使用OpenCV在Python中将单个BGR数组转换为HSV数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个用于图像跟踪的Python程序,在该程序中,我接收最小和最大值的输入BGR值,然后过滤实时视频供稿,以仅显示最小和最大值之间的值.当输入为BGR时,我需要使用HSV过滤视频.但是,我很难弄清楚如何将单个BGR输入数组转换为numpy中的HSV数组,而不必在整个图像上使用cvtColor函数.

I am writing a Python program for image tracking in which I receive input BGR values for min and max values and then filter a live video feed to only show values between those min and max values. While the input is in BGR, I need to filter the video using HSV. However, I am having trouble figuring out how to convert my single BGR input arrays into HSV arrays in numpy without having to use the cvtColor function on the entire image.

根据我的理解,似乎cv2.cvtColor()函数仅适用于整个图像,但是在进行颜色跟踪之前,我需要能够将这些选择的最小和最大BGR数组转换为HSV.

From my understanding, it seems like the cv2.cvtColor() function will only work on the entire image but I need to be able to convert just these select min and max BGR arrays to HSV before I do the color tracking.

每当我运行此代码时,我都会从cvtColor函数调用的OpenCV代码中得到以下错误.

Whenever I run this code I get the following error from the OpenCV code from the cvtColor function call.

OpenCV错误:断言失败(深度== CV_8U ||深度== CV_16U ||深度== CV_32F)在cv :: cvtColor中,文件D:\ Build \ OpenCV \ opencv-3.2.0 \ modules \ imgproc \ src \ color.cpp,第9710行

OpenCV Error: Assertion failed (depth == CV_8U || depth == CV_16U || depth == CV_32F) in cv::cvtColor, file D:\Build\OpenCV\opencv-3.2.0\modules\imgproc\src\color.cpp, line 9710

我尝试使用BGR阵列,该方法可以工作,但是我特别需要在这里使用HSV.

I have tried using the BGR arrays, which works but I specifically need to use HSV here.

解决此问题的最干净方法是什么?如果可以提供更多信息,请告诉我,谢谢.我对Python还是很陌生.

What is the cleanest way to solve this problem? Please let me know if I can provide more information, thanks. I'm still pretty new to Python.

minBGR = np.array([minB, minG, minR])
maxBGR = np.array([maxB, maxG, maxR])

minHSV = cv2.cvtColor(minBGR, cv2.COLOR_BGR2HSV)
maxHSV = cv2.cvtColor(maxBGR, cv2.COLOR_BGR2HSV)

mask = cv2.inRange(hsvFrame, minHSV, maxHSV)

推荐答案

默认情况下,numpy创建具有保留它们所需的最小类型的数组.

By default, numpy creates arrays with the minimum type necessary to hold them.

在我的64位系统中,从0到255范围内的值创建数组会创建int64数组.

Om my 64-bit system, creating arrays from values in the range 0 to 255 creates int64 arrays.

In [1]: import numpy as np

In [2]: np.array([0,0,0])
Out[2]: array([0, 0, 0])

In [3]: a = np.array([0,0,0])

In [4]: a.dtype
Out[4]: dtype('int64')

In [5]: a = np.array([255,255,255])

In [6]: a.dtype
Out[6]: dtype('int64')

这是8个字节的整数:

In [20]: dt = np.dtype('>i8')

In [21]: dt.itemsize
Out[21]: 8

In [22]: dt.name
Out[22]: 'int64'

您可能想创建uint8uint16数组.

In [7]: a = np.array([0,0,0], dtype=np.uint8)

In [8]: b = np.array([255,255,255], dtype=np.uint8)

In [9]: a.dtype, b.dtype
Out[9]: (dtype('uint8'), dtype('uint8'))

这篇关于如何使用OpenCV在Python中将单个BGR数组转换为HSV数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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