将色彩空间存储为EmguCV的变量

时间:2019-01-29 19:05:45

标签: c# emgucv

我正在尝试为用户提供选择,以切换检测是使用Bgr还是使用灰色颜色空间进行优化。

我看到这些选项的类型被称为“结构”,其名称空间为:

Emgu.CV.Structure.Gray

or

Emgu.CV.Structure.Gray

这是我当前的检测代码,您可以看到它目前仅使用“灰色”选项。

while (!found)
{
    timeTaken = Stopwatch.StartNew();

    window = new Image<Gray, byte>(WindowOperations.TakeScreenshot(focusWindow));

    using (var result = window.MatchTemplate(detect, TemplateMatchingType.CcoeffNormed))
    {
        result.MinMax(out var minValues, out var maxValues, out var minLocations, out var maxLocations);

        if (!(maxValues[0] > watchmanData.Profile.SettingsProfile.AccuracyDecimal)) continue;

        found = true;
        timeTaken.Stop();
    }
}

理想情况下,我想要这样的东西(如果可能):

while (!found)
{
    timeTaken = Stopwatch.StartNew();

    colourSpace = userChoice;

    window = new Image<colourSpace, byte>(WindowOperations.TakeScreenshot(focusWindow));

    using (var result = window.MatchTemplate(detect, TemplateMatchingType.CcoeffNormed))
    {
        result.MinMax(out var minValues, out var maxValues, out var minLocations, out var maxLocations);

        if (!(maxValues[0] > watchmanData.Profile.SettingsProfile.AccuracyDecimal)) continue;

        found = true;
        timeTaken.Stop();
    }
}

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

问题在于c#不允许您以想要的方式使用变量类型参数。但是您可以将代码分成这样的类型化方法:

        public void Process(bool useGray)
        {
            if (useGray)
            {
                DoStuff<Gray>(new Image<Gray, byte>(100, 100), new Image<Gray, byte>(10, 10));
            }
            else
            {
                DoStuff<Bgr>(new Image<Bgr, byte>(100, 100), new Image<Bgr, byte>(10, 10));
            }
        }

        public void DoStuff<TColor>(Image<TColor, byte> window, Image<TColor, byte> pattern)
            where TColor : struct, IColor
        {

            using (var result = window.MatchTemplate(pattern, TemplateMatchingType.CcoeffNormed))
            {
                result.MinMax(out var minValues, out var maxValues, out var minLocations, out var maxLocations);

                //... evaluate matching
            }
        }