如何使用C#从图像中获取HSV值并将其显示到datagrid视图?

时间:2019-04-27 19:44:31

标签: c# rgb hsv

我有两个图片框作为输入和输出,我想将其从RGB转换为HSV 看起来像(左图为rgb,右图为hsv):

image on left is rgb, image on right is hsv

但在这种情况下,使用库Emgu.cv

我想使用

这样的手动C#计算代码进行应用程序转换
private double[] getHSV(double nilaiR, double nilaiG, double nilaiB)
{
    double[] hsv = new double[3];
    double rAksen = 0, gAksen = 0, bAksen = 0, delta = 0, cMax = 0, cMin = 0, nilaiH = 0, nilaiS = 0, nilaiV = 0;
    rAksen = nilaiR / 255;
    gAksen = nilaiG / 255;
    bAksen = nilaiB / 255;
    cMax = Math.Max(rAksen, Math.Max(gAksen, bAksen));
    cMin = Math.Min(rAksen, Math.Min(gAksen, bAksen));
    delta = cMax - cMin;
    //getH
    if (delta == 0)
    {
        nilaiH = 0;
    }
    else if (cMax == rAksen)
    {
        nilaiH = 60 * (((gAksen - bAksen) / delta) % 6);
    }
    else if (cMax == gAksen)
    {
        nilaiH = 60 * (((bAksen - rAksen) / delta) + 2);
    }
    else if (cMax == bAksen)
    {
        nilaiH = 60 * (((rAksen - gAksen) / delta) + 4);
    }
    //getS
    if (cMax == 0)
    {
        nilaiS = 0;
    }
    else
    {
        nilaiS = delta / cMax;
    }
    //getV
    nilaiV = cMax;
    hsv[0] = nilaiH < 0 ? Math.Round((360 + nilaiH), 2) : Math.Round(nilaiH, 2);
    hsv[1] = Math.Round((nilaiS * 100), 2);
    hsv[2] = Math.Round((nilaiV * 100), 2);
    return hsv;
}

但是如何通过单击处理按钮执行上面的代码,并在datagrid视图中显示HSV值?

如果您能帮助我,我将不胜感激。

0 个答案:

没有答案