我已經檢查我的代碼幾個小時了,我不確定為什么這種對比演算法不起作用。
按照本
如果在 Photoshop 中使用相機原始濾鏡增加對比度,我希望得到類似于我得到的輸出。這是最大化 Photoshop 對比度滑塊的結果:

這是修改后的強度值和最小最大值的尾部:
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;
}
I have a repo for this
或者,您可以強制CImg在 Mac 上使用 IJPEG 實作:
clang $(pkg-config --cflags --libs libjpeg) -std=c 17 -Dcimg_use_jpeg -lm -lpthread -o "main" "main.cpp"
作為先決條件,您可能需要安裝pkkconfig并jpeg使用homebrew:
brew install jpeg pkgconfig
另請注意,只要您不想使用CImg display(),您可以通過將編譯命令更改為以下內容來避免需要將 X11 的所有路徑和開關放在編譯命令上:
clang -Dcimg_display=0 ...
uj5u.com熱心網友回復:
正如您所提到的,您可能會考慮其他拉伸對比度的方法,我想我會添加另一個選項,您可以在 RGB 色彩空間中進行操作。如果您找到紅色通道的最小值和最大值并拉伸紅色,然后對其他通道進行同樣的操作,您將引入偏色。因此,另一種方法是找到所有通道的最小值和所有通道的最大值,然后將通道一起拉伸相同的量。
實際上,您正在拉伸 RGB 直方圖,直到任何通道達到 0 或 255。我的 C 有點笨拙,但看起來像這樣:
#include <iostream>
#include "CImg.h"
int main() {
cimg_library::CImg<unsigned char> img("lena.png");
int width = img.width();
int height = img.height();
// Find min and max RGB values for whole image
unsigned char RGBmin = 255;
unsigned char RGBmax = 0;
for (int row = 0; row < height; row ) {
for (int col = 0; col < width; col ) {
const auto R = img(col, row, 0, 0);
const auto G = img(col, row, 0, 1);
const auto B = img(col, row, 0, 2);
RGBmin = std::min({R,G,B,RGBmin});
RGBmax = std::max({R,G,B,RGBmax});
}
}
std::cout << "RGBmin=" << int(RGBmin) << ", RGBmax=" << int(RGBmax) << std::endl;
// Stretch contrast equally for all channels
for (int row = 0; row < height; row ) {
for (int col = 0; col < width; col ) {
for (int chan = 0; chan <=3; chan ) {
const auto x = img(col, row, 0, chan);
const auto newVal = 255*((float)x - RGBmin) / (RGBmax - RGBmin);
img(col, row, 0, chan) = (unsigned char)newVal;
}
}
}
// Debugging
img.save("result2.png");
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/415704.html
標籤:
