·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> 用C#调用Matlab图像处理自制QQ游戏2D桌球瞄准器

用C#调用Matlab图像处理自制QQ游戏2D桌球瞄准器

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

平时不怎么玩游戏,有时消遣就玩玩QQ里的2D桌球,但是玩的次数少,不能像骨灰级玩家一样百发百中,肿么办呢?于是某天突发奇想,决定自己也来做个“外挂”。说是外挂,其实只是一个瞄准器,毕竟外挂是修改别人的软件,有点违法的意思,况且自己还没有能力去那么做,所以自己还是弄个瞄准器,做做弊,过下小瘾,同时也提高一下自己的编程能力。

起初(也就是半年前),自己尝试做一个瞄准器的初始版本,用C#做,想法很简单:

Step1.把鼠标移到洞口,获取鼠标位置;

Step2.将鼠标放到要击打的球的圆心上,获取鼠标当前位置

Step3.根据进球时三点共线的原则按照球的半径自动将鼠标移动到准确的击球点。

示意图如下:

 

于是当初就按照这个想法做了,开始给自己做了个C#版,调用Windows API中的GetDesktopWindow,GetWindowDC,SetCursorPos三个函数,经过简单的数学运算,就基本实现了功能。代码如下:

 

Csharp代码 
  1. using System.Drawing;  
  2. using System.Windows.Forms;  
  3. using System.Windows;  
  4. using System.Runtime.InteropServices;  
  5. using System;  
  6.   
  7. namespace TaiqiuGua  
  8. {  
  9.   
  10.     public partial class Form1 : Form  
  11.     {  
  12.         const int ra=25;  
  13.         [DllImport("user32.dll")]  
  14.         static extern IntPtr GetDesktopWindow();  
  15.         [DllImport("user32.dll")]  
  16.         static extern IntPtr GetWindowDC(IntPtr hWnd);  
  17.         [DllImport("user32.dll")]  
  18.         static extern bool SetCursorPos(int X, int Y);  
  19.   
  20.         public Form1()  
  21.         {  
  22.             InitializeComponent();  
  23.         }  
  24.   
  25.         Point startP;  
  26.         Point endP;  
  27.           
  28.         PRivate void Form1_KeyDown(object sender, KeyEventArgs e)  
  29.         {  
  30.             switch(e.KeyData)  
  31.             {  
  32.                 case Keys.F1:  
  33.                     startP=Control.MousePosition;  
  34.                     break;  
  35.                 case Keys.F2:  
  36.                     endP = Control.MousePosition;  
  37.                     break;  
  38.                 case Keys.D1:  
  39.                     int x1 = (int)(endP.X + ra * ((endP.X - startP.X) / Math.Sqrt((endP.X - startP.X) * (endP.X - startP.X) + (endP.Y - startP.Y) * (endP.Y - startP.Y))));  
  40.                     int y1 = (int)(endP.Y + ra * ((endP.Y - startP.Y) / Math.Sqrt((endP.X - startP.X) * (endP.X - startP.X) + (endP.Y - startP.Y) * (endP.Y - startP.Y))));  
  41.                     SetCursorPos(x1, y1);  
  42.                     break;  
  43.                 case Keys.D2:  
  44.                     int x2 = (int)(endP.X - ra * ((-endP.X + startP.X) / Math.Sqrt((-endP.X + startP.X) * (-endP.X + startP.X) + (endP.Y - startP.Y) * (endP.Y - startP.Y))));  
  45.                     int y2 = (int)(endP.Y + ra * ((endP.Y - startP.Y) / Math.Sqrt((-endP.X + startP.X) * (-endP.X + startP.X) + (endP.Y - startP.Y) * (endP.Y - startP.Y))));  
  46.                     SetCursorPos(x2, y2);  
  47.                     break;  
  48.                 case Keys.D3:                      
  49.                     int x3 = (int)(endP.X + ra * ((endP.X - startP.X) / Math.Sqrt((endP.X - startP.X) * (endP.X - startP.X) + (-endP.Y + startP.Y) * (-endP.Y + startP.Y))));  
  50.                     int y3 = (int)(endP.Y - ra * ((-endP.Y + startP.Y) / Math.Sqrt((endP.X - startP.X) * (endP.X - startP.X) + (-endP.Y + startP.Y) * (-endP.Y + startP.Y))));  
  51.                     SetCursorPos(x3, y3);  
  52.                     break;  
  53.                 case Keys.D4:  
  54.                     int x4 = (int)(endP.X - ra * ((-endP.X + startP.X) / Math.Sqrt((-endP.X + startP.X) * (-endP.X + startP.X) + (-endP.Y + startP.Y) * (-endP.Y + startP.Y))));  
  55.                     int y4 = (int)(endP.Y - ra * ((-endP.Y + startP.Y) / Math.Sqrt((-endP.X + startP.X) * (-endP.X + startP.X) + (-endP.Y + startP.Y) * (-endP.Y + startP.Y))));  
  56.                     SetCursorPos(x4, y4);  
  57.                     break;  
  58.             }  
  59.             GC.Collect();  
  60.         }  
  61.     }  
  62. }  

