当前位置:网站首页>Opencv learning notes-day9 opencv's own color table operation (colormap coloraptypes enumeration data types and applycolormap() pseudo color function)

Opencv learning notes-day9 opencv's own color table operation (colormap coloraptypes enumeration data types and applycolormap() pseudo color function)

2022-06-30 08:47:00 Chasing foot dream

OpenCV With color table operation

use OpenCV Bring your own color table to change the image color

function

  1. colormap( Chromaticity diagram )

OpenCV The definition of colormap( Chromaticity diagram ), Can be applied to grayscale images , Using functions applycolormap Generate pseudo color image .

// Definition ColormapTypes  Enumerate data types 
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)
};

Quote an example

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() Pseudo color function

// Definition 
CV_EXPORTS_W void applyColorMap(InputArray src, OutputArray dst, int colormap);
Parameters explain
src The source image ( Grayscale or color chart (CV_8UC1 or CV_8UC3))
dst The result image after color mapping on the source image
colormap Color map code value provided ( See :ColormapTypes Enumerate data types )

example

applyColorMap(image,dst,colormap[index%20]);//index%20  Guarantee colorMap The parameters inside are colormap[0-19] Between , To facilitate the selection of members in enumeration data types 

Code

#pragma once

#include <opencv2\highgui.hpp>

#include <opencv2\imgproc.hpp>

using namespace cv;
// Defining classes 
class QuickDemo{
    
public:
	void colorSpace_Demo(Mat &image);// Color space conversion function 2021-12-24
	void mat_creation_demo(Mat &image);//Mat Object and creation 2021-12-27
	void pixel_vist_Demo(Mat &image);// Read and write operation of image pixels 2022-1-3
	void operators_demo(Mat &image);// Arithmetic operation of image pixels 2022-1-4
	void tracking_bar_demo(Mat &image);// Scroll bar operation demonstration 2022-1-7
	void key_demo(Mat &image);// Keyboard response operation 2022-1-12
	void color_style_demo(Mat &image);//OpenCV With color table operation 2022-1-12
};

QuickDemo.cpp

#include <opencv2\highgui.hpp>

#include <opencv2\imgproc.hpp>

#include<quickopencv.h>

#include <iostream>
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  Guarantee colorMap The parameters inside are 0-18 Between 
		index++;
		imshow(" Color style ", dst);
	}
}

OpencvTest.cpp

#include <iostream>
#include <opencv2\highgui.hpp>
#include <opencv2\imgproc.hpp>
#include<quickopencv.h>

using namespace cv;
using namespace std;


int main()
{
    
	Mat scr = imread("...\\image\\1.jpg");// Open a picture 
	if (!scr.data == 1)// Sentenced to empty 
		return -1;
	namedWindow(" window 1", WINDOW_NORMAL);// establish  WINDOW_FREERATIO window 
	imshow(" window 1",scr);// Show in the created window 
	QuickDemo qd;
	qd.color_style_demo(scr);
	waitKey(0);
	return 0;
}

According to the effect

20 Color intervals 1 Seconds in sequence , Press ESC Key exit program

原网站

版权声明
本文为[Chasing foot dream]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202160534199611.html