OpenCV : 图像数字化

图像数字化

  • 头文件: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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值