一、概述:

人类能够观察到的光的波长范围是有限的,并且人类视觉有一个特点,只能分辨出二十几种灰度,也就是说即使采集到的灰度图像分辨率超级高,有上百个灰度级,但是很遗憾,人们只能看出二十几个,也就是说信息损失了五十倍。但人类视觉对彩色的分辨能力相当强,能够分辨出几千种色度,所以在实际应用中,可以将灰度图转变成彩虹图或者伪彩图等根据需求的彩色图。

二、彩虹图:

主要思路:把灰度图对应的0~255的数值分别转成彩虹色:红、橙、黄、绿、青、蓝,这里没有使用紫色,是因为紫色的效果并不好。

//彩虹图的颜色分配取一下值
// R G B gray //---------------------------------- // 红 255, 0, 0 255 // 橙 255, 127, 0 204 // 黄 255, 255, 0 153 // 绿 0, 255, 0 102 // 青 0, 255, 255 51 // 蓝 0, 0, 255 0

代码:

Mat gray2rainbow(const Mat& scaledGray)
{
Mat outputRainbow(scaledGray.size(), CV_8UC3);
unsigned char grayValue;
for (int y = ; y < scaledGray.rows; y++)
for (int x = ; x < scaledGray.cols; x++)
{
grayValue = scaledGray.at<uchar>(y, x);
Vec3b& pixel = outputRainbow.at<Vec3b>(y, x);
if (grayValue <= )
{
pixel[] = ;
pixel[] = grayValue * ;
pixel[] = ;
}
else if (grayValue <= )
{
grayValue -= ;
pixel[] = - grayValue * ;
pixel[] = ;
pixel[] = ;
}
else if (grayValue <= )
{
grayValue -= ;
pixel[] = ;
pixel[] = ;
pixel[] = grayValue * ;
}
else if (grayValue <= )
{
grayValue -= ;
pixel[] = ;
pixel[] = - static_cast<unsigned char>(grayValue * 128.0 / + 0.5);
pixel[] = ;
}
else if (grayValue <= )
{
grayValue -= ;
pixel[] = ;
pixel[] = - static_cast<unsigned char>(grayValue * 127.0 / + 0.5);
pixel[] = ;
}
} return outputRainbow;
}

三、伪彩图

伪彩色图片的处理,就是用RGB三色交叉,不同的彩色表示不同的灰度值,将一幅灰度图转变成为一幅彩色图片。

Mat gray2pseudocolor(const Mat& scaledGray)
{
Mat outputPseudocolor(scaledGray.size(), CV_8UC3);
unsigned char grayValue;
for (int y = ; y < scaledGray.rows; y++)
for (int x = ; x < scaledGray.cols; x++)
{
grayValue = scaledGray.at<uchar>(y, x);
Vec3b& pixel = outputPseudocolor.at<Vec3b>(y, x);
pixel[] = abs( - grayValue);
pixel[] = abs( - grayValue);
pixel[] = abs( - grayValue);
} return outputPseudocolor;
}

四、铜色图

将R去0,G、B两色交叉。

Mat gray2CopperColor(const Mat& scaledGray)
{
Mat outputCopperColor(scaledGray.size(), CV_8UC3);
unsigned char grayValue;
for (int y = ; y < scaledGray.rows; y++)
for (int x = ; x < scaledGray.cols; x++)
{
grayValue = scaledGray.at<uchar>(y, x);
Vec3b& pixel = outputCopperColor.at<Vec3b>(y, x);
pixel[] = abs();
pixel[] = abs(grayValue);
pixel[] = abs(grayValue);
} return outputCopperColor;
}

五、灰度反转

将图像进行灰度反转处理,即将灰度值为x的像素点转变为255-x。

利用Opencv中bitwise_not()函数可实现,没必要一个像素点一个像素点处理。

Mat gray2disColor(const Mat& scaledGray)
{ Mat disColor(scaledGray.size(), CV_8UC3);
bitwise_not(disColor, scaledGray);
return disColor;
}

六、灰度图

将一幅彩色图片转换为灰度图

Mat scaleGray(const Mat& inputGray)
{
Mat outputGray(inputGray.size(), CV_8U);
unsigned char grayValue, maxValue = ;
for (int y = ; y < inputGray.rows; y++)
for (int x = ; x < inputGray.cols; x ++)
{
grayValue = inputGray.at<uchar>(y, x);
maxValue = max(maxValue, grayValue);
} float scale = 255.0 / maxValue;
for (int y = ; y < inputGray.rows; y++)
for (int x = ; x < inputGray.cols; x ++)
{
outputGray.at<uchar>(y, x) = static_cast<unsigned char>(inputGray.at<uchar>(y, x) * scale + 0.5);
} return outputGray;
}

七、完整代码

