opencv: 顏色空間轉換(cv2.cvtColor) 探究(圖示+源碼)

 API Definition

我們從 OpenCV官網 的Miscellaneous Image Transformations 上,可查到 cv2.cvtColor 這個api的定義如下:

cvtColor Converts an image from one color space to another. C++: void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0 ) Python: cv2.cvtColor(src, code[, dst[, dstCn]]) → dst C: void cvCvtColor(const CvArr* src, CvArr* dst, int code) Python: cv.CvtColor(src, dst, code) → None Parameters: src – input image: 8-bit unsigned, 16-bit unsigned ( CV_16UC… ), or single-precision floating-point. dst – output image of the same size and depth as src. code – color space conversion code (see the description below). dstCn – number of channels in the destination image; if the parameter is 0, the number of the channels is derived automatically from src and code. The function converts an input image from one color space to another. In case of a transformation to-from RGB color space, the order of the channels should be specified explicitly (RGB or BGR). Note that the default color format in OpenCV is often referred to as RGB but it is actually BGR (the bytes are reversed). So the first byte in a standard (24-bit) color image will be an 8-bit Blue component, the second byte will be Green, and the third byte will be Red. The fourth, fifth, and sixth bytes would then be the second pixel (Blue, then Green, then Red), and so on.

參數探究

在探究的過程中,我發現 code 參數的 輸入類型int 型,於是寫代碼進行驗證:

import cv2
color_types = [cv2.COLOR_BGR2RGB, cv2.COLOR_BGR2GRAY]
for color_type in color_types:
    print ('{}    {}'.format(color_type, type(color_type)))

結果證明了,原來 code 參數的 輸入 不管是cv2.COLOR_BGR2RGBcv2.COLOR_BGR2GRAY,或是其他 顏色轉換空間(color space conversion),均是 int 型的:

4    <type 'int'>
6    <type 'int'>

顏色空間轉換探究

於是我另外編寫了一小段代碼,探究哪些整數可以作爲 cv2.cvtColorcode 參數的 替代輸入值 ,並看看在 轉換了顏色空間 後,會生成什麼樣的圖像。

(自己寫的實驗源碼附在文章末尾)

驗證得知,以下整數可以作爲 cv2.cvtColorcode 參數的 替代輸入值

Valid index in cv2.cvtColor:
[0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 44, 45, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 127, 128, 129, 130, 131, 132, 133, 134]

效果圖

原圖像

在進行 轉換顏色空間 之前的原圖(./pic/origin_pic.jpg):

生成的圖像

./generated_pics/1.jpg:

./generated_pics/2.jpg:

./generated_pics/6.jpg:

./generated_pics/32.jpg:

./generated_pics/34.jpg:

./generated_pics/35.jpg:

./generated_pics/36.jpg:

./generated_pics/38.jpg:

./generated_pics/41.jpg:

./generated_pics/53.jpg:

./generated_pics/54.jpg:

./generated_pics/55.jpg:

./generated_pics/69.jpg:

./generated_pics/72.jpg:

./generated_pics/73.jpg:

./generated_pics/79.jpg:

./generated_pics/82.jpg:

./generated_pics/85.jpg:

Code

附上自己寫的實驗代碼:

# coding=utf-8

origin_pic = './pic/origin_pic.jpg'
save_folder = './generated_pics'

import os
try:
    os.makedirs(save_folder)
except OSError:
    pass

import cv2
img = cv2.imread(origin_pic)
valid_index = []
for color_type in range(-300, 1000, 1):
    try:
        img_new = cv2.cvtColor(img, color_type)
        cv2.imwrite(os.path.join(save_folder, str(color_type)+'.jpg'), img_new)
        valid_index.append(color_type)
    except:
        pass
print ('Valid index in cv2.cvtColor:\n{}'.format(valid_index))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章