使用时,只需要激活瞄准器窗口,按F1,F2获取鼠标位置,再根据洞口位置分别选按1、2、3、4数字键就行。

经过N次试验,成功率还挺高,只是有时候手动放置鼠标到被击打球圆心会出现误差,导致击球不准,当然,后来我赢了很多场比赛(嘿嘿,有点不道德!)。

再后来,又用C写了一遍,给同学用了。代码如下:

Cpp代码 
  1. #include <windows.h>  
  2. #include <math.h>  
  3. int ra=26;  
  4. int flag=0;  
  5. POINT startP,endP;  
  6. int x5,y5,x2,y2,x3,y3,x4,y4;  
  7. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;  
  8.   
  9. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow)  
  10. {  
  11.     static TCHAR szAppName[] = TEXT ("GUA") ;  
  12.     HWND hwnd ;  
  13.     MSG msg ;  
  14.     WNDCLASS wndclass ;  
  15.     wndclass.style = CS_HREDRAW | CS_VREDRAW ;  
  16.     wndclass.lpfnWndProc = WndProc ;  
  17.     wndclass.cbClsExtra = 0 ;  
  18.     wndclass.cbWndExtra = 0 ;  
  19.     wndclass.hInstance = hInstance ;  
  20.     wndclass.hIcon = LoadIcon (NULL, IDI_application) ;  
  21.     wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;  
  22.     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;  
  23.     wndclass.lpszMenuName = NULL ;  
  24.     wndclass.lpszClassName = szAppName ;  
  25.     if (!RegisterClass (&wndclass))  
  26.     {  
  27.         MessageBox ( NULL, TEXT ("Program requires Windows NT!"),  
  28.             szAppName, MB_ICONERROR) ;  
  29.         return 0 ;  
  30.     }  
  31.         hwnd = CreateWindow (szAppName, TEXT ("Programmed By DC"),  
  32.             WS_OVERLAPPEDWINDOW,  
  33.             CW_USEDEFAULT, CW_USEDEFAULT,  
  34.             CW_USEDEFAULT, CW_USEDEFAULT,  
  35.             NULL, NULL, hInstance, NULL) ;  
  36.         ShowWindow (hwnd, iCmdShow) ;  
  37.         SetForegroundWindow(hwnd);  
  38.         MoveWindow(hwnd,100,100,200,200,TRUE);  
  39.         UpdateWindow (hwnd) ;  
  40.         while (GetMessage (&msg, NULL, 0, 0))  
  41.         {  
  42.             TranslateMessage (&msg) ;  
  43.             DispatchMessage (&msg) ;  
  44.         }         
  45.         return msg.wParam ;  
  46.     }  
  47.   
  48. void Draw(HWND hwnd,LPCSTR lpString)  
  49. {  
  50.     HDC hdc ;    
  51.     PAINTSTRUCT ps ;    
  52.     RECT rect ;   
  53.     hdc = BeginPaint (hwnd, &ps) ;    
  54.     GetClientRect (hwnd, &rect) ;    
  55.     DrawText (hdc, lpString, -1, &rect,  DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;    
  56.     EndPaint (hwnd, &ps) ;   
  57.     ReleaseDC(hwnd,hdc);  
  58. }  
  59.   
  60. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam)  
  61. {  
  62.     HBRUSH hBrush ;  
  63.     HDC hdc ;  
  64.     PAINTSTRUCT ps ;  
  65.     RECT rc ;  
  66.     switch (message)  
  67.     {  
  68.         case WM_CREATE:  
  69.             return 0 ;  
  70.         case WM_PAINT :  
  71.             return 0 ;  
  72.         case WM_KEYDOWN:    
  73.             switch (wParam)    
  74.            {    
  75.             case VK_F1:    
  76.                     GetCursorPos(&startP);  
  77.                     flag=1;  
  78.                     InvalidateRect (hwnd, NULL, TRUE) ;  
  79.                     Draw(hwnd,"第1点已锁定!");  
  80.                     break ;    
  81.             case VK_F2:    
  82.                     GetCursorPos(&endP);  
  83.                     flag=2;  
  84.                     InvalidateRect (hwnd, NULL, TRUE) ;  
  85.                     Draw(hwnd,"第2点已锁定!");  
  86.                     break ;   
  87.             case 0x31:   
  88.                     x5 = (int)(endP.x + ra * ((endP.x - startP.x) / sqrt((endP.x - startP.x) * (endP.x - startP.x) + (endP.y - startP.y) * (endP.y - startP.y))));  
  89.                     y5 = (int)(endP.y + ra * ((endP.y - startP.y) / sqrt((endP.x - startP.x) * (endP.x - startP.x) + (endP.y - startP.y) * (endP.y - startP.y))));  
  90.                     SetCursorPos(x5, y5);  
  91.                     break;  
  92.             case 0x32:   
  93.                     x2 = (int)(endP.x - ra * ((-endP.x + startP.x) / sqrt((-endP.x + startP.x) * (-endP.x + startP.x) + (endP.y - startP.y) * (endP.y - startP.y))));  
  94.                     y2 = (int)(endP.y + ra * ((endP.y - startP.y) / sqrt((-endP.x + startP.x) * (-endP.x + startP.x) + (endP.y - startP.y) * (endP.y - startP.y))));  
  95.                     SetCursorPos(x2, y2);  
  96.                     break;  
  97.             case 0x33:                      
  98.                     x3 = (int)(endP.x + ra * ((endP.x - startP.x) / sqrt((endP.x - startP.x) * (endP.x - startP.x) + (-endP.y + startP.y) * (-endP.y + startP.y))));  
  99.                     y3 = (int)(endP.y - ra * ((-endP.y + startP.y) / sqrt((endP.x - startP.x) * (endP.x - startP.x) + (-endP.y + startP.y) * (-endP.y + startP.y))));  
  100.                     SetCursorPos(x3, y3);  
  101.                     break;  
  102.             case 0x34:  
  103.                     x4 = (int)(endP.x - ra * ((-endP.x + startP.x) / sqrt((-endP.x + startP.x) * (-endP.x + startP.x) + (-endP.y + startP.y) * (-endP.y + startP.y))));  
  104.                     y4 = (int)(endP.y - ra * ((-endP.y + startP.y) / sqrt((-endP.x + startP.x) * (-endP.x + startP.x) + (-endP.y + startP.y) * (-endP.y + startP.y))));  
  105.                     SetCursorPos(x4, y4);  
  106.                     break;  
  107.             }  
  108.             return 0;  
  109.         case WM_SIZE :   
  110.             if(flag==1)  
  111.             {  
  112.                 Draw(hwnd,"第1点已锁定!");  
  113.             }  
  114.             else if(flag==2)  
  115.             {  
  116.                 Draw(hwnd,"第2点已锁定!");  
  117.             }  
  118.             else  
  119.             {InvalidateRect (hwnd, NULL, TRUE) ; }  
  120.             return 0 ;  
  121.         case WM_KILLFOCUS:  
  122.             InvalidateRect (hwnd, NULL, TRUE) ;  
  123.             hdc = BeginPaint (hwnd, &ps) ;  
  124.             GetClientRect (hwnd, &rc) ;  
  125.             hBrush = CreateSolidBrush ( RGB(255,0,0) ) ;  
  126.             FillRect (hdc, &rc, hBrush) ;  
  127.             EndPaint (hwnd, &ps) ;  
  128.             ReleaseDC(hwnd,hdc);  
  129.             DeleteObject (hBrush) ;  
  130.             return 0;  
  131.         case WM_SETFOCUS:  
  132.             InvalidateRect (hwnd, NULL, TRUE) ;  
  133.             if(flag==1)  
  134.             {  
  135.                 Draw(hwnd,"第1点已锁定!");  
  136.             }  
  137.             else if(flag==2)  
  138.             {  
  139.                 Draw(hwnd,"第2点已锁定!");  
  140.             }  
  141.             return 0;  
  142.         case WM_DESTROY :  
  143.             PostQuitMessage (0) ;  
  144.             return 0 ;  
  145.     }  
  146.     return DefWindowProc(hwnd, message, wParam, lParam) ;  
  147. }  

 

