1

嗨,我想在我的应用程序中实现对比度过滤器,例如此链接此对比度,但使用颜色矩阵和跟踪栏作为值
我已经找到了颜色矩阵

float c = mytrackbar.value * 0.01f //maxTrackbarValue = 100, minTrackbarValue = -100 
float t = 0.01f;
cmPicture = new ColorMatrix(new float[][] {
    new float[] {c,0,0,0,0},
    new float[] {0,c,0,0,0},
    new float[] {0,0,c,0,0},
    new float[] {0,0,0,1,0},
    new float[] {t,t,t,0,1}
});

但结果却大不相同。我尝试更改~c~中的0.01f 和 ~t~值中的 0.01f 但它只给出亮度等结果(例如:c = mytrackbar.value * 0.04f )

我想知道 ~c~ 和 ~t~ 值以及我应该使用多少最大和最小范围来创建对比度


更新@Nico

    private void myTrackBar_ValueChanged(object sender, EventArgs e) {
        imageHandle = imageHandleTemp.Bitmap;
        myNumericUpDown.Text = myTrackBar.Value.ToString();
        float value = myTrackBar.Value * 0.01f;

        Bitmap bmpInverted = new Bitmap(imageHandle.Width, imageHandle.Height);
        ImageAttributes ia = new ImageAttributes();
        ColorMatrix cmPicture = new ColorMatrix();

        float c = value;
        float t = 0.01f;
        cmPicture = new ColorMatrix(new float[][] {
            new float[] {c,0,0,0,0},
            new float[] {0,c,0,0,0},
            new float[] {0,0,c,0,0},
            new float[] {0,0,0,1,0},
            new float[] {t,t,t,0,1}
        });

        ia.SetColorMatrix(cmPicture);
        Graphics g = Graphics.FromImage(bmpInverted);
        g.DrawImage(imageHandle, new Rectangle(0, 0, imageHandle.Width, imageHandle.Height), 0, 0, imageHandle.Width, imageHandle.Height, GraphicsUnit.Pixel, ia);
        g.Dispose();

        Image<Bgr, Byte> myImage = new Image<Bgr, byte>(bmpInverted);
        imageBoxCamera.Image = myImage;
    }
4

2 回答 2

3

这里有一些关于这个过程背后的数学的进一步解释。如果您要接受答案,请接受 TaW 的回答,他比我快。

调整对比度基本上执行以下操作:

  1. 它将所有像素值移动 -0.5(使中灰色变为 0)
  2. 它将一个因子应用于所有像素值
  3. 它恢复了转变

所以在一个公式中:

newValue = factor * (oldValue - 0.5) + 0.5
         = factor * oldValue - factor * 0.5 + 0.5
         = factor * oldValue + 0.5 * (1 - factor)

对于单通道图像,我们可以将其转换为矩阵乘法:

(newValue, 1) = (oldValue, 1) * /        factor      , 0\     <-- this is c, 0
                                \ 0.5 * (1 - factor) , 1/     <-- this is t, 1

对于多通道图像,2x2 矩阵变为 5x5(四个通道 + w 通道用于翻译)矩阵,c 和 t 在您已经指定的位置。

请记住,一个因素1不会改变任何事情。一个因素0使整个图像呈中灰色。因此,您可能需要调整 c 的计算:

c = 1 + value;
t = 0.5f * (1.0f - c);
于 2014-05-26T09:24:39.667 回答
3

t 值必须来自新的对比度值 c。所以像这样改变它的分配:

浮动 t = (1.0f - c) / 2.0f;

根据this nice link to the ColorMatrix Guide,其余的矩阵代码似乎没问题。

注意:我对 c 的范围有误!!c 的值不是绝对值,而是应该应用的因子!因此,要将对比度加倍,它应该是2f

注意2:您的代码对源和目标不太清楚;并且由于您正在使用轨迹栏动态更改对比度,因此在应用更改之前应该很清楚,

  • 必须始终从相同的原始位图计算中间结果。

  • 此外,应该如此初始化轨迹栏,以使其从值 1 开始。

  • 最后,为了减少对比度值应该转换为 0 < c < 1。

Nico 的建议c = 1+ value;适用于您的原始范围 -100 到 +100,初始值为 0,您的因子为 0.01f。

于 2014-05-26T09:14:16.570 回答