1. 程式人生 > 其它 >OpenCV-Python官方文件中文翻譯13:Changing Colorspaces改變色彩空間

OpenCV-Python官方文件中文翻譯13:Changing Colorspaces改變色彩空間

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 HSV
	hsv = cv.cvtColor(frame,cv.COLOR_BGR2HSV)
	#define range of blue color in HSV
	lower_blue = np.array([110,50,50,])
	upper_blue = np.array([130,255,255])
	#threshold the HSV image to get only blue colors
	mask = cv.inRange(hsv,lower_blue,upper_blue)
	#bitwise-AND mask and original image
	res = cv.bitwise_anf(frame,frame,mask=mask)
	cv.imshow("frame",frame)
	cv.imshow("mask",mask)
	cv.imshow("res",res)
	k = cv.waitKey(5)&0xFF
	if k==27:
		break
cv.destroyAllWindows()

Below image shows tracking of the blue object:

下面圖片展示了藍色物體的追蹤:

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片儲存下來直接上傳(img-vvwbxg3S-1613479013764)(https://docs.opencv.org/4.5.1/frame.jpg)]

  • 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

  1. Try to find a way to extract more than one colored object, for example, extract red, blue, and green objects simultaneously.

    嘗試找到一種方法來提取超過一種的彩色物體,例如,同時提取紅,藍,和綠色的物體。