原理参考了 opencv关于对比度和亮度的误解
通过设计一个映射曲线就可完成对比度、亮度调整,具体过程如下图所示,
a) 为原图;
b) 把亮度调高,像素强度+固定值;
c) 把亮度调低,像素强度-固定值;
d) 增大像素强度75附近的对比度;增大附近的斜率
e) 增大像素强度150附近的对比度;
f) 增大像素强度75和225附近的对比度。
曲线斜率大于45度角的区域灰度被拉伸、精度上升、对比度变高;
曲线斜率小于45度角的区域灰度被压缩、精度下降、对比度变低。
下面图像是 0-255 映射到 0-255, a原图,b原图加上一个数,c原图减去一个数,d在value=75处拉伸,e在value=150出拉伸,f在75和225出进行拉伸。拉伸操作可以提升该区域的对比度
首先减去均值,然后调整对比度(乘上大于1的系数进行拉伸),然后加上均值,最后调整亮度。
aa是拉伸系数,bb是亮度变化
img_a = aa * (img - bri_mean) + bb + bri_mean
import cv2
import numpy as np
from matplotlib import pyplot as plt
if __name__ == "__main__":
file = r'D:\dataset\data_gain1_2\cap_frame_0002_raw.png'
img = cv2.imread(file).astype(np.float32)
bri_mean = np.mean(img)
a = np.arange(5, 16, 5) / 10
b = np.arange(-30, 31, 30)
a_len = len(a)
b_len = len(b)
print(a_len, b_len)
plt.figure()
for i in range(a_len):
for j in range(b_len):
aa = a[i]
bb = b[j]
img_a = aa * (img-bri_mean) + bb + bri_mean
print(i, j, aa, bb)
img_a = np.clip(img_a,0,255).astype(np.uint8)
plt.subplot(a_len+1, b_len, (j + b_len * i + 1))
plt.imshow(img_a, cmap='gray')
plt.subplot(a_len + 1, b_len, a_len*b_len+1)
plt.imshow(img.astype(np.uint8), cmap='gray')
plt.show()
opencv demo code
对应直方图的变化:
原图
增加亮度,直方图向右平移
增加对比度, 直方图更加均衡
首先认识 convertTo函数
image.convertTo(dst, CV_8U, a, b);
converTo 中的a,b是相乘和相加系数
opencv里是这样得到 a,b系数的
opencv中时如何求 alpha和beta的?如下:
if (contrast > 0)
{
delta = 127. * contrast / 100;
a = 255. / (255. - delta * 2);
b = a * (brightness - delta);
}
else
{
delta = -128. * contrast / 100;
a = (256. - delta * 2) / 255.;
b = a * brightness + delta;
}
img_a = aa * (img - bri_mean) + bb + bri_mean
bri_mean锚定为127的时候
img_a = aa * (img - 127) + bb + 127 = aa * img - 127*aa + bb + 127 = aa * img + (1-aa) * 127 + bb
_brightness 和 _contrast 的范围是 0-200, 等于100时不进行调整, 参考和运行下面的demo程序
a = _contrast / 100.0f;
b = (1 - a) * 127 + _brightness - 100;
通过对比直方图的变化效果 可知上面两个方法 作用是类似的。
本来想找opencv的方法对应的原理链接,但是链接已经失效
完整demo:
#include "opencv2/core/utility.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include
using namespace cv;
using namespace std;
int _brightness = 100;
int _contrast = 100;
Mat image;
/* brightness/contrast callback function */
static void updateBrightnessContrast(int /*arg*/, void*)
{
int histSize = 64;
int brightness = _brightness - 100;
int contrast = _contrast - 100;
/*
* The algorithm is by Werner D. Streidt
* (http://visca.com/ffactory/archives/5-99/msg00021.html)
*/
double a, b;
double delta=0;
if (contrast > 0)
{
delta = 127. * contrast / 100;
a = 255. / (255. - delta * 2);
b = a * (brightness - delta);
}
else
{
delta = -128. * contrast / 100;
a = (256. - delta * 2) / 255.;
b = a * brightness + delta;
}
/*method 2*/
//a = _contrast / 100.0f;
//b = (1 - a) * 127 + _brightness - 100;
//printf("ab value :%.4lf, %.4lf, %.4lf\n",delta, a, b);
Mat dst, hist;
image.convertTo(dst, CV_8U, a, b);
imshow("image", dst);
calcHist(&dst, 1, 0, Mat(), hist, 1, &histSize, 0);
Mat histImage = Mat::ones(200, 320, CV_8U) * 255;
normalize(hist, hist, 0, histImage.rows, NORM_MINMAX, CV_32F);
histImage = Scalar::all(255);
int binW = cvRound((double)histImage.cols / histSize);
for (int i = 0; i < histSize; i++)
rectangle(histImage, Point(i * binW, histImage.rows),
Point((i + 1) * binW, histImage.rows - cvRound(hist.at<float>(i))),
Scalar::all(0), -1, 8, 0);
imshow("histogram", histImage);
}
const char* keys =
{
"{help h||}{@image|baboon.jpg|input image file}"
};
int main(int argc, const char** argv)
{
CommandLineParser parser(argc, argv, keys);
parser.about("\nThis program demonstrates the use of calcHist() -- histogram creation.\n");
if (parser.has("help"))
{
parser.printMessage();
return 0;
}
string inputImage = parser.get<string>(0);
// Load the source image. HighGUI use.
image = imread(samples::findFile(inputImage), IMREAD_GRAYSCALE);
if (image.empty())
{
std::cerr << "Cannot read image file: " << inputImage << std::endl;
return -1;
}
namedWindow("image", 0);
namedWindow("histogram", 0);
createTrackbar("brightness", "image", &_brightness, 200, updateBrightnessContrast);
createTrackbar("contrast", "image", &_contrast, 200, updateBrightnessContrast);
updateBrightnessContrast(0, 0);
waitKey();
return 0;
}