我试图使用AForge库的算法在这个小例子中找到图像中的图像,该示例的代码完美地工作...but以完成比较(在1920x1080px中查找50x50px ),所以我想调整图像的大小以获得速度…
由此:
Dim sourceImage As Bitmap = Bitmap.FromFile("C:\1.bmp")
Dim template As Bitmap = Bitmap.FromFile("C:\2.bmp")对于其他方面:
Dim sourceImage As Bitmap = ResizedBitmap1
Dim template As Bitmap = ResizedBitmap2问题是,当我试图在调整大小的位图中使用这些方法时,我得到了一个例外:Unsupported pixel format of the source和这个StackTrace:
AForge.Imaging.UnsupportedImageFormatException未被处理 HResult=-2147024809 Message=Unsupported像素格式的源或模板图像。AForge.Imaging.ExhaustiveTemplateMatching.ProcessImage(Bitmap StackTrace: en StackTrace图像,位图模板,矩形searchZone) en StackTrace图像,位图模板)documents应用程序9.Form1.Test() en c:\user\ 2013\Projects\WindowsApplication9\WindowsApplication9\Form1.vb:línea \2013\Projects\WindowsApplication9\WindowsApplication9\Form1.vb:línea 22 en WindowsApplication9.Form1._Lambda$__1(Object a0,2013\Projects\WindowsApplication9\WindowsApplication9\Form1.Designer.vb:línea 0 en System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj) en System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback回调,对象状态,布尔preserveSyncCtx) en System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态,布尔preserveSyncCtx) en System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态( en System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme) en System.Windows.Forms.Control.InvokeMarshaledCallbacks() en en System.Windows.Forms.Control.WndProc(Message& m) en System.Windows.Forms.Form.WndProc(Message& m) en System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,( System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& ) en System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32原因,Int32 pvLoopData) en System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32原因,ApplicationContext上下文) en System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32原因,( Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() en Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() en Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) en WindowsApplication9.My.MyApplication.Main(String[] Args) en 17d14f5c-a337-4978-8281-53493378c1071.vb:línea 81 en System.AppDomain._nExecuteAssembly(RuntimeAssembly程序集,( Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() en System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback回调,对象状态,布尔preserveSyncCtx) en System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态,布尔preserveSyncCtx) en System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,Object state ) en System.Threading.ThreadHelper.ThreadStart(InnerException):
因此,Bitmap类实例的位图与我随ResizeImage函数返回的位图之间似乎存在差异。
这是我正在使用的代码
Imports AForge.Imaging
Imports System.Drawing.Imaging
Public Class Form1
Private Sub Test() Handles MyBase.Shown
' A Desktop Screenshot, 1920x1080 px. resolution.
Dim DesktopScreenshoot As New Bitmap("C:\1.png")
' A cutted piece of the screenshot, 55x57 px. resolution.
Dim PartOfImageToFind As New Bitmap("C:\2.png")
' create template matching algorithm's instance.
Dim sourceImage As Bitmap = ResizeImage(DesktopScreenshoot, Percent:=40.0R) ' Bitmap.FromFile("C:\1.bmp")
Dim template As Bitmap = ResizeImage(PartOfImageToFind, Percent:=40.0R) ' Bitmap.FromFile("C:\2.bmp")
' (set similarity threshold to 92.1%).
Dim tm As New ExhaustiveTemplateMatching(0.921F)
' find all matchings with specified above similarity.
Dim matchings As TemplateMatch() = tm.ProcessImage(sourceImage, template)
' highlight found matchings.
Dim data As BitmapData =
sourceImage.LockBits(New Rectangle(0, 0, sourceImage.Width, sourceImage.Height),
ImageLockMode.ReadWrite, sourceImage.PixelFormat)
For Each m As TemplateMatch In matchings
Drawing.Rectangle(data, m.Rectangle, Color.White)
' do something else with matching
MessageBox.Show(m.Rectangle.Location.ToString())
Next m
sourceImage.UnlockBits(data)
End Sub
' By Elektro
'
''' <summary>
''' Resizes an image by a size percentage.
''' </summary>
''' <param name="Bitmap">Indicates the image to resize.</param>
''' <param name="Percent">Indicates the percent size.</param>
''' <returns>Bitmap.</returns>
Private Function ResizeImage(ByVal [Bitmap] As Bitmap,
ByVal Percent As Double) As Bitmap
Dim [Width] As Integer = ([Bitmap].Width) \ (100.0R / Percent)
Dim [Height] As Integer = ([Bitmap].Height) \ (100.0R / Percent)
Dim NewBitmap As New Bitmap(Width, Height)
Using g As Graphics = Graphics.FromImage(NewBitmap)
g.DrawImage([Bitmap], 0, 0, [Width], [Height])
End Using
Return NewBitmap
End Function
End Class发布于 2014-05-10 22:59:43
问题可能在于:
Dim NewBitmap As New Bitmap(Width, Height)诸如PixelFormat或分辨率之类的东西的默认值可能是AForge无法处理的东西(文档会告诉它们可以和不能处理什么)。另外,浏览一下SO链接,看起来就像 (tl;dr) AForge有一个调整大小函数(ResizeBicubic),很明显,它可能会产生一个可以处理的位图。
忽略我的AR评论-我在想你想要调整大小到最大值或固定的H或W,这将需要缩放另一个。
https://stackoverflow.com/questions/23586979
复制相似问题