图像增强OpenCV+C++

1、添加椒盐噪声

Mat addSaltNoise(const Mat srcImage, int n)
{
	Mat dstImage = srcImage.clone();
	for (int k = 0; k < n; k++)
	{
		//随机取值行列  
		int i = rand() % dstImage.rows;
		int j = rand() % dstImage.cols;
		//图像通道判定  
		if (dstImage.channels() == 1)
		{
			dstImage.at(i, j) = 255;       //盐噪声  
		}
		else
		{
			dstImage.at(i, j)[0] = 255;
			dstImage.at(i, j)[1] = 255;
			dstImage.at(i, j)[2] = 255;
		}
	}
	for (int k = 0; k < n; k++)
	{
		//随机取值行列  
		int i = rand() % dstImage.rows;
		int j = rand() % dstImage.cols;
		//图像通道判定  
		if (dstImage.channels() == 1)
		{
			dstImage.at(i, j) = 0;     //椒噪声  
		}
		else
		{
			dstImage.at(i, j)[0] = 0;
			dstImage.at(i, j)[1] = 0;
			dstImage.at(i, j)[2] = 0;
		}
	}
	return dstImage;
}
	int n = 1;
	while (n<=500)
	{
		ImgName = "000";
		ImgName1 = "000";
		string str;
		string str1;
		stringstream ss;
		stringstream ss1;
		ss << n;
		ss >> str;
		ImgName = ImgName + str;
		ImgName = "image\\" + ImgName + ".bmp";
		Mat srcImage = imread(ImgName, 0);
		srcImage = addSaltNoise(srcImage, 3000);
		imshow("1", srcImage);
		string first = "s";
	    imwrite(ImgName+ "s" +".bmp", srcImage);
		waitKey(1);
		n++;
	}

2、添加高斯噪声

//生成高斯噪声  
double generateGaussianNoise(double mu, double sigma)
{
	//定义小值 
	//const double epsilon = numeric_limits::min();
	const double epsilon = 0.001;
	static double z0, z1;
	static bool flag = false;
	flag = !flag;
	//flag为假构造高斯随机变量X  
	if (!flag)
		return z1 * sigma + mu;
	double u1, u2;
	//构造随机变量  
	do
	{
		u1 = rand() * (1.0 / RAND_MAX);
		u2 = rand() * (1.0 / RAND_MAX);
	} while (u1 <= epsilon);
	//flag为真构造高斯随机变量  
	z0 = sqrt(-2.0*log(u1))*cos(2 * CV_PI*u2);
	z1 = sqrt(-2.0*log(u1))*sin(2 * CV_PI*u2);
	return z0*sigma + mu;
}

