1

我试图对图像应用对比度,但我找不到一个好的公式来做到这一点。我尝试了许多不同的网站公式,但都没有奏效。有人有什么建议吗?

这是我最近的尝试:

int r = Colors.red(pixel);
int g = Colors.green(pixel);
int b = Colors.blue(pixel);

float red = r / 255.0f;
float green = g / 255.0f;
float blue = b / 255.0f;

red = (((red - 0.5f) * amount) + 0.5f) * 255.0f;
green = (((green - 0.5f) * amount) + 0.5f) * 255.0f;
blue = (((blue - 0.5f) * amount) + 0.5f) * 255.0f;

int iR = (int)red;
iR = iR > 255 ? 255 : iR;
iR = iR < 0 ? 0 : iR;
int iG = (int)green;
iG = iG > 255 ? 255 : iG;
iG = iG < 0 ? 0 : iG;
int iB = (int)blue;
iB = iB > 255 ? 255 : iB;
iB = iB < 0 ? 0 : iB;

那没有用。有人有其他建议吗?

4

2 回答 2

1

我想到了!这是寻找相同效果的任何人的完整代码。

float value = (255.0f + amount) / 255.0f;
value *= value;
for(int i = 0; i < ImageSync.previewPixels.length; i++){
    int pixel = ImageSync.previewPixels[i];
    int r = Colors.red(pixel);
    int g = Colors.green(pixel);
    int b = Colors.blue(pixel);

    float red = r / 255.0f;
    float green = g / 255.0f;
    float blue = b / 255.0f;

    red = (((red - 0.5f) * value) + 0.5f) * 255.0f;
    green = (((green - 0.5f) * value) + 0.5f) * 255.0f;
    blue = (((blue - 0.5f) * value) + 0.5f) * 255.0f;

    int iR = (int)red;
    iR = iR > 255 ? 255 : iR;
    iR = iR < 0 ? 0 : iR;
    int iG = (int)green;
    iG = iG > 255 ? 255 : iG;
    iG = iG < 0 ? 0 : iG;
    int iB = (int)blue;
    iB = iB > 255 ? 255 : iB;
    iB = iB < 0 ? 0 : iB;

    ImageSync.previewPixels[i] = Colors.rgba(iR, iG, iB);
}
于 2012-11-21T19:18:39.457 回答
0

我开发了一个基于平均图像强度对比图像的公式。它的制作方式是,如果强度像素低于平均强度,它会变得更暗;但它高于平均强度,它会变得更亮。

C(x,y)=I(x,y)^{{(1+\overline{I}-I(x,y))}^{\alpha}}

\overline{I} = \frac{1}{NM}\sum_{x=0}^{N-1}\sum_{y=0}^{M-1}{I(x,y)}

I(x,y) 必须标准化为介于 0 和 1 之间。对比度级别由 alpha 因子定义。将其设置为 0,您将获得零对比度。

这是一些 OpenCV 代码:

cv::Mat setImageContrast(cv::Mat image, double alpha)
{
    cv::Mat output = cv::Mat::zeros(image.rows, image.cols, CV_8UC1);
    double I_mean = 0.0;
    int N = image.cols * image.rows;

    for(int x = 0; x < image.cols; x++) {
        for(int y = 0; y < image.rows; y++) {
            I_mean += (double)image.at<unsigned char>(y,x)/255.0;
        }
    }
    I_mean /= (double)N;

    for(int x = 0; x < image.cols; x++) {
        for(int y = 0; y < image.rows; y++) {
            double I = (double)image.at<unsigned char>(y,x)/255.0;
            I = pow(I, pow(1.0 + I_mean - I, alpha)) * 255.0;
            output.at<unsigned char>(y,x) = (int)I;
        }
    }
    return output;
}
于 2022-01-29T14:32:53.937 回答