【OpenCV 4】伪色彩 applyColorMap() 函数使用

一、编程环境:

OpenCV  4.1.0
IDE Visual Studio 2017 Enterprise (15.9.13)
操作系统 Windows 10 x64 中文专业版 (1903)

二、伪色彩函数:applyColorMap()

在OpenCV库中,常见的伪色彩模式可以通过 applyColorMap(InputArray src, OutputArray dst, int colormap)直接调用。

1、函数说明:

(1)void applyColorMap(InputArray src, OutputArray dst, int colormap)

        src: 源图像(灰度图或彩色图(CV_8UC1 or CV_8UC3))。

        dst: 在源图像上进行色彩映射后的结果图像。

        colormap:提供的色彩图代码值。(参见:ColormapTypes 枚举数据类型)

(2)void applyColorMap(InputArray src, OutputArray dst, InputArray userColor)

        src: 源图像(灰度图或彩色图(CV_8UC1 or CV_8UC3))。

        dst: 在源图像上进行色彩映射后的结果图像。

        userColor:自定义的色彩(CV_8UC1 or CV_8UC3 and size 256)。

2、OpenCV 4.1.0 中定义了色彩图枚举数据类型:ColormapTypes(包含 20 种)。

说明:(1)在 imgproc.hpp 文件中定义。(D:\opencv-4.1.0\build\include\opencv2\imgproc.hpp)

          (2)与 Octave、MATLAB中定义的值完全相同。

//! GNU Octave/MATLAB equivalent colormaps
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)
};

3、色度图:

  • 色度图文件所在目录:D:\opencv-4.1.0\sources\modules\imgproc\doc\pics\colormaps
  • 色度图文件属性:

 【OpenCV 4】伪色彩 applyColorMap() 函数使用_第1张图片

 

  • 色度枚举常量、枚举值、色度图对应表:
枚举值 枚举常量 色度图
0 COLORMAP_AUTUMN
1 COLORMAP_BONE

 

2 COLORMAP_JET
3 COLORMAP_WINTER
4 COLORMAP_RAINBOW
5 COLORMAP_OCEAN
6 COLORMAP_SUMMER
7 COLORMAP_SPRING
8 COLORMAP_COOL
9 COLORMAP_HSV
10 COLORMAP_PINK
11 COLORMAP_HOT
12 COLORMAP_PARULA
13 COLORMAP_MAGMA
14 COLORMAP_INFERNO
15 COLORMAP_PLASMA
16 COLORMAP_VIRIDIS
17 COLORMAP_CIVIDIS
18 COLORMAP_TWILIGHT
19 COLORMAP_TWILIGHT_SHIFTED


五、示例代码:

#include 
#include 

using namespace cv;
using namespace std;
void customColorMap(Mat &image);
int main(int argc, const char *argv[])
{
	Mat src = imread("../images/fp_my01.png");
	if (src.empty()){
		printf("不能加载图像!\n");
		return -1;
	}

	imshow("1-原图", src);

	Mat dst;

	//applyColorMap(src, dst, COLORMAP_SUMMER);
	//applyColorMap(src, dst, COLORMAP_AUTUMN);
	//applyColorMap(src, dst, COLORMAP_BONE);
	applyColorMap(src, dst, COLORMAP_JET); 
	//applyColorMap(src, dst, COLORMAP_WINTER);
	//applyColorMap(src, dst, COLORMAP_RAINBOW); 
	//applyColorMap(src, dst, COLORMAP_OCEAN);
	//applyColorMap(src, dst, COLORMAP_SPRING);
	//applyColorMap(src, dst, COLORMAP_COOL); 
	//applyColorMap(src, dst, COLORMAP_HSV); 
	//applyColorMap(src, dst, COLORMAP_PINK);
	//applyColorMap(src, dst, COLORMAP_HOT);
	//applyColorMap(src, dst, COLORMAP_PARULA);
	//applyColorMap(src, dst, COLORMAP_MAGMA);
	//applyColorMap(src, dst, COLORMAP_INFERNO);
	//applyColorMap(src, dst, COLORMAP_PLASMA);
	//applyColorMap(src, dst, COLORMAP_VIRIDIS);
	//applyColorMap(src, dst, COLORMAP_CIVIDIS);
	//applyColorMap(src, dst, COLORMAP_TWILIGHT);
	//applyColorMap(src, dst, COLORMAP_TWILIGHT_SHIFTED);

	// 显示结果
	imshow("2-ColorMap", dst);

	waitKey(0);
	return 0;
}

六、运行效果:

【OpenCV 4】伪色彩 applyColorMap() 函数使用_第2张图片

你可能感兴趣的:(OpenCV,OpenCV,伪色彩,applyColorMap)