100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > opencv: 颜色空间转换(cv2.cvtColor) 探究(图示+源码)

opencv: 颜色空间转换(cv2.cvtColor) 探究(图示+源码)

时间:2024-08-02 20:02:04

相关推荐

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 cv2color_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-8origin_pic = './pic/origin_pic.jpg'save_folder = './generated_pics'import ostry:os.makedirs(save_folder)except OSError:passimport cv2img = 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:passprint ('Valid index in cv2.cvtColor:\n{}'.format(valid_index))

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。