Opencv学习笔记5:Opencv处理彩虹图、铜色图、灰度反转图的更多相关文章

  1. [OpenCV学习笔记1][OpenCV基本数据类型]

    CvPoint基于二维整形坐标轴的点typedef struct CvPoint{int x; /* X 坐标, 通常以 0 为基点 */int y; /* y 坐标,通常以 0 为基点 */}CvP ...

  2. OpenCV学习笔记(12)——OpenCV中的轮廓

    什么是轮廓 找轮廓.绘制轮廓等 1.什么是轮廓 轮廓可看做将连续的点(连着边界)连在一起的曲线,具有相同的颜色和灰度.轮廓在形态分析和物体的检测和识别中很有用. 为了更加准确,要使用二值化图像.在寻找 ...

  3. 第十七周 - OpenCV 学习笔记 S1 - OpenCV 基本函数

    Imread()函数: 基本功能:读取图像到OpenCv中. 1.函数原型: Mat imwrite(const strings& filename, int flag = 1); 第一个参数 ...

  4. opencv学习笔记(七)SVM+HOG

    opencv学习笔记(七)SVM+HOG 一.简介 方向梯度直方图(Histogram of Oriented Gradient,HOG)特征是一种在计算机视觉和图像处理中用来进行物体检测的特征描述子 ...

  5. opencv学习笔记(四)投影

    opencv学习笔记(四)投影 任选了一张图片用于测试,图片如下所示: #include <cv.h> #include <highgui.h> using namespace ...

  6. opencv学习笔记(一)IplImage, CvMat, Mat 的关系

    opencv学习笔记(一)IplImage, CvMat, Mat 的关系 opencv中常见的与图像操作有关的数据容器有Mat,cvMat和IplImage,这三种类型都可以代表和显示图像,但是,M ...

  7. OpenCV 学习笔记(模板匹配)

    OpenCV 学习笔记(模板匹配) 模板匹配是在一幅图像中寻找一个特定目标的方法之一.这种方法的原理非常简单,遍历图像中的每一个可能的位置,比较各处与模板是否"相似",当相似度足够 ...

  8. OpenCV 学习笔记 07 目标检测与识别

    目标检测与识别是计算机视觉中最常见的挑战之一.属于高级主题. 本章节将扩展目标检测的概念,首先探讨人脸识别技术,然后将该技术应用到显示生活中的各种目标检测. 1 目标检测与识别技术 为了与OpenCV ...

  9. OpenCV 学习笔记 04 深度估计与分割——GrabCut算法与分水岭算法

    1 使用普通摄像头进行深度估计 1.1 深度估计原理 这里会用到几何学中的极几何(Epipolar Geometry),它属于立体视觉(stereo vision)几何学,立体视觉是计算机视觉的一个分 ...

  10. OpenCV 学习笔记03 boundingRect、minAreaRect、minEnclosingCircle、boxPoints、int0、circle、rectangle函数的用法

    函数中的代码是部分代码,详细代码在最后 1 cv2.boundingRect 作用:矩形边框(boundingRect),用于计算图像一系列点的外部矩形边界. cv2.boundingRect(arr ...

随机推荐

  1. supervisor简洁用例

    supervisor是什么 superviosr是一个Linux/Unix系统上进程监控和管理的工具,它由python编写,可以用pip安装.supervisor能将一个普通的命令行进程变为后台dae ...

  2. Python练习-从小就背不下来的99乘法表

    心血来潮,灵机一动,反正就是无聊的做了一个很简单的小玩意: for i in range(1,10):#让i 1-9 循环9次 print("\n")#每循环一次进行一次换行 fo ...

  3. [JL]最后的晚餐 动态规划(DP) codevs5318

    [JL]最后的晚餐 TimeLimit:1000MS  MemoryLimit:1000KB 64-bit integer IO format:%lld Problem Description [题库 ...

  4. 20155303 实验二 Java面向对象程序设计

    20155303 实验二 Java面向对象程序设计 目录 一.单元测试和TDD 任务一:实现百分制成绩转成"优.良.中.及格.不及格"五级制成绩的功能 任务二:以TDD的方式研究学 ...

  5. curator框架的使用以及实现分布式锁等应用与zkclient操作zookeeper,简化复杂原生API

    打开zookeeper集群 先体会一下原生API有多麻烦(可略过): //地址 static final String ADDR = "192.168.171.128:2181,192.16 ...

  6. Python标准库笔记(9) — functools模块

    functools 作用于函数的函数 functools 模块提供用于调整或扩展函数和其他可调用对象的工具,而无需完全重写它们. 装饰器 partial 类是 functools 模块提供的主要工具, ...

  7. 空洞卷积(dilated Convolution) 与感受野(Receptive Field)

    一.空洞卷积 空洞卷积是是为了解决基于FCN思想的语义分割中,输出图像的size要求和输入图像的size一致而需要upsample,但由于FCN中使用pooling操作来增大感受野同时降低分辨率,导致 ...

  8. 003_ElasticSearch详解与优化设计

    简介 概念 安装部署 ES安装 数据索引 索引优化 内存优化 1简介 ElasticSearch(简称ES)是一个分布式.Restful的搜索及分析服务器,设计用于分布式计算:能够达到实时搜索,稳定, ...

  9. 05 Diagnostics 诊断

    Diagnostics 诊断 Introduction 介绍 Profiling 分析 Tracing 跟踪 Debugging 调试 Runtime statistics and events 运行 ...

  10. 数据库-mysql数据连接

    一:Mysql 连接的使用 在前几章节中,我们已经学会了如果在一张表中读取数据,这是相对简单的,但是在真正的应用中经常需要从多个数据表中读取数据. 本章节我们将向大家介绍如何使用 MySQL 的 JO ...