OoenCV学习笔记(2)——图像颜色空间转换
2017-08-08 18:40
267 查看
引言
通常我们看到的图像都是RGB颜色空间的图像,但是在图像处理和分析的领域,我们很多时候需要用到其他颜色空间的图像,这就需要我们将RGB类型的图像装换为其他颜色类型的图像。OpenCV提供了cvtColor()函数可以完成图像颜色模式的转换。首先来看cvtColor、
函数的定义:
. OutputArray dst: 输出图像即进行颜色空间变换后存储图像,也可以Mat类
. int code: 转换的代码或标识,即在此确定将什么制式的图片转换成什么制式的图片,后面会详细将
. int dstCn = 0: 目标图像通道数,如果取值为0,则由src和code决定
函数的作用是将图像的从一种颜色空间转换成另一种颜色空间。
实例
通常我们看到的图像都是RGB颜色空间的图像,但是在图像处理和分析的领域,我们很多时候需要用到其他颜色空间的图像,这就需要我们将RGB类型的图像装换为其他颜色类型的图像。OpenCV提供了cvtColor()函数可以完成图像颜色模式的转换。首先来看cvtColor、
函数的定义:
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0 );. InputArray src: 输入图像即要进行颜色空间变换的原图像,可以是Mat类
. OutputArray dst: 输出图像即进行颜色空间变换后存储图像,也可以Mat类
. int code: 转换的代码或标识,即在此确定将什么制式的图片转换成什么制式的图片,后面会详细将
. int dstCn = 0: 目标图像通道数,如果取值为0,则由src和code决定
函数的作用是将图像的从一种颜色空间转换成另一种颜色空间。
实例
#include <iostream> #include <opencv2/highgui/highgui.hpp> #include <opencv2/core/core.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <stdio.h> #define NORM_WIDTH 230 //归一化后的宽和高 #define NORM_HEIGHT 230 using namespace std; using namespace cv; /* 归一化图像大小 */ void resize_img() { Mat src_img; string imgSrcPath = "pic"; string imgSavePath = "pic_new"; imgSrcPath += ".png"; src_img = imread(imgSrcPath,1); namedWindow("Test"); //创建一个名为Test窗口 imshow("Test",src_img); //窗口中显示图像 waitKey(1000); Mat dst_img_rsize(NORM_WIDTH,NORM_HEIGHT,src_img.type()); //创建Mat对象并指定大小和类型 resize(src_img,dst_img_rsize,dst_img_rsize.size(),0,0,INTER_LINEAR); imshow("Test",dst_img_rsize); imgSavePath += ".png"; waitKey(5000); imwrite(imgSavePath,dst_img_rsize); } void convert_color_space(){ //snow.jpg double fScale = 0.1; //Size new_size; string imgSrcPath = "snow.jpg"; Mat src_img = imread(imgSrcPath,1); //new_size.width = src_img.cols * fScale; //new_size.height = src_img.rows * fScale; Mat new_src_img(src_img.rows * fScale,src_img.cols * fScale,src_img.type()); //namedWindow("RGB Space",CV_WINDOW_AUTOSIZE); resize(src_img,new_src_img,new_src_img.size(),0,0,INTER_LINEAR); imshow("RGB Space",new_src_img); waitKey(3000); Mat dst_img; cvtColor(new_src_img,dst_img,COLOR_RGB2Lab); imshow("Lab Space",dst_img); waitKey(3000); cvtColor(new_src_img,dst_img,COLOR_RGB2HSV); imshow("HSV Space",dst_img); waitKey(3000); cvtColor(new_src_img,dst_img,COLOR_RGB2GRAY); imshow("GRAY Space",dst_img); waitKey(3000); cvtColor(new_src_img,dst_img,COLOR_RGB2XYZ); imshow("XYZ Space",dst_img); waitKey(3000); } int main(void) { //resize_img(); convert_color_space(); //转换图像的颜色空间 return 0; }
相关文章推荐
- Python OpenCV学习笔记之:颜色空间转换
- ASP.NET学习笔记六之表单&图像&背景&颜色&颜色值&颜色名
- 【笔记】《WebGL编程指南》学习-第5章颜色与纹理(2-在矩形表面贴上图像))
- 学习笔记TF015:加载图像、图像格式、图像操作、颜色
- Matlab图像处理学习笔记(二):基于颜色的图像分割
- Matlab图像处理学习笔记(二):基于颜色的图像分割
- 图像处理学习笔记(二):基于颜色的图像分割
- Opencv学习笔记(2)模块,图像读取、显示、叠加、融合、颜色分离、亮度、对比度
- 【计算机视觉】EmguCV学习笔记(4)分离颜色通道以及多通道图像混合
- Opencv学习笔记(三):图像的使用与操作_颜色空间的变换
- opencv学习笔记-2,图像颜色空间转换
- 【OpenCV学习笔记 017】图像颜色分布直方图
- 图像处理学习笔记
- 【Matlab学习笔记】【图像滤波去噪】高斯平滑滤波
- MATLAB图像处理学习笔记之一
- OpenCV学习笔记6-图像的基础操作
- MATLAB学习笔记 图像分割(二)
- OpenCV学习笔记:基本的图像处理
- 【OpenCV学习笔记】【函数学习】二十(访问图像像素)
- 基础学习笔记之opencv(20):OpenCV中的颜色空间(ing...)