【OpenCV(C++)】图像对比度、亮度值调整

【OpenCV(C++)】对比度、亮度值调整

理论依据

我们基于点操作,对图像进行处理变换。点操作的特点是:仅仅根据输入像素值或加上某些全局信息和参数,来计算相应的输出像素值。

在这里插入图片描述

程序示例

任务:调用本身电脑自带的摄像头得到图像,遍历图像所有像素点,使用滑动条对图像亮度,对比度进行调整,并添加一张灰度图片到原图像中的R通道中。

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;


#define WIN_NAME "输出图像" 
Mat srcImage;
Mat dstImage;
Mat dstImg;
Mat channels;
int Contrase_Value = 20;
int Bright_Value = 20;

void OnChange(int, void*)
{
	for (int i = 0; i < srcImage.rows; i++)
	{
		for (int j = 0; j < srcImage.cols; j++)
		{
			dstImage.at<Vec3b>(i, j)[0] = saturate_cast<uchar>(Contrase_Value * 0.01 * srcImage.at<Vec3b>(i, j)[0] + Bright_Value);
			dstImage.at<Vec3b>(i, j)[1] = saturate_cast<uchar>(Contrase_Value * 0.01 * srcImage.at<Vec3b>(i, j)[1] + Bright_Value);
			dstImage.at<Vec3b>(i, j)[2] = saturate_cast<uchar>(Contrase_Value * 0.01 * srcImage.at<Vec3b>(i, j)[2] + Bright_Value);
		}
	}

	namedWindow("原图", CV_WINDOW_NORMAL);
	imshow("原图", srcImage);
	imshow(WIN_NAME, dstImage);
}


int main()
{
	VideoCapture capture(0);

	while (1)
	{
		Mat frame;
		capture >> srcImage;

		dstImage = Mat::zeros(srcImage.size(), srcImage.type());

		namedWindow(WIN_NAME, CV_WINDOW_NORMAL);

		createTrackbar("对比度", WIN_NAME, &Contrase_Value, 200, OnChange, 0);
		createTrackbar("亮  度", WIN_NAME, &Bright_Value, 200, OnChange, 0);

		OnChange(Contrase_Value, 0);
		OnChange(Bright_Value, 0);
		waitKey(1);

		vector<Mat> channels;

		Mat imageRedChannel;

		dstImage = imread("pigh.jpg", 0);
		resize(dstImage, dstImage, cv::Size(srcImage.size()));
		cout << dstImage.channels();
		split(srcImage, channels);

		imageRedChannel = channels[2];

		resize(dstImage, dstImage, cv::Size(imageRedChannel.size()));
		addWeighted(imageRedChannel, 0.5,
			dstImage, 0.5, 0., imageRedChannel);
	
			merge(channels, srcImage);

		namedWindow("原画+pig红色通道 ");
		imshow("原画+pig红色通道 ", srcImage);
		waitKey(1);

	}

	return 0;

}

在这里插入图片描述

发布了25 篇原创文章 · 获赞 18 · 访问量 2887

猜你喜欢

转载自blog.csdn.net/weixin_43645790/article/details/104087173