OpenCV 之 数字图像
1 数字图像
数字图像可看作一个 数值矩阵, 其中的每个 元素代表一个 像素点,如下图所示:

2 存储方式
M 行 N 列图像的存储位数: b = M * N * k ( L=2k, l ∈ [0, L-1], l 为灰度值 )
2.1 灰度图
OpenCV 中,灰度图的存储形式如下:

2.2 RGB图像
OpenCV中,RGB图像以 BGR 的顺序存储,如下:

2.3 Mat 类
Mat = 矩阵头 + 指针(指向包含像素值的矩阵),其有如下特点:
1) 矩阵头包含矩阵大小,存储函数,存储地址等信息
2) 使用 OpenCV 的接口函数,无需考虑内存管理
3) 执行赋值算子 “=” 或 拷贝构造函数时,仅仅复制矩阵头 (matrix header)
4) 图像矩阵的复制,应该使用 clone() 和 copyTo() 函数
#include <iostream>
#include "opencv2/core/core.hpp" using namespace std;
using namespace cv; int main(int,char**)
{
// 1) 构造函数
Mat M(,, CV_8UC3, Scalar(,,));
cout << "M = " << endl << " " << M << endl << endl; // 2) create 函数
M.create(,, CV_8UC());
cout << "M = "<< endl << " " << M << endl << endl; // 3) 多维矩阵
int sz[] = {,,};
Mat L(,sz, CV_8UC(), Scalar::all()); // 4) MATLAB 风格 eye, ones or zero
Mat E = Mat::eye(, , CV_64F);
cout << "E = " << endl << " " << E << endl << endl;
Mat O = Mat::ones(, , CV_32F);
cout << "O = " << endl << " " << O << endl << endl;
Mat Z = Mat::zeros(, , CV_8UC1);
cout << "Z = " << endl << " " << Z << endl << endl; // 5) 3x3 双精度
Mat C = (Mat_<double>(,) << , -, , -, , -, , -, );
cout << "C = " << endl << " " << C << endl << endl; //! [clone]
Mat RowClone = C.row().clone();
cout << "RowClone = " << endl << " " << RowClone << endl << endl; // 6) 随机值填充矩阵
Mat R = Mat(, , CV_8UC3);
randu(R, Scalar::all(), Scalar::all()); // 演示输出格式
cout << "R (default) = " << endl << R << endl << endl;
cout << "R (python) = " << endl << format(R, Formatter::FMT_PYTHON) << endl << endl;
cout << "R (numpy) = " << endl << format(R, Formatter::FMT_NUMPY ) << endl << endl;
cout << "R (csv) = " << endl << format(R, Formatter::FMT_CSV ) << endl << endl;
cout << "R (c) = " << endl << format(R, Formatter::FMT_C ) << endl << endl; vector<float> v;
v.push_back((float)CV_PI);
v.push_back();
v.push_back(3.01f);
cout << "Vector of floats via Mat = " << Mat(v) << endl << endl;
}
3 邻域
D 定义为像素点 p(x, y) 和 q(s, t)的距离
3.1 四邻域 - 十字格
D4 = |x - s| + |y - t| = 1
$\quad \begin{bmatrix} 2 & 1 & 2 \\ 1 & 0 & 1 \\ 2 & 1 & 2 \end{bmatrix} $
3.2 八邻域 - 田字格
D8 = max(|x - s|, |y - t|) = 1
$\quad \begin{bmatrix} 1 & 1 & 1 \\ 1 & 0 & 1 \\ 1 & 1 & 1 \end{bmatrix} $
4 代码示例
4.1 读图和显示
OpenCV 中,图像读取和显示的函数名,和 Matlab 中的一致, 即 imread 和 imshow
#include "opencv2/highgui/highgui.hpp"
using namespace cv; int main(int argc, char ** argv) // int main()
{
Mat img = imread(argv[] - ); // Mat img = imread("E:/.../feng.jpg");
if(img.empty())
return -;
namedWindow("Example", CV_WINDOW_AUTOSIZE);
imshow("Example", img);
waitKey();
}
4.2 遍历像素
4.2.1 efficient method
Mat& ScanImageAndReduceC(Mat& I, const uchar* const table)
{
// accept only char type matrices
CV_Assert(I.depth() == CV_8U);
int channels = I.channels();
int nRows = I.rows;
int nCols = I.cols * channels;
if (I.isContinuous())
{
nCols *= nRows;
nRows = ;
}
int i,j;
uchar* p;
for( i = ; i < nRows; ++i)
{
p = I.ptr<uchar>(i);
for ( j = ; j < nCols; ++j)
{
p[j] = table[p[j]];
}
}
return I;
}
4.2.2 iterator method
Mat& ScanImageAndReduceIterator(Mat& I, const uchar* const table)
{
// accept only char type matrices
CV_Assert(I.depth() == CV_8U);
const int channels = I.channels();
switch(channels)
{
case :
{
MatIterator_<uchar> it, end;
for( it = I.begin<uchar>(), end = I.end<uchar>(); it != end; ++it)
*it = table[*it];
break;
}
case :
{
MatIterator_<Vec3b> it, end;
for( it = I.begin<Vec3b>(), end = I.end<Vec3b>(); it != end; ++it)
{
(*it)[] = table[(*it)[]];
(*it)[] = table[(*it)[]];
(*it)[] = table[(*it)[]];
}
}
}
return I;
}
OpenCV 之 数字图像的更多相关文章
- [OpenCV] Image Processing - Grayscale Transform
"每个像素的输出值只取决于其输入值" 重难点:Histogram equalization 参考:笑得很甜 http://blog.csdn.net/xiaowei_cqu/art ...
- [OpenCV] Image Processing - Grayscale Transform & Histogram
颜色直方图 首先,先介绍一些Hist的基本使用. Ref:[OpenCV]数字图像灰度直方图 官方文档:https://docs.opencv.org/trunk/d8/dbc/tutorial_hi ...
- opencv图像坐标
原图: 尺寸:240 × 150 灰度化: 1. 程序中输出像素点的灰度值: 2. 用工具取得的灰度值: 按照如下的坐标(图像处理坐标系) 得到的灰度值: (35,82) (82,35) 换算后分别是 ...
- OpenCV 之 边缘检测
上一篇 <OpenCV 之 图像平滑> 中,提到的图像平滑,从信号处理的角度来看,实际上是一种“低通滤波器”. 本篇中,数字图像的边缘,因为通常都是像素值变化剧烈的区域 (“高频”),故可 ...
- sobel算子原理及opencv源码实现
sobel算子原理及opencv源码实现 简要描述 sobel算子主要用于获得数字图像的一阶梯度,常见的应用和物理意义是边缘检测. 原理 算子使用两个33的矩阵(图1)算子使用两个33的矩阵(图1)去 ...
- opencv 简单模糊和高斯模糊 cvSmooth
cv::Mat 是C++版OpenCV的新结构. cvSmooth() 是老版 C API. 没有把C接口与C + + 结合. 建议你们也可以花一些时间看一下介绍. 同样,你如果查看opencv/mo ...
- OpenCV成长之路(4):图像直方图
一.图像直方图的概念 图像直方图是反映一个图像像素分布的统计表,其实横坐标代表了图像像素的种类,可以是灰度的,也可以是彩色的.纵坐标代表了每一种颜色值在图像中的像素总数或者占所有像素个数的百分比. 图 ...
- OpenCV成长之路(3):模仿PhotoShop中魔术棒工具
本文的主题实际上是图像的颜色空间的转换,借助一个颜色选取程序来说明OpenCV中颜色转换函数的用法以及一些注意事项. 一.几种常见的颜色空间: RGB颜色空间:RGB采用加法混色法,因为它是描述各种“ ...
- [OpenCV] Feature Extraction
特征检测 特征描述 特征匹配 特征跟踪 “不读白不读,读了还想读” 的一本基础书 低层次特征提取 阈值方法 1. 边缘检测 一阶检测算子 二阶检测算子 相位一致性(频域) 2. 角点检测(局部特征提取 ...
随机推荐
- Flask and uWSGI - unable to load app 0 (mountpoint='') (callable not found or import error)
Here is my directory structure: -/path/to/folder/run.py -|app -|__init__.py -|views.py -|templates - ...
- Verilog学习笔记设计和验证篇(二)...............同步有限状态机
上图表示的就是数字电路设计中常用的时钟同步状态机的结构.其中共有四个部分产生下一状态的组合逻辑F.状态寄存器组.输出组合逻辑G.流水线输出寄存器组.如果状态寄存器组由n个寄存器组成,就可以记忆2^n个 ...
- Verilog学习笔记基本语法篇(十)········ 常用系统函数
$display 和 $write 任务 格式: $display (p1,p2,...,pn); $write (p1,p2,..,pn); 这两个函数和系统的任务作用是用来输出信息,即将参数p2到 ...
- android 类ios actionsheet效果
1.http://blog.csdn.net/zhaoxy_thu/article/details/17733389 2. https://github.com/ojhariddhish/action ...
- 记录一个调了半天的问题:java.lang.SecurityException: Permission denied (missing INTERNET permission?)
Move the <uses-permission> elements outside of <application>. They need to be immediate ...
- 小波说雨燕 第三季 构建 swift UI 之 UI组件集-视图集(五)Image View视图 学习笔记
留下两个问题:1.后面涉及到的异常不知道原因.2.动态图片到了程序里面就不动了. 然后: 上面是有问题的,下面是没有问题的了. 代码(另外简单写的代码,纠正了那个错误): imp ...
- Windows 编 程中的字符串
(1)在win32编程中,如何使用string类型 #include <string> using namespace std; LPTSTR lpCmdLine = L" ...
- linux下安装mysql手记
安装mysql 下载mysql-standard-4.1.8-pc-linux-i686.tar.gz文件到目录/usr/local/下 # groupadd mysql //添加mysql用户组 ...
- 整理了一些常用的jQuery动画事件
部分jQuery常用的动画函数,整理了一下,在做交互式页面的时候挺有用的 .css('a','12px');.css({ a:'12px', b:'#fff'}); .show();.hide() ...
- [Weblogic]如何清理缓存
背景:在开发调试或测试时,很多时候重新更新部署服务后会发现某些更新并没有体现到,还是之前的情况,也或者会出现其他错误问题,这个时候就要考虑到可能是weblogic缓存未清理引起. 清理缓存步骤如下: ...