一、概述:

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

二、彩虹图:

主要思路:把灰度图对应的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. 31、LinkedHashSet简介和练习

    LinkedHashSet简介 通过LinkedHashSet的名字就可以看出,他的底层使用了链表的数据结构,因此LinkedHashSet的特点是读取元素的顺序跟存入元素的顺序是一致的,并且元素不能 ...

  2. 【多视图几何】TUM 课程 第2章 刚体运动

    课程的 YouTube 地址为:https://www.youtube.com/playlist?list=PLTBdjV_4f-EJn6udZ34tht9EVIW7lbeo4 .视频评论区可以找到课 ...

  3. 技术分享:如何在PowerShell脚本中嵌入EXE文件

    技术分享:如何在PowerShell脚本中嵌入EXE文件 我在尝试解决一个问题,即在客户端攻击中只使用纯 PowerShell 脚本作为攻击负荷.使用 PowerShell 运行恶意代码具有很多优点, ...

  4. 【算法】Base64编码

    1.说明 Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法. 2.编码 ASCII码 -> 十六进制码 -> ...

  5. 从此编写 Bash 脚本不再难【转】

    从此编写 Bash 脚本不再难 原创 Linux技术 2017-05-02 14:30 在这篇文章中,我们会介绍如何通过使用 bash-support vim 插件将 Vim 编辑器安装和配置 为一个 ...

  6. win7 64位mysql安装及navicat 解压版

    教程:http://jingyan.baidu.com/article/f3ad7d0ffc061a09c3345bf0.html Mysql修改设置root密码的命令及方法:http://jingy ...

  7. js如何查看元素类型

    <script type="text/javascript"> //定义变量temp var temp = Object.prototype.toString.appl ...

  8. mysqli链接数据库示例代码

    $mysqli = new mysqli("localhost", "数据库用户名", "数据库密码", "数据库名称" ...

  9. ipad webapp禁止长按选择

    1.禁止长按屏幕弹出对话框并选中文字 /*禁止长按选择文字事件*/ * { -webkit-touch-callout: none; -webkit-user-select: none; -khtml ...

  10. excl筛选求和

    Excel中的筛选,是一个很常用的功能.但不知道是有意还是疏忽,Excel没有直接提供在筛选后的一些统计功能,例如求和.平均值等.而由于筛选的主要功能之一就是可以方便快捷的进行变换,所普通的以直接在数 ...