Muryllo Posted July 8, 2016 at 05:46 PM Report #597597 Posted July 8, 2016 at 05:46 PM (edited) Boa tarde, estou desenvolvendo um aplicativo que procure uma imagem dentro de outra imagem e retorne a posição onde ela se encontra. Não estou conseguindo mais 'desenvolver' a partir da parte de procurar pois não vejo uma lógica que não seja tão complexa quanto procurar pixel por pixel em cada posição da imagem de forma a desenhar o quadrado ou retângulo da mesma. Isso está sendo muito complexo, portanto se alguém tiver uma solução alternativa para procurar uma imagem dentro de outra eu agradeço. Até agora o que eu desenvolvi foi isso : Imports System.Drawing.Graphics Public Class Form1 ' Public Shared Function Print_Screen() As Bitmap Try Dim Screen_Size As Size = New Size(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height) Dim Screen_BMP As New Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height) Dim Screen As Graphics = FromImage(Screen_BMP) Screen.CopyFromScreen(New Point(0, 0), New Point(0, 0), Screen_Size) System.GC.Collect() Return Screen_BMP Catch IMG_STREAM_ERROR As Exception Return Nothing End Try End Function ' Public Shared Function Get_Pixel(ByVal Posx As Integer, ByVal Posy As Integer, ByVal Screen_BMP As Bitmap) As Color Try Return Screen_BMP.GetPixel(Posx - 1, Posy - 1) Catch IMG_STREAM_ERROR As Exception Return Nothing End Try End Function ' Public Shared Function Find_Image64(ByVal Find As Bitmap, ByVal Image As Bitmap) As Point ' Dim Posh As Integer = 0 Dim Posv As Integer = 0 ' For Posy As Integer = 1 To Find.Size.Height For Posx As Integer = 1 To Find.Size.Width ' ' Next Next ' End Function ' Public Shared Function Find_Image32(ByVal Find As Bitmap, ByVal Image As Bitmap) As Point Try Return Find_Image64(Find, Image) Catch FUNC_RUNTIME_EXCEPTION As Exception Return New Point(0, 0) End Try End Function ' End Class A ideia é : através da função Print_Screen que retorna um bitmap da tela, procurar uma imagem "Image" que esteja dentro do print na tela. Como numa imagem de uma macieira procurar as maçãs e retornar suas coordenadas na imagem. Edited July 8, 2016 at 05:48 PM by Muryllo
Solution Muryllo Posted July 18, 2016 at 03:39 AM Author Solution Report #597691 Posted July 18, 2016 at 03:39 AM Problema resolvido, Solução : Imports System.Drawing Imports System.Windows.Forms Imports System.Drawing.Imaging Imports System.Drawing.Graphics Imports System.Runtime.InteropServices ' Public Class Sniffer ' Public Declare Auto Function Mouse_Event Lib "user32.dll" Alias "mouse_event" (ByVal dwFlags As Int32, ByVal dX As Int32, ByVal dY As Int32, ByVal cButtons As Int32, ByVal dwExtraInfo As Int32) As Boolean ' Public Const MOUSEEVENTF_LEFTDOWN = &H2 Public Const MOUSEEVENTF_LEFTUP = &H4 Public Const MOUSEEVENTF_RIGHTDOWN = &H8 Public Const MOUSEEVENTF_RIGHTUP = &H10 ' Public Sub Mouse_Click(ByVal Position As Point) Try Cursor.Position = Position Mouse_Event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) Mouse_Event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) Catch NTSTATUS As Exception End Try End Sub ' Public Shared Function Print_Screen() As Bitmap Try Dim Screen_Size As Size = New Size(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height) Dim Screen_BMP As New Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height) Dim Screen As Graphics = FromImage(Screen_BMP) Screen.CopyFromScreen(New Point(0, 0), New Point(0, 0), Screen_Size) System.GC.Collect() Return Screen_BMP Catch IMG_STREAM_ERROR As Exception Return Nothing End Try End Function ' Public Shared Function Get_Pixel(ByVal Posx As Integer, ByVal Posy As Integer, ByVal Screen_BMP As Bitmap) As Color Try Return Screen_BMP.GetPixel(Posx - 1, Posy - 1) Catch IMG_STREAM_ERROR As Exception Return Nothing End Try End Function ' Public Shared Function Return_Vector(ByVal Image As Bitmap) As Color() Try Dim Pixel_Matrix As New List(Of Color) For Posy As Integer = 1 To Image.Size.Height For Posx As Integer = 1 To Image.Size.Width Pixel_Matrix.Add(Get_Pixel(Posx, Posy, Image)) Next Next System.GC.Collect() Return Pixel_Matrix.ToArray() Catch IMG_STREAM_ERROR As Exception Return Nothing End Try End Function ' Public Shared Function Find_Image32(ByVal Find As Bitmap, ByVal Image As Bitmap) As Point Try Return Finder.Img_Contains(Find, Image) Catch FUNC_RUNTIME_EXCEPTION As Exception Return New Point(0, 0) End Try End Function ' Public Shared Function Find_Image64(ByVal Find As Bitmap, ByVal Image As Bitmap) As Point Try Return Find_Image32(Find, Image) Catch FUNC_RUNTIME_EXCEPTION As Exception Return New Point(0, 0) End Try End Function ' Public Shared Function Load_Image64(ByVal Path As String) As Bitmap Try Return Bitmap.FromFile(Path) Catch LOAD_FILE_EXCEPTION As Exception Return Nothing End Try End Function ' End Class Imports System.Drawing Imports System.Windows.Forms Imports System.Drawing.Imaging Imports System.Drawing.Graphics Imports System.Runtime.InteropServices ' Public Class Finder ' Public Shared Function Img_Contains(src As Bitmap, ByRef bmp As Bitmap) As Point If src Is Nothing OrElse bmp Is Nothing Then Return Nothing If src.Width = bmp.Width AndAlso src.Height = bmp.Height Then If src.GetPixel(0, 0) = bmp.GetPixel(0, 0) Then Return New Point(0, 0) Else Return Nothing End If ElseIf src.Width < bmp.Width OrElse src.Height < bmp.Height Then Return Nothing End If Dim sr As New Rectangle(0, 0, src.Width, src.Height) Dim br As New Rectangle(0, 0, bmp.Width, bmp.Height) Dim srcLock As BitmapData = src.LockBits(sr, Imaging.ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb) Dim bmpLock As BitmapData = bmp.LockBits(br, Imaging.ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb) Dim sStride As Integer = srcLock.Stride Dim bStride As Integer = bmpLock.Stride Dim srcSz As Integer = sStride * src.Height Dim bmpSz As Integer = bStride * bmp.Height Dim srcBuff(srcSz) As Byte Dim bmpBuff(bmpSz) As Byte Marshal.Copy(srcLock.Scan0, srcBuff, 0, srcSz) Marshal.Copy(bmpLock.Scan0, bmpBuff, 0, bmpSz) bmp.UnlockBits(bmpLock) src.UnlockBits(srcLock) Dim x, y, x2, y2, sx, sy, bx, by, sw, sh, bw, bh As Integer Dim r, g, b As Byte Dim p As Point = Nothing bw = bmp.Width bh = bmp.Height sw = src.Width - bw sh = src.Height - bh bx = 0 : by = 0 For y = 0 To sh sy = y * sStride For x = 0 To sw sx = sy + x * 3 r = srcBuff(sx + 2) g = srcBuff(sx + 1) b = srcBuff(sx) If r = bmpBuff(2) AndAlso g = bmpBuff(1) AndAlso b = bmpBuff(0) Then p = New Point(x, y) For y2 = 0 To bh - 1 by = y2 * bStride For x2 = 0 To bw - 1 bx = by + x2 * 3 sy = (y + y2) * sStride sx = sy + (x + x2) * 3 r = srcBuff(sx + 2) g = srcBuff(sx + 1) b = srcBuff(sx) If Not (r = bmpBuff(bx + 2) AndAlso g = bmpBuff(bx + 1) AndAlso b = bmpBuff(bx)) Then p = Nothing sy = y * sStride Exit For End If Next If p = Nothing Then Exit For Next End If If p <> Nothing Then Exit For Next If p <> Nothing Then Exit For Next bmpBuff = Nothing srcBuff = Nothing Return p End Function ' End Class
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now