实现遍历文件夹下图片,并给图片加噪声和模糊(注意图像数组不要越界)还有insert函数的使用

#include
#include
#include
#include
#include
#include //用system函数
//#include

#include 
#include
#include
#include
//#include
using namespace std;
using namespace cv;


void processing(char mode);





Mat addSaltNoise(const Mat srcImage, int n, int scale)
{
	Mat dstImage = srcImage.clone();
	for (int k = 0; k < n; k++)
	{
		//随机取值行列
		int i = rand() % dstImage.rows;
		int j = rand() % dstImage.cols;

		for (int r =0; r ((i + r) % dstImage.rows, (j + l) % dstImage.cols)[0] = 200;
				dstImage.at((i + r) % dstImage.rows, (j + l) % dstImage.cols)[1] = 200;
				dstImage.at((i + r) % dstImage.rows, (j + l) % dstImage.cols)[2] = 200;
				//printf("%d  ", i + r);
				//printf("%d  ", j + l);
				//printf("%d\n", k);
				//int a = i + r;
				//int b = j + l;
				//int a = dstImage.at((i + r) % dstImage.rows, (j + l) % dstImage.cols)[0];
				//printf("%d", dstImage.at((i + r) % dstImage.rows, (j + l) % dstImage.cols)[0]);
			}
	}


	for (int k = 0; k < n; k++)
	{
		//随机取值行列
		int i = rand() % dstImage.rows;
		int j = rand() % dstImage.cols;
		for (int r = 0; r ((i + r) % dstImage.rows, (j + l) % dstImage.cols)[0] = 0;
				dstImage.at((i + r) % dstImage.rows, (j + l) % dstImage.cols)[1] = 0;
				dstImage.at((i + r) % dstImage.rows, (j + l) % dstImage.cols)[2] = 0;
			}
	}
	return dstImage;
}













void main()
{
	cout << "switch mode:";
	char mode;
	cin >> mode;
	processing(mode);

}

void processing(char mode)
{
	Directory dir;
	string a = "\\";
	string b = "/";
	string s1 = "_c1";
	string s2 = "_c2";
	string s3 = "_c3";

	string exten = "*";
	string exten1 = "*.jpg";
	bool addpath = false;
	string pathall = "F:/yangben/train_1017_zaosheng_yunxing/";





	switch (mode)
	{
	case 't':
	{
		string pathx = pathall + "trainall/";
		string pathxf = pathx + "train/";
		vectorfoldernames = dir.GetListFolders(pathxf, exten, addpath);
		for (int i = 0; i < foldernames.size(); i++)
		{
			string foldername = foldernames[i];
			string pathxfi = pathxf + foldername;
			vectorfilenames = dir.GetListFiles(pathxfi, exten1, addpath);
			for (int n = 0; n < filenames.size(); n++)
			{
				cout << "第" << n << "张图片"<foldernames = dir.GetListFolders(pathxf, exten, addpath);
		for (int i = 0; i < foldernames.size(); i++)
		{
			string foldername = foldernames[i];
			string pathxfi = pathxf + foldername;
			vectorfilenames = dir.GetListFiles(pathxfi, exten1, addpath);
			for (int n = 0; n < filenames.size(); n++)
			{
				cout << "第" << n << "张图片"<

你可能感兴趣的:(opencv)