AForge图像处理例外图像处理、AForge

2023-09-06 11:07:09 作者:淡看云卷云舒

我想使用 AForge 图书馆看到的这个小例子,以找到一个图像内的形象,code表示例如工程perfectlly ......但要完成它需要永远的作仪(找到1920x1080px 50x50px),所以我想调整图像以提升运行速度...

这是这样的:

 昏暗sourceImage为位图= Bitmap.FromFile(C:\ 1.bmp)
昏暗的模板为位图= Bitmap.FromFile(C:\ 2.bmp)
 

要这个东西:

 昏暗sourceImage为位图= ResizedBitmap1
昏暗的模板为位图= ResizedBitmap2
 
faceforge人脸合成软件下载 faceforge免费版下载 当易网

现在的问题是,当我尝试使用方法与我调整大小后的位图我得到一个异常的:源此堆栈跟踪的不支持的像素格式为:

  

AForge.Imaging.UnsupportedImageFormatException是未处理   的HResult = -2147024809消息=源不支持的像素格式   或模板图像。来源= AForge.Imaging堆栈跟踪:          恩AForge.Imaging.ExhaustiveTemplateMatching.ProcessImage(位图图像​​,   位图模板,矩形searchZone)          恩AForge.Imaging.ExhaustiveTemplateMatching.ProcessImage(位图图像​​,   位图模板)          恩WindowsApplication9.Form1.Test()EN C:\用户\ administrador \文档\ Visual Studio中   2013 \项目\ WindowsApplication9 \ WindowsApplication9 \ Form1.vb的:拉利内阿   22          恩WindowsApplication9.Form1._Lambda $ __ 1(对象A0,A1 EventArgs的)恩C:\用户\ administrador \文档\ Visual Studio中   2013 \项目\ WindowsApplication9 \ WindowsApplication9 \ Form1.Designer.vb:拉利内阿   0          恩System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(obj对象)          恩System.Threading.ExecutionContext.RunInternal(执行上下文   执行上下文,ContextCallback回调,对象的状态,布尔   preserveSyncCtx)          恩System.Threading.ExecutionContext.Run(ExecutionContext中的ExecutionContext,ContextCallback回调,对象的状态,布尔   preserveSyncCtx)          恩System.Threading.ExecutionContext.Run(ExecutionContext中的ExecutionContext,ContextCallback回调,对象的状态)          恩System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry   TME)          恩System.Windows.Forms.Control.InvokeMarshaledCallbacks()          恩System.Windows.Forms.Control.WndProc(消息和M)          恩System.Windows.Forms.Form.WndProc(消息和M)          恩System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr的的HWND,味精的Int32,IntPtr的WPARAM,IntPtr的LPARAM)          恩System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG和放大器; MSG)          恩System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr   dwComponentID,的Int32原因的Int32 pvLoopData)          恩System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(的Int32   因此,ApplicationContext的情况下)          恩System.Windows.Forms.Application.ThreadContext.RunMessageLoop(的Int32   因此,ApplicationContext的情况下)          恩Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()          恩Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()          恩Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(字符串[]   命令行)          恩WindowsApplication9.My.MyApplication.Main(字串[] args)EN 17d14f5c-a337-4978-8281-53493378c1071.vb:拉利内阿81          恩System.AppDomain._nExecuteAssembly(RuntimeAssembly组装,字串[] args)          恩Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()          恩System.Threading.ExecutionContext.RunInternal(执行上下文   执行上下文,ContextCallback回调,对象的状态,布尔   preserveSyncCtx)          恩System.Threading.ExecutionContext.Run(ExecutionContext中的ExecutionContext,ContextCallback回调,对象的状态,布尔   preserveSyncCtx)          恩System.Threading.ExecutionContext.Run(ExecutionContext中的ExecutionContext,ContextCallback回调,对象的状态)          恩System.Threading.ThreadHelper.ThreadStart()的InnerException:

所以似乎存在由位图类比,我回到我的 ResizeImage 的功能。

这是我使用的code

 进口AForge.Imaging
进口System.Drawing.Imaging

公共类Form1中

