您的位置:首页 > 其它

OoenCV学习笔记(2)——图像颜色空间转换

2017-08-08 18:40 267 查看
引言

通常我们看到的图像都是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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: