一、概述:

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

二、彩虹图:

主要思路:把灰度图对应的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. windebug分析高cpu问题

    分析高CPU的关键是找到哪个线程是持续运行,占用CPU时间. 可以隔上两分钟连续抓两个dump文件,使用 !runaway 查看线程运行的时间 通过对比两个dump文件的线程时间,看看哪个线程运行的时 ...

  2. 存储过程简单Demo

    --创建存储过程 delimiter // create procedure p1() begin end // --调用存储过程 call p1(); --删除存储过程 drop procedure ...

  3. 渗透测试===kali linux的安装

    方法一: kali linux 安装在本地的vitural box 或者 wm ware中 方法二: 安装在移动硬盘或者储存卡中,插到电脑就能用

  4. ### mysql系统结构_3_Mysql_Learning_Notes

    mysql系统结构_3_Mysql_Learning_Notes 存储层,内存结构 全局(buferpool) 只分配一次 全局共享 连接/会话(session) 针对每个会话/线程分配 按需动态分配 ...

  5. SQL 存储过程分页

    CREATE PROC p_Team_GetTemaList @pageindex INT , @pagesize INT , @keywords VARCHAR(200) , --模糊查询 名称 标 ...

  6. html 列表标签

    1.有序列表 <ol> <li>你好</li> <li>你好</li> <li>你好</li> </ol> ...

  7. Visual Studio 2017 for Mac

    Visual Studio 2017 for Mac Last Update: 2017/6/16 我们非常荣幸地宣布 Visual Studio 2017 for Mac 现已推出. Visual ...

  8. 【前端vue开发】vue单页应用添加百度统计

    前言 申请百度统计后,会得到一段JS代码,需要插入到每个网页中去,在Vue.js项目首先想到的可能就是,把统计代码插入到index.html入口文件中,这样就全局插入,每个页面就都有了;这样做就涉及到 ...

  9. 洛谷P2149 Elaxia的路线

    传送门啦 分析: 我最开始想的是跑两遍最短路,然后记录一下最短路走了哪些边(如果有两条最短路就选经过边多的),打上标记.两边之后找两次都标记的边有多少就行了. 但...我并没有实现出来. 最后让我们看 ...

  10. linux下快速安装emacs方法

    背景 在公司工作的时候经常需要在很多服务器之间切换,而公司的服务器上一般都没emacs,因此总结一下快速安装emacs的方法. 最简单的是直接使用yum安装,但是有两个问题,一个是有的生产服务器直接没 ...