You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于CImg的简易对比度增强算法失效问题排查求助

HSI色彩空间对比度增强输出过暗,CImg库实现排查求助

我已经对着代码折腾了好几个小时,还是搞不明白为什么我的对比度增强算法达不到预期效果。情况是这样的:

我参考了一份用Pillow库实现的对比度增强指南,但因为要保留图像原有色彩,我改用了HSI色彩空间来处理强度(I)通道。指南里只提了HSI的修改方向,没给具体步骤,而且我用的是CImg库,和指南的工具链不一样。

代码能正常编译运行,但输出的图像整体偏暗——我想要的是类似Photoshop Camera Raw滤镜里把对比度滑块拉满的那种强烈对比效果,现在完全达不到。

下面是我调试时输出的部分强度值转换数据,以及原图像强度通道的最小、最大值:

old Intensity 0.422222 new Intensity 0.313531
old Intensity 0.437909 new Intensity 0.353135
old Intensity 0.437909 new Intensity 0.353135
old Intensity 0.436601 new Intensity 0.349835
old Intensity 0.439216 new Intensity 0.356436
old Intensity 0.443137 new Intensity 0.366337
old Intensity 0.45098 new Intensity 0.386139
old Intensity 0.458824 new Intensity 0.405941
old Intensity 0.461438 new Intensity 0.412541
min 0.298039 max 0.694118

我的实现代码

#include <iostream>
#include "CImg.h"
int main() {
 cimg_library::CImg<float> lenaCondec("./colors/lena_condec.jpeg");
 int width = lenaCondec.width();
 int height = lenaCondec.height();
 // enhancing contrast
 float minIntensity = 1.0f;
 float maxIntensity = 0.0f;
 cimg_library::CImg<float> imgBuffer = lenaCondec.get_RGBtoHSI();
 for (int row = 0; row < height; row++)
 for (int col = 0; col < width; col++) {
 const auto I = imgBuffer(col, row, 0, 2);
 minIntensity = std::min((float)I, minIntensity);
 maxIntensity = std::max((float)I, maxIntensity);
 }
 for (int row = 0; row < height; row++)
 for (int col = 0; col < width; col++) {
 auto I = imgBuffer(col, row, 0, 2);
 const auto newIntensity = (((float)I - minIntensity) / (maxIntensity - minIntensity));
 std::cout << "old Intensity " << (float)I << std::endl;
 imgBuffer(col, row, 0, 2) = newIntensity;
 I = imgBuffer(col, row, 0, 2);
 std::cout << "new Intensity " << (float)I << std::endl;
 }
 std::cout << "min " << minIntensity << std::endl;
 std::cout << "max " << maxIntensity << std::endl;
 cimg_library::CImg<float> outputImg = imgBuffer.get_HSItoRGB();
 // Debugging
 outputImg.save_jpeg("./colors/output-image.jpeg");
 std::getchar();
 return 0;
}

额外信息

  • 代码仓库需要切换到「so-question」分支
  • 我已经修改了CImg.h的第389行,将#include <X11/Xlib.h>改为#include "X11/Xlib.h"

有没有大佬能帮忙看看问题出在哪?多谢了!

内容的提问来源于stack exchange,提问作者Ryan Sam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 06:55:33