Qt图像处理技术三:图片亮度与对比度处理
Qt图像处理技术三:图片亮度和对比度处理
github地址
实战应用项目:
github :https://github.com/dependon/simple-image-filter //纯qt图像处理项目
效果图
公式
value都是取0-300
red=0.01对比值red-150+曝光值;
green=0.01对比值green-150+曝光值;
blue=0.01对比值blue-150+曝光值;
代码(容易理解)
QImage lightContrastImage(const QImage &img, int light, int Contrast)
{
QImage imgCopy = QImage(img);
for (int y = 0; y < img.height(); y++) {
for (int x = 0; x < img.width(); x++) {
float r = light * 0.01 * qRed(img.pixel(x, y)) - 150 + Contrast;
float g = light * 0.01 * qGreen(img.pixel(x, y)) - 150 + Contrast;
float b = light * 0.01 * qBlue(img.pixel(x, y)) - 150 + Contrast;
r = Bound(0, r, 255);
g = Bound(0, g, 255);
b = Bound(0, b, 255);
imgCopy.setPixel(x, y, qRgb(r, g, b));
}
}
return imgCopy;
}
源码(相对上述源码快5-20倍,因为绕过了Qt每个取点的检测)
QImage QImageAPI::lightContrastImage(const QImage &img, int light, int Contrast)
{
QImage imgCopy;
if (img.format() != QImage::Format_RGB888) {
imgCopy = QImage(img).convertToFormat(QImage::Format_RGB888);
} else {
imgCopy = QImage(img);
}
uint8_t *rgb = imgCopy.bits();
if (nullptr == rgb) {
return QImage();
}
int r;
int g;
int b;
int size = img.width() * img.height();
for (int i = 0; i < size ; i++) {
r = light * 0.01 * rgb[i * 3] - 150 + Contrast;
g = light * 0.01 * rgb[i * 3 + 1] - 150 + Contrast;
b = light * 0.01 * rgb[i * 3 + 2] - 150 + Contrast;
r = Bound(0, r, 255);
g = Bound(0, g, 255);
b = Bound(0, b, 255);
rgb[i * 3] = r;
rgb[i * 3 + 1] = g;
rgb[i * 3 + 2] = b;
}
return imgCopy;
}
联系我
liuminghang0821@qq.com
有问必答,期待你的指教肯小星星