图片灰度化批量处理




#include 

#include "opencv2/opencv.hpp"

#include "opencv/cv.h"



using namespace std;

using namespace cv;



void cvtCOLOR(Mat src, Mat dst)

{

	float R, G, B;

	for (int y = 0; y < src.rows; y++)

	{

		uchar* data = dst.ptr(y);

		for (int x = 0; x < src.cols; x++)

		{

			B = src.at(y, x)[0];

			G = src.at(y, x)[1];

			R = src.at(y, x)[2];

			data[x] = (int)(R * 0.299 + G * 0.587 + B * 0.114);//利用公式计算灰度值(加权平均法)

		}

	}

}



void main()

{
	string a[] = { "cam1.jpg","cam2.jpg","cam3.jpg","cam4.jpg","cam5.jpg","cam6.jpg",

		"cam7.jpg","cam8.jpg","cam9.jpg","cam10.jpg","cam11.jpg","cam12.jpg" };

	int i;

	i = 0;

	int image_count = 0;

	ifstream fin("F:/repos/gray/Project3/calibdata3.txt");

	string filename;      // 图片名

	vector filenames;

	while (getline(fin, filename))
	{
		++image_count;

		Mat src = imread(filename);
		
		Mat dst(src.rows, src.cols, CV_8UC1);//大小与原图相同的八位单通道图

		cvtCOLOR(src, dst);

		imshow("原始图", src);

		imshow("灰度图", dst);

		imwrite(a[i], dst);

		i = i + 1;

		waitKey(84);

	}
	

}

看过很多别人写的不满意,就自己写了个,新人哈哈哈

你可能感兴趣的:(图片灰度化批量处理)