数字图像处理的本质是操作矩阵
一、OpenCV 中将图像数字化的API
1、imread
(1)头文件
#include <opencv2\highgui\highgui.hpp>
注:imread(), namedWindow(), imshow() 和 waitKey() 函数都声明在这个头文件中
(2)函数原型
Mat cv::imread (const String & filename, int flags = IMREAD_COLOR )
(3)介绍
Loads an image from a file. If the image cannot be read , the function returns an empty matrix ( Mat::data==NULL ).
(4)参数
- 参数一:给图像路径/文件名
- 参数二:给flags,具体看Imread flags.,可以通过此参数设置矩阵存储的是原图、灰度图、图像的几分之一。
(5)理解
函数将第一个形参对应的图像文件,以flags形式转化为矩阵,具体见示例
(6)示例
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main(int argc, char **argv)
{
//input image array
String imgPath = "./1.JPG";
Mat img = imread(imgPath, IMREAD_UNCHANGED);
if (img.empty())
return -1;
//show image
imshow("", img);
waitKey(0);
return 0;
}
注:Mat数据结构,它在”opencv2/core/core.hpp”中声明的,因为在”opencv2/highgui/highgui.hpp”头文件中已经包含了core.hpp头文件,所以程序不用再次包含
2、split
(1)头文件
(2)函数原型
void split(const Mat& src, Mat *mvBegin);
void split(InputArray m, OutputArrayOfArrays mv);
(3)介绍
Divides a multi-channel array into several single-channel arrays.
(4)参数
src:input multi-channel array.
mvbegin:output array; the number of arrays must match src.channels(); the arrays themselves are reallocated, if needed.
第一个参数为要进行分离的图像矩阵
第二个参数可以是Mat数组的首地址,或者一个vector对象第一个参数为要进行分离的图像矩阵,第二个参数可以是Mat数组的首地址,或者一个vector对象
(5)理解
- 如果split函数输入的图像为HSV的,则分离后,channels[0]对应H,channels[1]对应S,channels[2]对应V
- 如果split函数输入的图像为RGB的,则分离后,channels[0]对应B,channels[1]对应G,channels[2]对应R
(6)示例
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main(int argc, char **argv)
{
//input image array
String imgPath = "./2.JPG";
Mat img = imread(imgPath, IMREAD_UNCHANGED);
if (img.empty())
return -1;
//show image
imshow("", img);
//split channel
vector<Mat> planes;
split(img, planes);
//show channel B
imshow("B", planes[0]);
//show channel G
imshow("G", planes[1]);
//show channel R
imshow("R", planes[2]);
waitKey(0);
return 0;
}
二、矩阵
1、Mat
(1)头文件
#include "mat.hpp"
(2)介绍
2、矩阵运算
(1)加、减、点乘、点除、乘法
(2)其他
参考
1、《OpenCV算法精讲》 张平 编著
2、docs.opencv.org-4.0.0
3、opencv读取图像数据的方式总结
4、OpenCV中的split函数