[OpenCV-Python] 13 Color space conversion

OpenCV-Python: IV Image Processing in OpenCV

13 Color space conversion

Goal
  • You will learn how to convert the color space of an image, such as from BGR to grayscale, or from BGR to HSV, etc.
  • I didn't have to create a program to get objects of a certain color from an image.
  • The functions we will learn are: cv2.cvtColor(), cv2.inRange() and so on.

13.1 Convert color space

There are more than 150 methods for color space conversion in OpenCV. But you will find that there are only two types we often use: BGR↔Gray and BGR↔HSV.
The function we will use is: cv2.cvtColor(input_image, flag), where flag is the conversion type.
For the conversion of BGR↔Gray, the flag we want to use is cv2.COLOR_BGR2GRAY.
Also for the conversion of BGR↔HSV, the flag we use is cv2.COLOR_BGR2HSV.
You can also get all available flags through the following command.

import cv2
flags=[i for in dir(cv2) if i startswith('COLOR_')]
print flags

Note: In the OpenCV HSV format, the value range of H (color/chroma) is [0, 179], the value range of S (saturation) is [0, 255], and the value range of V (brightness) [0,255]. But different software may use different values. So when you need to compare the HSV value of OpenCV with the HSV value of other software, you must remember to normalize.

13.2 Object tracking

Now that we know how to convert an image from BGR to HSV, we can use this to extract objects with a certain color. It is easier to represent a specific color in the HSV color space than in the BGR space. In our program, what we want to extract is a blue object. Here are the steps we need to do:
  • Get each frame of the image from the video
  • Convert the image to HSV space
  • Set the HSV threshold to the blue range.
  • Get the blue object. Of course, we can also do anything else we want to do, such as drawing a circle around the blue object.
Here is our code:

import cv2
import numpy as np cap=cv2.VideoCapture(0) while(1):
# 获取每一帧
ret,frame=cap.read()

# 转换到 HSV
hsv=cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)

# 设定蓝色的阈值
lower_blue=np.array([110,50,50]) 
upper_blue=np.array([130,255,255])

# 根据阈值构建掩模
mask=cv2.inRange(hsv,lower_blue,upper_blue)

# 对原图像和掩模进行位运算
res=cv2.bitwise_and(frame,frame,mask=mask)

# 显示图像cv2.imshow('frame',frame) 
cv2.imshow('mask',mask) 
cv2.imshow('res',res) k=cv2.waitKey(5)&0xFF
if k==27:
	break
# 关闭窗口
cv2.destroyAllWindows()

img

Note: This is the simplest method in object tracking. After you learn the outline, you will learn more related knowledge, that is, you can find the center of gravity of the object, and track the object according to the center of gravity, just wave your hand in front of the camera to draw the same graphics, or other More interesting things.

13.3 How to find the HSV value of the object to be tracked?

This is the most common question I have encountered on stackoverflow.com. In fact, this is really simple, the function cv2.cvtColor() can also be used here. But now the parameter you want to pass in is the (what you want) BGR value instead of a picture. For example, if we want to find the green HSV value, we only need to enter the following command in the terminal:

import cv2
import numpy as np
green=np.uint8([0,255,0])
hsv_green=cv2.cvtColor(green,cv2.COLOR_BGR2HSV)
error: /builddir/build/BUILD/opencv-2.4.6.1/
modules/imgproc/src/color.cpp:3541:
error: (-215) (scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F)
in function cvtColor
#scn (the number of channels of the source),
#i.e. self.img.channels(), is neither 3 nor 4.
#
#depth (of the source),
#i.e. self.img.depth(), is neither CV_8U nor CV_32F.
# 所以不能用 [0,255,0] ,而要用 [[[0,255,0]]]
# 这里的三层括号应该分别对应于 cvArray , cvMat , IplImage
green=np.uint8([[[0,255,0]]])
hsv_green=cv2.cvtColor(green,cv2.COLOR_BGR2HSV)
print(hsv_green)
# [[[60 255 255]]]

Now you can use [H-100, 100, 100] and [H+100, 255, 255] as upper and lower thresholds respectively. In addition to this method, you can use any other image editing software (such as GIMP) or online conversion software to find the corresponding HSV value, but don't forget to adjust the HSV range at the end.

For more information, please pay attention to the official account:
img

Guess you like

Origin blog.csdn.net/yegeli/article/details/113406099