但是问题还存在,就是手动找圆心太麻烦,一般用触摸板比鼠标方便,但是仍然很不智能,于是一直想着用图像处理的方法去自动找圆心。

这几天在做数模,经常用到Matlab,刚做了一道要处理图像的题,正好想起这个问题来,于是,搁置的瞄准器继续开始完善了。

很快就有了思路,通过C#截图,然后Matlab进行图像滤波,灰度化,二值化以及边缘提取,然后进行圆曲线拟合,最后找到圆心,返回到C#中使用,代替手动找点。

首先,我用Matlab写了个函数:

Html代码 
  1. function [x,y]=findcenter()  
  2. %close all,clear,clc  
  3. format short  
  4. a=imread('E:\360data\重要数据\桌面\test.bmp');  
  5. b=rgb2gray(a);%转化为灰度图像  
  6. %figure;imshow(b)  
  7. b=filter2(fspecial('average',1),b)/255;  
  8. %b=medfilt2(b);%中值滤波  
  9. level=graythresh(b);%自动获取灰度图片的阈值  
  10. c=im2bw(b,level);%二值化  
  11. %figure;imshow(c)  
  12. bw=edge(c,'canny');  
  13. bw1=~bw;%取反,黑变白,白变黑  
  14. %figure;imshow(bw1)  
  15. %figure;imshow(bw1)  
  16. [yf,xf]=find(bw1==0);  
  17. xmin=min(xf);  
  18. xmax=max(xf);  
  19. ymin=min(yf);  
  20. ymax=max(yf);  
  21. %cirPx=[xmin;(xmax-xmin)/2;(xmax-xmin)/2;xmax]  
  22. %cirPy=[(ymax-ymin)/2;ymin;ymax;(ymax-ymin)/2]  
  23. %fitellipse(cirPx,cirPy)  
  24. centerX=(xmax+xmin)/2;  
  25. centerY=(ymax+ymin)/2;  
  26. ra=(ymax-ymin)/2;  
  27. x=centerX;y=centerY;  
  28. %hold on  
  29. %x=0:size(bw1,2);  
  30. %degree=[0:0.01:pi*2];  
  31. %degree=[0:0.01:pi*2];  
  32. %plot(ra*cos(degree)+centerX,ra*sin(degree)+centerY,'r-');  
  33. %plot(centerX,centerY,'r+');  