私人小组测试()处理MyBase.Shown

    桌面截图,1920×1080像素。解析度。
    昏暗DesktopScreenshoot作为新位图(C:\ 1.png)

    A片切边的截图,55x57像素。解析度。
    昏暗PartOfImageToFind作为新位图(C:\ 2.png)

    创建模板匹配算法的实例。
    昏暗sourceImage为位图= ResizeImage(DesktopScreenshoot,百分比:= 40.0R)'Bitmap.FromFile(C:\ 1.bmp)
    昏暗的模板为位图= ResizeImage(PartOfImageToFind,百分比:= 40.0R)'Bitmap.FromFile(C:\ 2.bmp)

    (设置相似性阈值92.1%)。
    昏暗的TM作为新ExhaustiveTemplateMatching(0.921F)
    找到所有的匹配与上面的相似规定。

    昏暗的匹配由于TemplateMatch()= tm.ProcessImage(sourceImage,模板)
    亮点中的匹配。

    昏暗的数据的BitmapData =
        sourceImage.LockBits(新的Rectangle(0,0,sourceImage.Width,sourceImage.Height)
                                           ImageLockMode.ReadWrite,sourceImage.PixelFormat)

    对于每个M作为TemplateMatch在的匹配

        Drawing.Rectangle(数据,m.Rectangle,Color.White)

        做点别的与之搭配
        的MessageBox.show(m.Rectangle.Location.ToString())

    接下来的M

    sourceImage.UnlockBits(数据)

结束小组

通过电力币

'''<总结>
'''由一个大小百分比大小调整图像。
'''< /总结>
'''< PARAM NAME =位图>表示的图像调整和LT; /参数>
'''< PARAM NAME =百分比>表示的百分比大小和LT; /参数>
'''<返回>位图< /回报>
专用功能ResizeImage(BYVAL [位图作为位图,
                             BYVAL%,因双人间)作为位图

    昏暗[宽度]作为整数=([位图] .WIDTH)\(100.0R /百分比)
    昏暗[高度]作为整数=([位图] .Height)\(100.0R /百分比)

    昏暗NewBitmap作为新位图(宽,高)

    将G作​​为图形= Graphics.FromImage(NewBitmap)
        g.DrawImage([位图],0,0,[宽度],[高度])
    结束使用

    返回NewBitmap

端功能

末级
 

解决方案

这个问题可能是这样的:

 昏暗NewBitmap作为新位图(宽,高)
 

对于像的PixelFormat 或分辨率的默认值可能是一些AForge无法处理(该文档会告诉他们,不能办理)。此外,扫了一眼SO链接,它的看起来(TL; DR)AForge有大小功能( ResizeBicubic ),这显然可以预料到产生一个位图,它可以处理。

忽略我的AR评论 - 我在想你想调整到最大或固定的H或W,这将需要扩展其他

I'm trying to use the algorithms of AForge library seen in this little example to find an image inside an image, the code of that example works perfectlly ...but to finish the comparission (find 50x50px in 1920x1080px) it takes forever, so I would like to resize the images to gain speed...

From this:

Dim sourceImage As Bitmap = Bitmap.FromFile("C:\1.bmp")
Dim template As Bitmap = Bitmap.FromFile("C:\2.bmp")

To this else:

Dim sourceImage As Bitmap = ResizedBitmap1
Dim template As Bitmap = ResizedBitmap2

The problem is that when I try to use the methods with my resized Bitmaps I get an exception of: Unsupported pixel format of the source with this StackTrace:

AForge.Imaging.UnsupportedImageFormatException was unhandled HResult=-2147024809 Message=Unsupported pixel format of the source or template image. Source=AForge.Imaging StackTrace: en AForge.Imaging.ExhaustiveTemplateMatching.ProcessImage(Bitmap image, Bitmap template, Rectangle searchZone) en AForge.Imaging.ExhaustiveTemplateMatching.ProcessImage(Bitmap image, Bitmap template) en WindowsApplication9.Form1.Test() en c:\users\administrador\documents\visual studio 2013\Projects\WindowsApplication9\WindowsApplication9\Form1.vb:línea 22 en WindowsApplication9.Form1._Lambda$__1(Object a0, EventArgs a1) en c:\users\administrador\documents\visual studio 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 callback, Object state, Boolean preserveSyncCtx) en System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) en System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) en System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme) en System.Windows.Forms.Control.InvokeMarshaledCallbacks() 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, IntPtr lparam) en System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) en System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) en System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) en System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) en 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 assembly, String[] args) en Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() en System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) en System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) en System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) en System.Threading.ThreadHelper.ThreadStart() InnerException:

So there seems to exist a difference between the Bitmaps instanced by the Bitmap Class than the Bitmaps that I return with my ResizeImage function.

This is the code that I'm using

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

解决方案

The problem may be with this:

Dim NewBitmap As New Bitmap(Width, Height)

The default values for things like PixelFormat or resolution may be something AForge cannot handle (the docs would tell what they can and cannot handle). Also, glancing at the SO link, it looks like (tl;dr) AForge has a Resize function (ResizeBicubic) which obviously might be expected to produce a bitmap it can handle.

Ignore my AR comment - I was thinking you wanted to resize to a Max or fixed H or W, which would require scaling the other.