Visual Basi c обрабатывает вложенные циклы очень медленно, или есть другие проблемы с моим кодом? - PullRequest
0 голосов
/ 21 апреля 2020

По сути, я пытаюсь провести l oop через каждый пиксель изображения и сравнить его с каждым пикселем другого изображения. Проблема в том, что он, кажется, просто делает это очень медленно (я больше не могу взаимодействовать с открытым окном, и Debug.WriteLine работает). Я хочу быть уверен, что это проблема, а не просто что-то не так с моим кодом.

monPic и crop затемнены как растровые изображения в верхней части моего кода.

Private Sub BtnCheck_Click(sender As Object, e As EventArgs) Handles btnCheck.Click
        monPic = New Bitmap("../../../../" & picNum & ".png")
        crop = New Bitmap("../../../../mm.png")
        For x As Integer = 0 To monPic.Width - 1
            Debug.WriteLine("level 1")
            For y As Integer = 0 To monPic.Height - 1
                Debug.WriteLine("level 2")
                If CInt(monPic.GetPixel(x, y).A) <> 0 Then
                    For x2 As Integer = 0 To crop.Width - 1
                        Debug.WriteLine("level 3")
                        For y2 As Integer = 0 To crop.Height - 1
                            Debug.WriteLine("level 4")
                            If monPic.GetPixel(x, y).R = crop.GetPixel(x2, y2).R And monPic.GetPixel(x, y).G = crop.GetPixel(x2, y2).G And monPic.GetPixel(x, y).B = crop.GetPixel(x2, y2).B Then matches += 1
                        Next y2
                    Next x2
                End If
            Next y
        Next x
        lblMatches.Text = CStr(matches)
    End Sub

1 Ответ

0 голосов
/ 22 апреля 2020

Это работает быстро. Требуется

Imports System.Security.Cryptography

. Преобразуйте 2 битовых массива в байтовые массивы, а затем sh с Sha256. Сравните га sh.

Адаптировано с https://www.codeproject.com/Articles/9299/Comparing-Images-using-GDI

Private Function Compare(bmp1 As Bitmap, bmp2 As Bitmap) As String
    Dim result = "It's a match!"
    If Not (bmp1.Size = bmp2.Size) Then
        result = "It's not even the same size"
    Else
        Dim ic As New ImageConverter
        Dim btImage1(0) As Byte
        btImage1 = CType(ic.ConvertTo(bmp1, btImage1.GetType), Byte())
        Dim btImage2(0) As Byte
        btImage2 = CType(ic.ConvertTo(bmp2, btImage2.GetType), Byte())
        Dim shaM As New SHA256Managed
        Dim hash1 = shaM.ComputeHash(btImage1)
        Dim hash2 = shaM.ComputeHash(btImage2)
        Dim i As Integer = 0
        Do While i < hash1.Length AndAlso i < hash2.Length AndAlso result = "It's a match!"
            If hash1(i) <> hash2(i) Then
                result = "The pixels don't match"
            End If
            i = (i + 1)
        Loop
    End If
    Return result
End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim png1 As New Bitmap(path1)
    Dim png2 As New Bitmap(path2)
    Dim message = Compare(png1, png2)
    MessageBox.Show(message)
End Sub
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...