图像数字化
- 头文件:opencv2/highgui.hpp
加载图片
imread(string filename, int flags=1)
- filename : 文件名(含路径)
- flags : 色彩参数
- IMREAD_COLOR : 彩色
- IMREAD_GRAYSCALE : 灰度
- IMREAD_ANYCOLOR : 任意
imshow(string winname, Mat mat)
- winname : 显示窗口名
- mat : 要显示的矩阵
#include<iostream>
#include<opencv2/core.hpp>
#include<opencv2/highgui.hpp>
using namespace cv;
using namespace std;
int main()
{
Mat img = imread("./People/3.jpg", 1);
cout << "img channels:" << img.channels() << endl;
cout << "img rows is :" << img.rows << endl;
cout << "img cols is :" << img.cols << endl;
imshow("test", img);
waitKey(0);
return 0;
}
RGB通道分离
#include<iostream>
#include<opencv2/core.hpp>
#include<opencv2/highgui.hpp>
using namespace cv;
using namespace std;
int main()
{
Mat img = imread("./People/wave.jpg", 1);
cout << "img channels:" << img.channels() << endl;
cout << "img rows is :" << img.rows << endl;
cout << "img cols is :" << img.cols << endl;
cout << "分离RGB" << endl;
vector<Mat> Channels;
split(img, Channels);
cout << "B channel" << endl;
cout << Channels[0] << endl;
cout << "G channel" << endl;
cout << Channels[1] << endl;
cout << "R channel" << endl;
cout << Channels[2] << endl;
return 0;
}
RGB合成灰度
g r a y = [ 0.114 0.587 0.229 ] [ B G R ] gray = \left[\begin{matrix}0.114 & 0.587 & 0.229\end{matrix}\right]\left[\begin{matrix}B \\ G \\ R\end{matrix}\right] gray=[0.1140.5870.229]⎣⎡BGR⎦⎤
#include<iostream>
#include<opencv2/core.hpp>
#include<opencv2/highgui.hpp>
using namespace cv;
using namespace std;
typedef Vec<float, 3> vec3f;
int main()
{
Mat img = imread("./People/wave.jpg", 1);
cout << "img channels:" << img.channels() << endl;
cout << "img rows is :" << img.rows << endl;
cout << "img cols is :" << img.cols << endl;
cout << "RGB->GRAY" << endl;
cout << "gray = 0.114*B+0.587*G+0.299*R" << endl;
vector<Mat> Channels;
Mat B, G, R;
Mat M1, M2, M3;
Mat gray;
split(img, Channels);
B = Channels[0];
G = Channels[1];
R = Channels[2];
M1 = B * 0.114;
M2 = G * 0.587;
M3 = R * 0.299;
cout << "M1 matrix:" << endl;
//cout << M1 << endl;
cout << "M2 matrix:" << endl;
//cout << M2 << endl;
cout << "M3 matrix:" << endl;
//cout << M3 << endl;
gray = M1 + M2 + M3;
cout << gray << endl;
imshow("gray", gray);
imwrite("./People/gray_wave.jpg", gray);
return 0;
}