09 OpenCV自带颜色表操作
opencv知识点:
- opencv4的颜色表
- 应用图像映射 - applyColorMap()
本科所解决的问题:
- 如何使用OpenCV中的颜色表?
- 如何循环转换各种颜色风格?
1.Opencv4的[颜色表
opencv提供22种颜色风格的查找表映射,官方文档查阅ColormapTypes
即可
2.颜色表的使用
在使用的时候要用到applyColorMap
,传入对应colormap
,就可以进行颜色风格的转换
applyColorMap
应用颜色图
共3个参数
第1个参数 输入
第2个参数 输出
第3个参数 颜色图(查阅官方文档可知)
如我们传入COLORMAP_DEEPGREEN
,就会有如下效果
重点程序部分
void QuickDemo::color_style_demo(Mat& image) {
Mat dst;
applyColorMap(image, dst, COLORMAP_DEEPGREEN);
imshow("COLORMAP_DEEPGREEN", dst);
}
完整程序
main.cpp
#include "opencv2/opencv.hpp"
#include "quickopencv.h"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
Mat src = imread("C:/Users/LZQ/Desktop/lena.png"); // B, G, R
if (src.empty()) {
printf("could not load image....\n");
return -1;
}
imshow("输入窗口", src);
QuickDemo qd;
qd.color_style_demo(src);
waitKey(0);
destroyAllWindows();
return 0;
}
quickdemo.cpp
#include "quickopencv.h"
#include <opencv2/dnn.hpp>
using namespace cv;
using namespace std;
void QuickDemo::color_style_demo(Mat& image) {
Mat dst;
applyColorMap(image, dst, COLORMAP_DEEPGREEN);
imshow("COLORMAP_DEEPGREEN", dst);
}
quickopencv.h
#pragma once
#include <opencv2/opencv.hpp>
using namespace cv;
class QuickDemo { //快速的演示文件 class类
public:
void color_style_demo(Mat& image); //09
};
3.循环转换颜色风格
我们首先把颜色表做成一个枚举数组
int colormap[]= {
COLORMAP_AUTUMN,
COLORMAP_BONE,
COLORMAP_JET,
COLORMAP_WINTER,
COLORMAP_RAINBOW,
COLORMAP_OCEAN,
COLORMAP_SUMMER,
COLORMAP_SPRING,
COLORMAP_COOL,
COLORMAP_HSV,//10
COLORMAP_PINK,
COLORMAP_HOT,
COLORMAP_PARULA,
COLORMAP_MAGMA,
COLORMAP_INFERNO,
COLORMAP_PLASMA,
COLORMAP_VIRIDIS,
COLORMAP_CIVIDIS,
COLORMAP_TWILIGHT,
COLORMAP_TWILIGHT_SHIFTED,//20
COLORMAP_TURBO,
COLORMAP_DEEPGREEN
};
通过applyColorMap
以及1个while循环,我们便可以实现颜色风格的循环转换
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_HSV,//10
COLORMAP_PINK,
COLORMAP_HOT,
COLORMAP_PARULA,
COLORMAP_MAGMA,
COLORMAP_INFERNO,
COLORMAP_PLASMA,
COLORMAP_VIRIDIS,
COLORMAP_CIVIDIS,
COLORMAP_TWILIGHT,
COLORMAP_TWILIGHT_SHIFTED,//20
COLORMAP_TURBO,
COLORMAP_DEEPGREEN
};
Mat dst;
int index = 0;
while (true) {
int c = waitKey(500);
if (c == 27) {//退出
break;
}
applyColorMap(image, dst, colormap[index % 22]);
index++;
imshow("22种颜色风格",dst);
}
}
标签:颜色,COLORMAP,image,09,OpenCV,DEEPGREEN,自带,dst,Mat
From: https://www.cnblogs.com/L707/p/17047206.html