OpenCV 学习笔记-day9 OpenCV自带颜色表操作(colormap(色度图)ColormapTypes 枚举数据类型与applyColorMap()伪彩色函数)

OpenCV 学习笔记

    • OpenCV自带颜色表操作
      • 函数
        • colormap(色度图)
        • applyColorMap() 伪彩色函数
      • 代码
        • 显示效果

OpenCV自带颜色表操作

用OpenCV自带颜色表改变图像色彩

函数

  1. colormap(色度图)

OpenCV的定义colormap(色度图),可以应用于灰度图像,使用函数applycolormap产生伪彩色图像。

//定义ColormapTypes 枚举数据类型
enum ColormapTypes
{
    COLORMAP_AUTUMN = 0, //!< ![autumn](pics/colormaps/colorscale_autumn.jpg)
    COLORMAP_BONE = 1, //!< ![bone](pics/colormaps/colorscale_bone.jpg)
    COLORMAP_JET = 2, //!< ![jet](pics/colormaps/colorscale_jet.jpg)
    COLORMAP_WINTER = 3, //!< ![winter](pics/colormaps/colorscale_winter.jpg)
    COLORMAP_RAINBOW = 4, //!< ![rainbow](pics/colormaps/colorscale_rainbow.jpg)
    COLORMAP_OCEAN = 5, //!< ![ocean](pics/colormaps/colorscale_ocean.jpg)
    COLORMAP_SUMMER = 6, //!< ![summer](pics/colormaps/colorscale_summer.jpg)
    COLORMAP_SPRING = 7, //!< ![spring](pics/colormaps/colorscale_spring.jpg)
    COLORMAP_COOL = 8, //!< ![cool](pics/colormaps/colorscale_cool.jpg)
    COLORMAP_HSV = 9, //!< ![HSV](pics/colormaps/colorscale_hsv.jpg)
    COLORMAP_PINK = 10, //!< ![pink](pics/colormaps/colorscale_pink.jpg)
    COLORMAP_HOT = 11, //!< ![hot](pics/colormaps/colorscale_hot.jpg)
    COLORMAP_PARULA = 12, //!< ![parula](pics/colormaps/colorscale_parula.jpg)
    COLORMAP_MAGMA = 13, //!< ![magma](pics/colormaps/colorscale_magma.jpg)
    COLORMAP_INFERNO = 14, //!< ![inferno](pics/colormaps/colorscale_inferno.jpg)
    COLORMAP_PLASMA = 15, //!< ![plasma](pics/colormaps/colorscale_plasma.jpg)
    COLORMAP_VIRIDIS = 16, //!< ![viridis](pics/colormaps/colorscale_viridis.jpg)
    COLORMAP_CIVIDIS = 17, //!< ![cividis](pics/colormaps/colorscale_cividis.jpg)
    COLORMAP_TWILIGHT = 18, //!< ![twilight](pics/colormaps/colorscale_twilight.jpg)
    COLORMAP_TWILIGHT_SHIFTED = 19 //!< ![twilight shifted](pics/colormaps/colorscale_twilight_shifted.jpg)
};

引用举例

int colormap[] = {
		COLORMAP_AUTUMN,
		COLORMAP_BONE,
		COLORMAP_JET,
		COLORMAP_WINTER,
		COLORMAP_RAINBOW,
		COLORMAP_OCEAN,
		COLORMAP_SUMMER,
		COLORMAP_SPRING,
		COLORMAP_COOL,
		COLORMAP_PINK,
		COLORMAP_HOT,
		COLORMAP_PARULA,
		COLORMAP_MAGMA,
		COLORMAP_INFERNO,
		COLORMAP_PLASMA,
		COLORMAP_VIRIDIS,
		COLORMAP_CIVIDIS,
		COLORMAP_TWILIGHT,
		COLORMAP_TWILIGHT_SHIFTED,
	};
  1. applyColorMap() 伪彩色函数

//定义
CV_EXPORTS_W void applyColorMap(InputArray src, OutputArray dst, int colormap);
参数 说明
src 源图像(灰度图或彩色图(CV_8UC1 or CV_8UC3))
dst 在源图像上进行色彩映射后的结果图像
colormap 提供的色彩图代码值(参见:ColormapTypes 枚举数据类型)

applyColorMap(image,dst,colormap[index%20]);//index%20 保证colorMap里面的参数在colormap[0-19]之间,以方便选择枚举数据类型中的成员

代码

#pragma once

#include 

#include 

using namespace cv;
//定义类
class QuickDemo{
public:
	void colorSpace_Demo(Mat &image);//色彩空间转换函数2021-12-24
	void mat_creation_demo(Mat &image);//Mat对象与创建2021-12-27
	void pixel_vist_Demo(Mat &image);//图像像素的读写操作2022-1-3
	void operators_demo(Mat &image);//图像像素的算数操作2022-1-4
	void tracking_bar_demo(Mat &image);//滚动条操作演示2022-1-7
	void key_demo(Mat &image);//键盘响应操作2022-1-12
	void color_style_demo(Mat &image);//OpenCV自带颜色表操作2022-1-12
};

QuickDemo.cpp

#include 

#include 

#include

#include 
void QuickDemo::color_style_demo(Mat &image)
{
	int colormap[] = {
		COLORMAP_AUTUMN,
		COLORMAP_BONE,
		COLORMAP_JET,
		COLORMAP_WINTER,
		COLORMAP_RAINBOW,
		COLORMAP_OCEAN,
		COLORMAP_SUMMER,
		COLORMAP_SPRING,
		COLORMAP_COOL,
		COLORMAP_PINK,
		COLORMAP_HOT,
		COLORMAP_PARULA,
		COLORMAP_MAGMA,
		COLORMAP_INFERNO,
		COLORMAP_PLASMA,
		COLORMAP_VIRIDIS,
		COLORMAP_CIVIDIS,
		COLORMAP_TWILIGHT,
		COLORMAP_TWILIGHT_SHIFTED,
		COLORMAP_HSV,
	};
	Mat dst;
	int index = 0;
	while (true)
	{
		int c = waitKey(1000);
		if (c == 27){
			break;
		}
		applyColorMap(image,dst,colormap[index%20]);//index%19 保证colorMap里面的参数在0-18之间
		index++;
		imshow("色彩风格", dst);
	}
}

OpencvTest.cpp

#include 
#include 
#include 
#include

using namespace cv;
using namespace std;


int main()
{
	Mat scr = imread("...\\image\\1.jpg");//打开一张图
	if (!scr.data == 1)//判空
		return -1;
	namedWindow("窗口1", WINDOW_NORMAL);//创建 WINDOW_FREERATIO窗口
	imshow("窗口1",scr);//在创建的窗口显示
	QuickDemo qd;
	qd.color_style_demo(scr);
	waitKey(0);
	return 0;
}

显示效果

20个颜色间隔1秒依次显示,按ESC键退出程序

你可能感兴趣的:(OpenCV学习,opencv,计算机视觉,人工智能)