将颜色 space 存储为 EmguCV 的变量

Storing colour space as a variable for EmguCV

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

我看到这些选项的类型被称为 "struct",其命名空间为:

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();
    }
}

如有任何帮助,我们将不胜感激。

问题是 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
            }
        }