opencvC++学习6调整图像亮度与对比度

图像变换可以看作如下:
 - 像素变换 – 点操作
 - 邻域操作 – 区域

调整图像亮度和对比度属于像素变换-点操作

公式:

α:表示对比度;β:表示亮度;

#include <opencv2\opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;

int main()
{
	Mat src;
	src = imread("D:/opencv/test.jpg");
	if (src.empty()) {

		printf("imread imgae error");
		return -1;
	}
	namedWindow("input", CV_WINDOW_AUTOSIZE);
	imshow("input", src);


	//cvtColor(src, src, CV_BGR2GRAY);//把src转为灰度图像

	int height = src.rows;
	int width = src.cols;
	int channels = src.channels();
	double alpha = 1.2;
	double bata = 30;

	Mat dst;
	dst = Mat::zeros(src.size(), src.type());
	//src.convertTo(dst, CV_32F);
	printf("height=%d width=%d channels=%d", height, width, channels);
	for (int row = 0; row < height; row++) {

		for (int col = 0; col < width; col++) {

			if (channels == 1) {

				dst.at<uchar>(row, col) = saturate_cast<uchar>(alpha * src.at<uchar>(row, col) + bata);
			}
			if (channels == 3) {

				dst.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(alpha * src.at<Vec3b>(row, col)[0] + bata);
				dst.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(alpha * src.at<Vec3b>(row, col)[1] + bata);
				dst.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(alpha * src.at<Vec3b>(row, col)[2] + bata);
			}
		}
	}
	namedWindow("output", CV_WINDOW_AUTOSIZE);
	imshow("output", dst);

	waitKey(0);
	return 0;
}

效果图:


猜你喜欢

转载自blog.csdn.net/xiansong1005/article/details/80622421