然后用Matlab2010b里的deploytool导出.net能使用的程序集dll文件(不知道为什么malab2009b在build时出现.net framework相关的错误),通过C#添加引用,调用其返回的参数,成功完成自动拾取圆心。

 

 

改进后代码如下:

Csharp代码 
  1. using System.Drawing;  
  2. using System.Windows.Forms;  
  3. using System.Windows;  
  4. using System.Runtime.InteropServices;  
  5. using System;  
  6. using MathWorks.MATLAB.NET.Arrays;  
  7. using MathWorks.MATLAB.NET.Utility;  
  8. using findcenter;  
  9.   
  10. namespace TaiqiuGua  
  11. {  
  12.   
  13.     public partial class Form1 : Form  
  14.     {  
  15.         const int ra=25;  
  16.         [DllImport("user32.dll")]  
  17.         static extern IntPtr GetDesktopWindow();  
  18.         [DllImport("user32.dll")]  
  19.         static extern IntPtr GetWindowDC(IntPtr hWnd);  
  20.         [DllImport("user32.dll")]  
  21.         static extern bool SetCursorPos(int X, int Y);  
  22.   
  23.         public Form1()  
  24.         {  
  25.             InitializeComponent();  
  26.         }  
  27.   
  28.         Point startP,startP1;  
  29.         Point endP,endP1;  
  30.           
  31.         private void Form1_KeyDown(object sender, KeyEventArgs e)  
  32.         {  
  33.             switch(e.KeyData)  
  34.             {  
  35.                 case Keys.F1:  
  36.                     startP=Control.MousePosition;  
  37.                     break;  
  38.                 case Keys.F5:  
  39.                     endP = Control.MousePosition;  
  40.                     break;  
  41.                 case Keys.F2:  
  42.                     startP1 = Control.MousePosition;  
  43.                     break;  
  44.                 case Keys.F3:  
  45.                     endP1 = Control.MousePosition;  
  46.                     break;  
  47.                 case Keys.D1:  
  48.                     int x1 = (int)(endP.X + ra * ((endP.X - startP.X) / Math.Sqrt((endP.X - startP.X) * (endP.X - startP.X) + (endP.Y - startP.Y) * (endP.Y - startP.Y))));  
  49.                     int y1 = (int)(endP.Y + ra * ((endP.Y - startP.Y) / Math.Sqrt((endP.X - startP.X) * (endP.X - startP.X) + (endP.Y - startP.Y) * (endP.Y - startP.Y))));  
  50.                     SetCursorPos(x1, y1);  
  51.                     break;  
  52.                 case Keys.D2:  
  53.                     int x2 = (int)(endP.X - ra * ((-endP.X + startP.X) / Math.Sqrt((-endP.X + startP.X) * (-endP.X + startP.X) + (endP.Y - startP.Y) * (endP.Y - startP.Y))));  
  54.                     int y2 = (int)(endP.Y + ra * ((endP.Y - startP.Y) / Math.Sqrt((-endP.X + startP.X) * (-endP.X + startP.X) + (endP.Y - startP.Y) * (endP.Y - startP.Y))));  
  55.                     SetCursorPos(x2, y2);  
  56.                     break;  
  57.                 case Keys.D3:                      
  58.                     int x3 = (int)(endP.X + ra * ((endP.X - startP.X) / Math.Sqrt((endP.X - startP.X) * (endP.X - startP.X) + (-endP.Y + startP.Y) * (-endP.Y + startP.Y))));  
  59.                     int y3 = (int)(endP.Y - ra * ((-endP.Y + startP.Y) / Math.Sqrt((endP.X - startP.X) * (endP.X - startP.X) + (-endP.Y + startP.Y) * (-endP.Y + startP.Y))));  
  60.                     SetCursorPos(x3, y3);  
  61.                     break;  
  62.                 case Keys.D4:  
  63.                     int x4 = (int)(endP.X - ra * ((-endP.X + startP.X) / Math.Sqrt((-endP.X + startP.X) * (-endP.X + startP.X) + (-endP.Y + startP.Y) * (-endP.Y + startP.Y))));  
  64.                     int y4 = (int)(endP.Y - ra * ((-endP.Y + startP.Y) / Math.Sqrt((-endP.X + startP.X) * (-endP.X + startP.X) + (-endP.Y + startP.Y) * (-endP.Y + startP.Y))));  
  65.                     SetCursorPos(x4, y4);  
  66.                     break;  
  67.                 case Keys.F4:  
  68.                     //Graphics g1 = pictureBox1.CreateGraphics();  
  69.                     //g1.CopyFromScreen(startP1.X,startP1.Y,0,0,new Size(endP1.X-startP1.X,endP1.Y-startP1.Y));  
  70.                     int w=endP1.X - startP1.X;  
  71.                     int h=endP1.Y - startP1.Y;  
  72.                     Bitmap bmSave = new Bitmap(w,h);  
  73.                     Graphics g=Graphics.FromImage(bmSave);  
  74.                     g.CopyFromScreen(startP1.X,startP1.Y,0,0,new Size(w,h),CopyPixelOperation.SourceCopy);  
  75.                     bmSave.Save(@"E:\360data\重要数据\桌面\test.bmp");  
  76.                     g.Dispose();  
  77.                     //g1.Dispose();  
  78.                     bmSave.Dispose();  
  79.                     findcenter.Class1 f = new findcenter.Class1();       
  80.                     MWArray centerx = f.findcenter();  
  81.                     MWArray centery = f.findy();  
  82.                     double[,] x = (double[,])centerx.ToArray();  
  83.                     double[,] y = (double[,])centery.ToArray();  
  84.                     SetCursorPos((int)(x[0, 0] + startP1.X), (int)(y[0, 0] + startP1.Y));  
  85.                     //int [] d=center.Dimensions;                      
  86.                     //int x=int.Parse((center[1, 1]).ToString());  
  87.                     //int y= int.Parse((center[1, 2]).ToString());  
  88.                     //MessageBox.Show((y[0,0]).ToString());  
  89.                     f.Dispose();  
  90.                     break;  
  91.             }  
  92.             GC.Collect();  
  93.         }  
  94.   
  95.         private void Form1_Activated(object sender, EventArgs e)  
  96.         {  
  97.             //this.BackColor = Color.Red;  
  98.         }  
  99.   
  100.         private void Form1_Deactivate(object sender, EventArgs e)  
  101.         {  
  102.             //this.BackColor = Color.Blue;  
  103.         }  
  104.   
  105.     }  
  106. }  

经试验,成功率也很高,偶尔出现不准(估计是边缘提取和计算精度的问题),但是大多数偏差可以手动修正。

到此为止,改进版全部完成。希望以后继续改进,更加智能化。C# 真是码农的利器啊!