Changing Colorspaces
Goal
- In this tutorial, you will learn how to convert images from one color-space to another, like BGR ? Gray, BGR ? HSV, etc.
- In addition to that, we will create an application to extract a colored object in a video
- You will learn the following functions: cv.cvtColor(), cv.inRange(), etc.
- 在这个教程,你将学习如何将图片从一个色彩空间转换到另一个,比如BGR?Gray,BGR?HSV等等。
- 除此之外,我们将创建一个应用来提取视频中的彩色对象
- 你将学习下列的函数:cv.cvtColor(),cv.inRange()等等
Changing Color-space
There are more than 150 color-space conversion methods available in OpenCV. But we will look into only two, which are most widely used ones: BGR ? Gray and BGR ? HSV.
For color conversion, we use the function cv.cvtColor(input_image, flag) where flag determines the type of conversion.
For BGR → Gray conversion, we use the flag cv.COLOR_BGR2GRAY. Similarly for BGR → HSV, we use the flag cv.COLOR_BGR2HSV. To get other flags, just run following commands in your Python terminal:
在OpenCV中有超过150种色彩空间转换方法。但是我们将仅仅研究两个应用最广泛的:BGR?Gray和BGR?HSV。
对于颜色转换,我们用cv.cvtColor函数(input_image,flag)。flag决定转换的类型。
对于BGR?Gray转换,我们用标志cv.COLOR_BGR2GRAY。相似的BGR?HSV,我们用标志cv.COLOR_BGR2HSV。想得到其他的标志,在你的Python命令行里运行下列的命令:
>>>import cv2 as cv
>>>flags =[i for i in dir(cv) if i.startswith("COLOR_")]
>>>print(flags)
-
note
对于HSV,色相范围是[0,179],饱和范围是[0,255],值范围是[0,255]。不同的软件用不同的规模。所以如果你想用OpenCV的值和它们比较,你需要常规化这些范围。
- For HSV, hue range is [0,179], saturation range is [0,255], and value range is [0,255]. Different software use different scales. So if you are comparing OpenCV values with them, you need to normalize these ranges.
Object Tracking目标追踪
Now that we know how to convert a BGR image to HSV, we can use this to extract a colored object. In HSV, it is easier to represent a color than in BGR color-space. In our application, we will try to extract a blue colored object. So here is the method:
- Take each frame of the video
- Convert from BGR to HSV color-space
- We threshold the HSV image for a range of blue color
- Now extract the blue object alone, we can do whatever we want on that image.
Below is the code which is commented in detail:
现在我们知道了如何将BGR图像转换为HSV,我们可以用这个来提取彩色对象。在HSV中,比BGR色彩空间更容易表示一种颜色。在我们的应用中,我们将尝试提取一个蓝色的物体。下面是方法:
- 提取视频的每一帧
- 从BGR转换到HSV
- 将HSV图像设置为蓝色的阈值
- 单独提取蓝色物体,我们可以对那图片为所欲为。
下面是详细注释的代码:
import cv2 as cv
import numpy as np
cap = cv.VideoCapture(0)
while(1):#take each frame_,frame = cap.read()#convert BGR to HSVhsv = cv.cvtColor(frame,cv.COLOR_BGR2HSV)#define range of blue color in HSVlower_blue = np.array([110,50,50,])upper_blue = np.array([130,255,255])#threshold the HSV image to get only blue colorsmask = cv.inRange(hsv,lower_blue,upper_blue)#bitwise-AND mask and original imageres = cv.bitwise_anf(frame,frame,mask=mask)cv.imshow("frame",frame)cv.imshow("mask",mask)cv.imshow("res",res)k = cv.waitKey(5)&0xFFif k==27:break
cv.destroyAllWindows()
Below image shows tracking of the blue object:
下面图片展示了蓝色物体的追踪:
-
Note
There is some noise in the image. We will see how to remove it in later chapters.
This is the simplest method in object tracking. Once you learn functions of contours, you can do plenty of things like find the centroid of an object and use it to track the object, draw diagrams just by moving your hand in front of a camera, and other fun stuff.
-
note
在图片中有一些噪声。在接下来的章节我们将知道如何来去除它。
这是最简单的物体追踪方法。一旦你学会了轮廓的功能,你可以做很多事,比如找到物体的质心并用它来追踪物体,通过移动你的手到相机前面来绘制图表,还有其他有趣的事。
How to find HSV values to track?如何找到要追踪的HSV值
This is a common question found in stackoverflow.com. It is very simple and you can use the same function, cv.cvtColor(). Instead of passing an image, you just pass the BGR values you want. For example, to find the HSV value of Green, try the following commands in a Python terminal:
这是在stackoverflow.com上一个常见的问题。这非常简单,你可以用同样的函数,cv.cvtColor()。你仅仅传递你想要的BGR值,而不是传递整个图像。例如,想要找到绿色的HSV值,在Python终端尝试下面的命令:
>>>green = np.uint8([[[0,255,0]]])
>>>hsv_green = cv.cvtColor(green,cv.COLOR_BGR2HSV)
>>>print(hsv_green)
[[[60 255 255]]]
Now you take [H-10, 100,100] and [H+10, 255, 255] as the lower bound and upper bound respectively. Apart from this method, you can use any image editing tools like GIMP or any online converters to find these values, but don’t forget to adjust the HSV ranges.
现在你分别将[H-10,100,100]和[H+10,255,255]作为下界和上界。除了这个方法外,你可以使用任何图片编辑工具例如GIMP或者在线转换器来找到这些值,但是不要忘记调整HSV的范围。
Additional Resources
Exercises
-
Try to find a way to extract more than one colored object, for example, extract red, blue, and green objects simultaneously.
尝试找到一种方法来提取超过一种的彩色物体,例如,同时提取红,蓝,和绿色的物体。