OpenCV-图像处理操作(一)

将opencv安装包解压后,在项目属性-链接器中添加依赖项,引入头文件后即可在项目中调用opencv库。

#include 
#include 
#include
#include
#include
#include

using namespace std;
using namespace cv;

读取与显示图像

	Mat img = imread("123.jpg");
	namedWindow("123", WINDOW_NORMAL);
	imshow("123", img);
	waitKey(0);

效果如下:

OpenCV-图像处理操作(一)_第1张图片

像素分离(通道分离)

    Mat img_R, img_G, img_B;
	Mat img_123[3];

	split(img, img_123);//将多通道图像分离成单通道图像
	img_R = img_123[0];
	img_G = img_123[1];
	img_B = img_123[2];

	imshow("R", img_R);
	namedWindow("R", WINDOW_NORMAL);
	waitKey(0);

	imshow("G", img_G);
	namedWindow("G", WINDOW_NORMAL);
	waitKey(0);

	imshow("B", img_B);
	namedWindow("B", WINDOW_NORMAL);
	waitKey(0);

转换为r,g,b三种单通道图之后,每个像素点只有一个值来表示颜色,所以三张图都是灰色的。不严格的来说单通道图就是灰度图。

通道合并

    Mat result;
	Mat zero(img.rows, img.cols, CV_8UC1);
	img_123[0] = zero;
	img_123[1] = zero;          //R,G通道置零显示合并后B通道    
	merge(img_123, 3, result);

	imshow("result", result);
	namedWindow("result", WINDOW_NORMAL);
	waitKey(0);

OpenCV-图像处理操作(一)_第2张图片
求图像平均值

    cout << "用mean求图像平均值" << endl;
	Scalar my_mean;
	my_mean = mean(img);
	cout << "mean=" << my_mean << endl;

	cout << endl;
	cout << "用meanstdDev求图像平均值和标准差" << endl;
	Mat ymean_mat, ymean_stdDev;
	meanStdDev(img, ymean_mat, ymean_stdDev);
	cout << "图像平均值为:" << ymean_mat << endl;
	cout << "图像标准差为:" <<  ymean_stdDev<< endl;

	Mat outimg = img.clone();
	Mat_<Vec3b>::iterator it = outimg.begin< Vec3b>();
	Mat_<Vec3b>::iterator itend = outimg.end< Vec3b>();

	int sum = 0;
	int div = outimg.cols * outimg.rows* outimg.channels();
	
	
	for (; it != itend; it++)
	{
		sum += (*it)[0] + (*it)[1] + (*it)[2];
	}
	float arv = sum / div;
	cout << "迭代器遍历图像平均值为:" << endl;
	cout << arv << endl;

OpenCV-图像处理操作(一)_第3张图片

随机数填充生成图像

    //随机数填充生成图像
	RNG rng = theRNG();
	Mat matf = Mat(2592, 1944, CV_8UC4,Scalar(0));
	rng.fill(matf, RNG::UNIFORM, 0, 255);
	namedWindow("Imgfill", WINDOW_NORMAL);
	imshow("Imgfill", matf);
	waitKey(0);

完整代码:

#include 
#include 
#include
#include
#include
#include

using namespace std;
using namespace cv;


int main()
{
	//显示图像
	Mat img = imread("123.jpg");
	namedWindow("123", WINDOW_NORMAL);
	imshow("123", img);
	waitKey(0);

	//-----------------像素分离合并---------------------------

	Mat img_R, img_G, img_B;
	Mat img_123[3];

	split(img, img_123);//将多通道图像分离成单通道图像
	img_R = img_123[0];
	img_G = img_123[1];
	img_B = img_123[2];

	imshow("R", img_R);
	namedWindow("R", WINDOW_NORMAL);
	waitKey(0);

	imshow("G", img_G);
	namedWindow("G", WINDOW_NORMAL);
	waitKey(0);

	imshow("B", img_B);
	namedWindow("B", WINDOW_NORMAL);
	waitKey(0);


	Mat result;
	Mat zero(img.rows, img.cols, CV_8UC1);
	img_123[0] = zero;
	img_123[1] = zero;          //R,G通道置零显示合并后B通道    
	merge(img_123, 3, result);

	imshow("result", result);
	namedWindow("result", WINDOW_NORMAL);
	waitKey(0);

	//---------------求图像平均值--------------------------------

	cout << "用mean求图像平均值" << endl;
	Scalar my_mean;
	my_mean = mean(img);
	cout << "mean=" << my_mean << endl;

	cout << endl;
	cout << "用meanstdDev求图像平均值和标准差" << endl;
	Mat ymean_mat, ymean_stdDev;
	meanStdDev(img, ymean_mat, ymean_stdDev);
	cout << "图像平均值为:" << ymean_mat << endl;
	cout << "图像标准差为:" <<  ymean_stdDev<< endl;

	Mat outimg = img.clone();
	Mat_<Vec3b>::iterator it = outimg.begin< Vec3b>();
	Mat_<Vec3b>::iterator itend = outimg.end< Vec3b>();

	int sum = 0;
	int div = outimg.cols * outimg.rows* outimg.channels();
	
	
	for (; it != itend; it++)
	{
		sum += (*it)[0] + (*it)[1] + (*it)[2];
	}
	float arv = sum / div;
	cout << "迭代器遍历图像平均值为:" << endl;
	cout << arv << endl;


	//-------------随机数填充生成图像------------------------
	RNG rng = theRNG();
	Mat matf = Mat(2592, 1944, CV_8UC4,Scalar(0));
	rng.fill(matf, RNG::UNIFORM, 0, 255);
	namedWindow("Imgfill", WINDOW_NORMAL);
	imshow("Imgfill", matf);
	waitKey(0);
    
}

你可能感兴趣的:(OpenCV,opencv,图像处理,计算机视觉)