- 操作系统:ubuntu22.04
- OpenCV版本:OpenCV4.9
- IDE:Visual Studio Code
- 编程语言:C++11
算法描述
OpenCV 中应用类似于 GNU Octave 或 MATLAB 中的颜色映射,虽然 OpenCV 中的颜色映射类型与 GNU Octave 或 MATLAB 中的颜色映射类型名称不完全相同,但它们在功能上是类似的。
函数原型
void cv::applyColorMap
(
InputArray src,
OutputArray dst,
int colormap
)
参数
- 参数src 源图像,灰度或彩色,类型为 CV_8UC1 或 CV_8UC3。
- 参数dst 结果是带有颜色映射的源图像。注意:Mat::create 会在 dst 上被调用。
- 参数colormap T要应用的颜色映射,参见颜色映射类型(ColormapTypes)。
代码示例
#include <iostream>
#include <opencv2/opencv.hpp>
int main( int argc, char** argv )
{
// 加载灰度图像
cv::Mat grayImage = cv::imread( "/media/dingxin/data/study/OpenCV/sources/images/qiu.jpg", cv::IMREAD_GRAYSCALE );
if ( grayImage.empty() )
{
std::cerr << "Error: Image cannot be loaded!" << std::endl;
return -1;
}
// 创建一个空的 Mat 对象来保存彩色图像
cv::Mat colorImage;
cv::imshow( "Original Gray Image", grayImage );
cv::applyColorMap( grayImage, colorImage, cv::COLORMAP_JET );
cv::imshow( "Colored Image COLORMAP_JET", colorImage );
cv::applyColorMap( grayImage, colorImage, cv::COLORMAP_BONE );
cv::imshow( "Colored Image COLORMAP_BONE", colorImage );
cv::applyColorMap( grayImage, colorImage, cv::COLORMAP_HOT );
cv::imshow( "Colored Image COLORMAP_HOT", colorImage );
cv::applyColorMap( grayImage, colorImage, cv::COLORMAP_COOL );
cv::imshow( "Colored Image COLORMAP_COOL", colorImage );
cv::applyColorMap( grayImage, colorImage, cv::COLORMAP_SPRING );
cv::imshow( "Colored Image COLORMAP_SPRING", colorImage );
cv::applyColorMap( grayImage, colorImage, cv::COLORMAP_SUMMER );
cv::imshow( "Colored Image COLORMAP_SUMMER", colorImage );
cv::applyColorMap( grayImage, colorImage, cv::COLORMAP_AUTUMN );
cv::imshow( "Colored Image COLORMAP_AUTUMN", colorImage );
cv::applyColorMap( grayImage, colorImage, cv::COLORMAP_WINTER );
cv::imshow( "Colored Image COLORMAP_WINTER", colorImage );
cv::waitKey( 0 );
return 0;
}