前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >opencv: 颜色空间转换(cv2.cvtColor) 探究(图示+源码)

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

作者头像
JNingWei
发布2018-09-28 15:51:51
3K0
发布2018-09-28 15:51:51
举报
文章被收录于专栏:JNing的专栏JNing的专栏

 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 型,于是写代码进行验证:

代码语言:javascript
复制
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 型的:

代码语言:javascript
复制
4    <type 'int'>
6    <type 'int'>

颜色空间转换探究

于是我另外编写了一小段代码,探究哪些整数可以作为 cv2.cvtColorcode 参数的 替代输入值 ,并看看在 转换了颜色空间 后,会生成什么样的图像。

(自己写的实验源码附在文章末尾)

验证得知,以下整数可以作为 cv2.cvtColorcode 参数的 替代输入值

代码语言:javascript
复制
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

附上自己写的实验代码:

代码语言:javascript
复制
# 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))
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年08月30日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  •  API Definition
  • 参数探究
  • 颜色空间转换探究
  • 效果图
    • 原图像
    • 生成的图像
    • Code
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档