//为图像添加高斯噪声  
Mat addGaussianNoise(Mat &srcImag)
{
	Mat dstImage = srcImag.clone();
	int channels = dstImage.channels();
	int rowsNumber = dstImage.rows;
	int colsNumber = dstImage.cols*channels;
	//判断图像的连续性  
	if (dstImage.isContinuous())
	{
		colsNumber *= rowsNumber;
		rowsNumber = 1;
	}
	for (int i = 0; i < rowsNumber; i++)
	{
		for (int j = 0; j < colsNumber; j++)
		{
			//添加高斯噪声  
			int val = dstImage.ptr(i)[j] +
				generateGaussianNoise(0, 2.235) * 32;
			if (val < 0)
				val = 0;
			if (val>255)
				val = 255;
			dstImage.ptr(i)[j] = (uchar)val;
		}
	}
	return dstImage;
}
	int n = 1;
	ofstream OutFile;//利用构造函数创建txt文本,并且打开该文本
	ofstream outFile1;
	BOOL isProcessed = FALSE;
	while (n<=500)
	{
		ImgName = "000";
		ImgName1 = "000";
		string str;
		string str1;
		stringstream ss;
		stringstream ss1;
		ss << n;
		ss >> str;
		ss1 << n + 1;
		ss1 >> str1;
		ImgName = ImgName + str;
		ImgName1 = ImgName1 + str1;
		ImgNamesave = ImgName;
		ImgName = "image\\" + ImgName + ".bmp";
		Mat srcImage = imread(ImgName, 0);
		srcImage = addGaussianNoise(srcImage);
		imshow("1", srcImage);
		string first = "s";
	    imwrite(ImgName+ "s" +".bmp", srcImage);
		waitKey(1);
		n++;

3、图像滤波

	int n = 1;
	while (n<=500)
	{
		ImgName = "000";
		ImgName1 = "000";
		string str;
		string str1;
		stringstream ss;
		stringstream ss1;
		ss << n;
		ss >> str;
		ss1 << n + 1;
		ss1 >> str1;
		ImgName = ImgName + str;
		ImgName1 = ImgName1 + str1;
		ImgNamesave = ImgName;
		ImgName = "image\\" + ImgName + ".bmp";
		Mat srcImage = imread(ImgName, 0);
		medianBlur(srcImage, srcImage, 5);    //执行时间5ms
		imshow("1", srcImage);
		string first = "s";
	    imwrite(ImgName+ "s" +".bmp", srcImage);
		waitKey(1);
		n++;

4、图像亮度调整

	int n = 1;
	while (n<=500)
	{
		ImgName = "000";
		ImgName1 = "000";
		string str;
		string str1;
		stringstream ss;
		stringstream ss1;
		ss << n;
		ss >> str;
		ss1 << n + 1;
		ss1 >> str1;
		ImgName = ImgName + str;
		ImgName1 = ImgName1 + str1;
		ImgNamesave = ImgName;
		ImgName = "image\\" + ImgName + ".bmp";
		Mat srcImage = imread(ImgName, 0);
		for (int i = 0; i < srcImage.rows; i++)
		{
			for (int j = 0; j < srcImage.cols; j++)
			{
				srcImage.at(i, j)= srcImage.at(i, j)/1.07;
				if(srcImage.at(i, j)>253)
				{
					srcImage.at(i, j) = 253;
				}
			}
		}

		//medianBlur(srcImage, srcImage, 5);    //执行时间5ms
		//srcImage = addGaussianNoise(srcImage);
		imshow("1", srcImage);
		string first = "s";
	    imwrite(ImgName+ "s" +".bmp", srcImage);
		waitKey(1);
		n++;
	}

5、图像旋转

	int n = 1;
	ofstream OutFile;//利用构造函数创建txt文本,并且打开该文本
	ofstream outFile1;
	BOOL isProcessed = FALSE;
	while (n<=500)
	{
		ImgName = "000";
		ImgName1 = "000";
		string str;
		string str1;
		stringstream ss;
		stringstream ss1;
		ss << n;
		ss >> str;
		ss1 << n + 1;
		ss1 >> str1;
		ImgName = ImgName + str;
		ImgName1 = ImgName1 + str1;
		ImgNamesave = ImgName;
		ImgName = "image\\" + ImgName + ".bmp";
		Mat srcImage = imread(ImgName, 0);

		cv::flip(srcImage,srcImage,1);//左右翻转
		cv::flip(srcImage,srcImage,0);//上下翻转(没有意义)
		cv::flip(srcImage,srcImage,-1);//水平垂直翻转(没有意义)

		imshow("1", srcImage);
		string first = "s";
	    imwrite(ImgName+ "s" +".bmp", srcImage);
		waitKey(1);
		n++;
	}

6、图像仿射变换

	int n = 1;
	while (n<=500)
	{
		ImgName = "000";
		ImgName1 = "000";
		string str;
		string str1;
		stringstream ss;
		stringstream ss1;
		ss << n;
		ss >> str;
		ss1 << n + 1;
		ss1 >> str1;
		ImgName = ImgName + str;
		ImgName1 = ImgName1 + str1;
		ImgNamesave = ImgName;
		ImgName = "image\\" + ImgName + ".bmp";
		Mat srcImage = imread(ImgName, 0);

		Mat srcImage_warp;
		Point2f srcPoints[3];//原图中的三点 ,一个包含三维点(x,y)的数组,其中x、y是浮点型数
		Point2f dstPoints[3];//目标图中的三点  
 
		//第一种仿射变换的调用方式:三点法
		//三个点对的值,上面也说了,只要知道你想要变换后图的三个点的坐标,就可以实现仿射变换  
		srcPoints[0] = Point2f(0, 0);
		srcPoints[1] = Point2f(0, srcImage.rows);
		srcPoints[2] = Point2f(srcImage.cols, 0);
		//映射后的三个坐标值
		dstPoints[0] = Point2f(0, srcImage.rows*0.08);
		dstPoints[1] = Point2f(srcImage.cols*0.05, srcImage.rows*0.95);
		dstPoints[2] = Point2f(srcImage.cols*0.95, srcImage.rows*0.05);
		Mat M1 = getAffineTransform(srcPoints, dstPoints);//由三个点对计算变换矩阵  
		warpAffine(srcImage, srcImage, M1, srcImage.size());//仿射变换
		imshow("1", srcImage);
		string first = "s";
	    imwrite(ImgName+ "s" +".bmp", srcImage);
		//isProcessed = ProcessSymmetricVImage1(srcImage);
		waitKey(1);
		n++;
	}

你可能感兴趣的:(工程,opencv,c++,计算机视觉)