对比度和亮度(一)
作者:
小小蒟蒻
,
2021-11-14 04:13:52
,
所有人可见
,
阅读 301
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace std;
int main()
{
cv::Mat mat1(8, 8, CV_8UC3);
cv::randu(mat1, cv::Scalar::all(0), cv::Scalar::all(255));
cout << "src = " << endl << mat1 << endl;
cv::Mat mat2;
double alpha = 0.5, beta = 50;
mat1.convertTo(mat2, -1, alpha, beta);
cout << "src convertTo() = " << endl << mat2 << endl;
// 按照对比度和亮度的线性公式进行调整
cv::Mat mat3(mat1.rows, mat1.cols, CV_8UC3);
if (mat3.empty())
return -1;
for (int row = 0; row < mat1.rows; ++row)
{
uchar* p1 = mat1.ptr<uchar>(row);
uchar* p3 = mat3.ptr<uchar>(row);
for (int col = 0; col < mat1.cols; ++col)
{
p3[col] = cv::saturate_cast<uchar>(p1[col] * alpha + beta);
}
}
cout << "alpha * mat + beta = " << endl << mat3 << endl;
// 查看两种方法的结果差异
cv::Mat diff = mat2 - mat3;
if (diff.empty())
return -1;
cout << "diff = " << endl << diff << endl;
return 0;
}