Opencv学习笔记5:Opencv处理彩虹图、铜色图、灰度反转图
一、概述:
人类能够观察到的光的波长范围是有限的,并且人类视觉有一个特点,只能分辨出二十几种灰度,也就是说即使采集到的灰度图像分辨率超级高,有上百个灰度级,但是很遗憾,人们只能看出二十几个,也就是说信息损失了五十倍。但人类视觉对彩色的分辨能力相当强,能够分辨出几千种色度,所以在实际应用中,可以将灰度图转变成彩虹图或者伪彩图等根据需求的彩色图。
二、彩虹图:
主要思路:把灰度图对应的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处理彩虹图、铜色图、灰度反转图的更多相关文章
- [OpenCV学习笔记1][OpenCV基本数据类型]
CvPoint基于二维整形坐标轴的点typedef struct CvPoint{int x; /* X 坐标, 通常以 0 为基点 */int y; /* y 坐标,通常以 0 为基点 */}CvP ...
- OpenCV学习笔记(12)——OpenCV中的轮廓
什么是轮廓 找轮廓.绘制轮廓等 1.什么是轮廓 轮廓可看做将连续的点(连着边界)连在一起的曲线,具有相同的颜色和灰度.轮廓在形态分析和物体的检测和识别中很有用. 为了更加准确,要使用二值化图像.在寻找 ...
- 第十七周 - OpenCV 学习笔记 S1 - OpenCV 基本函数
Imread()函数: 基本功能:读取图像到OpenCv中. 1.函数原型: Mat imwrite(const strings& filename, int flag = 1); 第一个参数 ...
- opencv学习笔记(七)SVM+HOG
opencv学习笔记(七)SVM+HOG 一.简介 方向梯度直方图(Histogram of Oriented Gradient,HOG)特征是一种在计算机视觉和图像处理中用来进行物体检测的特征描述子 ...
- opencv学习笔记(四)投影
opencv学习笔记(四)投影 任选了一张图片用于测试,图片如下所示: #include <cv.h> #include <highgui.h> using namespace ...
- opencv学习笔记(一)IplImage, CvMat, Mat 的关系
opencv学习笔记(一)IplImage, CvMat, Mat 的关系 opencv中常见的与图像操作有关的数据容器有Mat,cvMat和IplImage,这三种类型都可以代表和显示图像,但是,M ...
- OpenCV 学习笔记(模板匹配)
OpenCV 学习笔记(模板匹配) 模板匹配是在一幅图像中寻找一个特定目标的方法之一.这种方法的原理非常简单,遍历图像中的每一个可能的位置,比较各处与模板是否"相似",当相似度足够 ...
- OpenCV 学习笔记 07 目标检测与识别
目标检测与识别是计算机视觉中最常见的挑战之一.属于高级主题. 本章节将扩展目标检测的概念,首先探讨人脸识别技术,然后将该技术应用到显示生活中的各种目标检测. 1 目标检测与识别技术 为了与OpenCV ...
- OpenCV 学习笔记 04 深度估计与分割——GrabCut算法与分水岭算法
1 使用普通摄像头进行深度估计 1.1 深度估计原理 这里会用到几何学中的极几何(Epipolar Geometry),它属于立体视觉(stereo vision)几何学,立体视觉是计算机视觉的一个分 ...
- OpenCV 学习笔记03 boundingRect、minAreaRect、minEnclosingCircle、boxPoints、int0、circle、rectangle函数的用法
函数中的代码是部分代码,详细代码在最后 1 cv2.boundingRect 作用:矩形边框(boundingRect),用于计算图像一系列点的外部矩形边界. cv2.boundingRect(arr ...
随机推荐
- 网络流建图(典型)(EK)
题目链接:https://cn.vjudge.net/contest/68128#problem/B 具体思路: 按照 源点 - > 食物 - > 牛1 - > 牛2 - > ...
- python——脚本和print
脚本和print 1.脚本文件 <Python 基础教程>(第二版)中 P118页,原操作为下: 1 _metaclass_ = type 2 3 class Person: 4 def ...
- 20165230 2017-2018-2 《Java程序设计》第9周学习总结
20165230 2017-2018-2 <Java程序设计>第9周学习总结 教材学习内容总结 第十二章 java网络编程 学习了用于网络编程的类,了解URL.Socket.InetAdd ...
- 【杂谈】需要mark的一些东西
https://riteme.github.io/blog/2017-10-28/oi-coding-guidelines.html https://www.luogu.org/blog/34238/ ...
- PATH变量重复
命令: export PATH=$(echo $PATH | tr : "\n"| sort | uniq | tr "\n" :) Code: awk -F: ...
- 搭建本地git服务器
最近因为项目需求,需要实现一个原型系统,加上后期项目需要多人协作,考虑采用了git做版本控制. 这里主要简要描述下git服务器和客户端的搭建和配置. 1.git服务器 (1)安装git sudo ap ...
- AngularJS中ng-class使用方法
转自:https://blog.csdn.net/jumtre/article/details/50802136 其他博文ng-class使用方法:https://blog.csdn.net/sina ...
- pip install 升级时候 出现报asciii码错误的问题。
原因是pip安装python包会加载我的用户目录,我的用户目录恰好是中文的,ascii不能编码.解决办法是: python目录 Python27\Lib\site-packages 建一个文件site ...
- rsync + inotify 实时同步
1. 前言 2 台 nginx 需要做集群, 静态文件和php文件都在nginx服务器本地. 有三种方案: (1)NFS (2)Rsync + inotify (3)共享存储服务器 第一种:当 nfs ...
- tensorflow中的boolean_mask
将mask中所有为true的抽取出来,放到一起,这里从n维降到1维度 tensor = [[1, 2], [3, 4], [5, 6]] import numpy as np mask=np.arra ...