本文介绍了AForge图像处理例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是这样的:

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

要这个东西:

 昏暗sourceImage为位图= ResizedBitmap1
昏暗的模板为位图= ResizedBitmap2
 

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

所以似乎存在由位图类比,我回到我的 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有大小功能(<$ C C $> 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:

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.

这篇关于AForge图像处理例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 02:24