1

我知道有很多与此类似的主题,但没有一个对我正在搜索的内容有一个准确的答案,所以如果有人知道,请告诉我,而且我正在用 C# 做。

你们可能都知道(FPS)游戏,在分辨率为 1024x768 的游戏屏幕上,我需要找到一个红色矩形(这是敌人)并将鼠标移到它上面。所以我的主要问题是找到那个红色矩形。好的,这就是我到目前为止所尝试的:

我已经尝试了 AForge 并且内存不足:

ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(0);
TemplateMatch[] matchings = tm.ProcessImage(image1.Clone(r,
        System.Drawing.Imaging.PixelFormat.Format24bppRgb), image2);

我使用 CopyfromScreen 创建了 image1,而 image2 是我拥有的模板。

我已经尝试过 LockBits,所以我可以为位图创建二维码数组并找到红色的代码并尝试识别它是否是矩形,但这个想法似乎非常复杂,现在在这里停留了 4 天。

网络上充满了这方面的信息,但我越深入,我就越感到困惑:(

无论如何,请在这里帮助我:

4

1 回答 1

0

好吧,首先我需要说这可能会很慢,所以如果红色矩形移动得很快,你需要一些其他的解决方案。C++、CUDA 等...

第一:保存红色矩形的图像。定义红色矩形的可能位置区域。

脚步:

  1. 捕获游戏图像(您可以使用 Graphics CopyFromScreen)。仅复制可能是红色矩形的区域,以减少处理时间。
  2. 使用 EmguCV MatchTemplate 找到红色矩形的位置。
  3. 将鼠标移动到该位置。
  4. 一些线程睡眠并重复 1 ......

处理图像使用EmguCV
控制鼠标使用MouseKeyboardActivityMonitor

加速说明:EmguCV 现在有一些 CUDA 支持,所以你可以尝试使用 CUDA 版本的方法。

        //You can check if a 8bpp image is enough for the comparison, 
        //since it will be much more faster. Otherwise, use 24bpp images.
        //Bitmap pattern = "YOUR RED RECTANGLE IMAGE HERE!!"
        Point location = Point.Empty;
        //source is the screen image !!
        Image<Bgr, Byte> srcBgr = new Image<Bgr, Byte>(source);
        Image<Bgr, Byte> templateBgr = new Image<Bgr, Byte>(pattern);

        Image<Gray, Byte> src;
        Image<Gray, Byte> template;
        src = srcBgr.Convert<Gray, Byte>();
        template = templateBgr.Convert<Gray, Byte>();

        Image<Gray, float> matchFloat;
        matchFloat = src.MatchTemplate(template, TM_TYPE.CV_TM_CCOEFF_NORMED);
        if (debug)
        {
            //matchFloat.Save(@"match.png");
            //Process.Start(@"D:\match.png");
        }
        //Gets the max math value
        double max;
        double[] maxs, mins;
        Point[] maxLocations, minLocations;
        matchFloat.MinMax(out mins, out maxs, out minLocations, out maxLocations);
        max = maxs[0];

        if (max > threshold)
        {
        location = maxLocations[0];
        //Point the mouse... Do your stuff
于 2014-03-07T17:23:20.993 回答