OpenCV C++ 改变亮度、对比度、图像膨胀腐蚀

OpenCV C++ 改变图片亮度、对比度

目录

  • OpenCV C++ 改变图片亮度、对比度
  • 一、改变图片视频亮度
    • 1. 图片
    • 2. 视频
    • 3. 改变对比度
  • 二、高斯模糊
  • 三、图像腐蚀
  • 四、图像膨胀
  • 总结


一、改变图片视频亮度

1. 图片

#include 
#include 

using namespace std;
using namespace cv;

int main() {
	Mat img = imread("C:/Users/12271/Desktop/test/eren.jpg");

	if (img.empty() == 1) {
		cout << "cannot load the image" << endl;
		return -1;
	}

	Mat img_brighness_high100, img_brighness_low100;
	img.convertTo(img_brighness_high100, -1, 1, 100);
	img.convertTo(img_brighness_low100, -1, 1, -100);

	string high_img = "brighness high 100", low_img = "brighness low 100", original = "original";

	namedWindow(high_img);
	namedWindow(low_img);
	namedWindow(original);

	imshow(high_img, img_brighness_high100);
	imshow(low_img, img_brighness_low100);
	imshow(original, img);
	waitKey(0);
	destroyAllWindows();
}

2. 视频

更改视频亮度时要将新建数组放在while循环内,每帧都要创建一个新数组用来存放更改亮度后的图片。

#include 
#include 

using namespace std;
using namespace cv;

int main() {
	VideoCapture cap(0);

	if (cap.isOpened() == 0) {
		cout << "cannot load the video or webcam" << endl;
		return -1;
	}

	string brighness_high = "+100", brightness_low = "-100", original = "original";

	namedWindow(brighness_high);
	namedWindow(brightness_low);
	namedWindow(original);

	while (1) {
		Mat frame;

		bool is_read = cap.read(frame);
		if (is_read == 0) {
			cout << "this is the end" << endl;
			break;
		}

		Mat brighter, lower;
		frame.convertTo(brighter, -1, 1, 100);
		frame.convertTo(lower, -1, 1, -100);

		imshow(brighness_high, brighter);
		imshow(brightness_low, lower);
		imshow(original, frame);

		if (waitKey(10) == 27) {
			break;
		}
	}
	return 0;
}

3. 改变对比度

		frame.convertTo(brighter, -1, 2, 0);

参数分别为:void Mat::convertTo( OutputArray m, int rtype, double alpha=1, double beta=0 ) const
m 输出图片,发起请求时将原图片数据重新分配到该数组内存中。
rtype 输出图片的格式,值为负数时,输出格式将和输入图片格式保持一致。
alpha 输出前数组中的每个像素都将与该数相乘。
beta 输出前数组中每个像素都与该数相加

改变方法中第三个参数的值即可,alpha>1,对比度增强,0 < alpha < 1, 对比度减弱

二、高斯模糊

#include 
#include 

using namespace std;
using namespace cv;

int main() {
	VideoCapture cap(0);

	if (cap.isOpened() == 0) {
		cout << "cannot load the video or webcam" << endl;
		return -1;
	}

	string gauss = "gauss", original = "original";

	namedWindow(gauss);
	namedWindow(original);

	while (1) {
		Mat frame;

		bool is_read = cap.read(frame);
		if (is_read == 0) {
			cout << "this is the end" << endl;
			break;
		}

		Mat gaussfiler;
		GaussianBlur(frame, gaussfiler, Size(3, 3), 0);

		imshow(gauss, gaussfiler);
		imshow(original, frame);

		if (waitKey(10) == 27) {
			break;
		}
	}
	return 0;
}

三、图像腐蚀

#include 
#include 

using namespace std;
using namespace cv;

int main() {
	VideoCapture cap(0);

	if (cap.isOpened() == 0) {
		cout << "cannot load the video or webcam" << endl;
		return -1;
	}

	string erode_win = "erosion", original = "original";

	namedWindow(erode_win);
	namedWindow(original);

	while (1) {
		Mat frame;

		bool is_read = cap.read(frame);
		if (is_read == 0) {
			cout << "this is the end" << endl;
			break;
		}

		Mat erosion_img;
		erode(frame, erosion_img, getStructuringElement(MORPH_RECT, Size(5,	5)));

		imshow(erode_win, erosion_img);
		imshow(original, frame);

		if (waitKey(10) == 27) {
			break;
		}
	}
	return 0;
}

卡姿兰大眼睛

四、图像膨胀

太丑了,卧槽

#include 
#include 

using namespace std;
using namespace cv;

int main() {
	VideoCapture cap(0);

	if (cap.isOpened() == 0) {
		cout << "cannot load the video or webcam" << endl;
		return -1;
	}

	string dilate_win = "dilate img", original = "original";

	namedWindow(dilate_win);
	namedWindow(original);

	while (1) {
		Mat frame;

		bool is_read = cap.read(frame);
		if (is_read == 0) {
			cout << "this is the end" << endl;
			break;
		}

		Mat dilate_img;
		dilate(frame, dilate_img, getStructuringElement(MORPH_RECT, Size(5,	5)));

		imshow(dilate_win, dilate_img);
		imshow(original, frame);

		if (waitKey(10) == 27) {
			break;
		}
	}
	return 0;
}

总结

好像没啥总结,溜了。

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