在游戏屏幕上找到一个红色矩形

时间:2012-02-23 18:19:17

标签: c# bitmap screen aforge

我知道有很多与此类似的主题,但没有一个对我正在搜索的内容有一个确切的答案,所以如果有人知道的话,我也是用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,所以我可以为位图创建二维代码数组并找到红色代码并尝试ID,如果它是一个矩形但这个想法看起来非常复杂,已经在这里停留了4天现在

网上充满了关于此的信息,但是我越是陷入困境:(

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

1 个答案:

答案 0 :(得分:0)

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

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

步骤:

  1. 捕获游戏图像(您可以使用Graphics CopyFromScreen)。仅复制红色矩形可能的区域,以减少处理时间。
  2. 使用EmguCV MatchTemplate查找红色的位置 矩形。
  3. 将鼠标移动到该位置。
  4. 某些线程睡眠并重复1 ...
  5. 要处理图像,请使用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