VB.Net 中的图像比较

发布于 2024-08-15 07:32:11 字数 174 浏览 6 评论 0 原文

在阅读了本网站上有关图像比较主题的许多相关帖子后,我想我会尝试在每个图像上实现 PCA,以查看图像是否“相似”,但我不确定如何从我的图像中获取数据 - 是否有一个 VB 函数可以用来将图像转换为字节数组或其他内容以便比较图像?或者比较两个图像的更简单的方法(应该是黑白的,但它们会被扫描,v.小图像)

非常感谢, 贝基

After reading many related posts on this site on the subject of image comparison I'm thinking I'll try implementing a PCA on each image in order to see if an image is 'similar' or not, but I'm not sure how to get the data from my images - is there a VB function that I can use to convert the image into an array of bytes or something in order to compare images? Or a simpler way to compare two images (should be black and white but they'll be scanned, v. small images)

Thanks very much,
Becky

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

携君以终年 2024-08-22 07:32:11

另外,这是一篇有用的文章:这个人拍摄了两张图像,比较了它们,然后创建了第三张图像,以图形方式表示两者之间的差异。这似乎是描述相似性的一种很好的视觉方式。

Also, here is a useful article: this guy took two images, compared them, then created a third image that graphically represented the difference between the two. It appears to be a nice visual way to depict similiarity.

不美如何 2024-08-22 07:32:11

使用 roygbiv 的答案来查看它们是否相同非常容易。要了解它们的相似程度相当复杂。如果这些是扫描文档,它们实际上永远不会完全相同。投资第三方选项可能是值得的。我们在扫描过程中使用 AccusoftTiS

也就是说,有一些潜在的重复 问题

To see if they're identical or not is quite easy using roygbiv's answer. To see how similar they are is quite complicated. If these are scanned documents they're really never going to be identical. It may be worthwhile to invest in a third party option. We use products in our scanning process from Accusoft and TiS.

That said, there are a couple of potential duplicate questions.

囍孤女 2024-08-22 07:32:11

你可以使用一些东西:

Public Class MyClass
    Shared  Sub Main(ByVal args() As String)
       Byte() mydata = File.ReadAllBytes("C:\MyFile.jpg")
    End Sub
End Class

You can use something:

Public Class MyClass
    Shared  Sub Main(ByVal args() As String)
       Byte() mydata = File.ReadAllBytes("C:\MyFile.jpg")
    End Sub
End Class
烂柯人 2024-08-22 07:32:11

所以,这就是我想出的办法。我没有单独比较像素,而是使用了根据文件内容提供的哈希算法。然后它比较返回的散列的各个字节。在我的测试中,它的返回速度是比较 1152 X 720 和 101KB 大的灰度位图图像的各个像素的速度的两倍。

这是代码:(

编辑是因为我第一次发布代码时一切看起来都很奇怪。删除了注释。)

Public Shared Function CompareTwoImageHashes(ByVal pathToFirstImage As String, ByVal pathToSecondImage As String) As Boolean

    Dim firstImage As FileInfo = New FileInfo(pathToFirstImage)
    Dim secondImage As FileInfo = New FileInfo(pathToSecondImage)

    If Not firstImage.Exists Then
        Throw New ArgumentNullException("pathToFirstImage", "The file referenced by the path does not exist!")
    End If

    If Not secondImage.Exists Then
        Throw New ArgumentNullException("pathToSecondImage", "The file referenced by the path does not exist!")
    End If

    Dim hashingTool As SHA256Managed
    Dim imagesMatch As Boolean = True

    Try

        Using firstImageStream As New FileStream(firstImage.FullName, FileMode.Open)
            Using secondImageStream As New FileStream(secondImage.FullName, FileMode.Open)

                hashingTool = SHA256Managed.Create()

                Dim imageOneHash As Byte() = hashingTool.ComputeHash(firstImageStream)
                Dim imageTwoHash As Byte() = hashingTool.ComputeHash(secondImageStream)

                hashingTool.Clear()

                If (imageOneHash.Length = imageTwoHash.Length) Then

                    For length As Integer = 0 To (imageOneHash.Length - 1)

                        If imageOneHash(length) <> imageTwoHash(length) Then
                            imagesMatch = False

                            Exit For
                        End If

                    Next

                    CompareTwoImageHashes = imagesMatch
                Else
                    CompareTwoImageHashes = False
                End If

            End Using
        End Using

    Catch ex As Exception

        Console.WriteLine("Error during compare: {0}", ex.Message)

    End Try

End Function

So, this is what I came up with. Rather than compare the pixels individually, I used a hashing algorithm fed from the contents of the file. It then compares the individual bytes of the returned hash. In my tests, it came back twice as fast as comparing the individual pixels for a gray-scale bitmap image 1152 X 720 and 101KB big.

Here's the code:

(editing because the first time I posted the code everything looked strange. removed comments.)

Public Shared Function CompareTwoImageHashes(ByVal pathToFirstImage As String, ByVal pathToSecondImage As String) As Boolean

    Dim firstImage As FileInfo = New FileInfo(pathToFirstImage)
    Dim secondImage As FileInfo = New FileInfo(pathToSecondImage)

    If Not firstImage.Exists Then
        Throw New ArgumentNullException("pathToFirstImage", "The file referenced by the path does not exist!")
    End If

    If Not secondImage.Exists Then
        Throw New ArgumentNullException("pathToSecondImage", "The file referenced by the path does not exist!")
    End If

    Dim hashingTool As SHA256Managed
    Dim imagesMatch As Boolean = True

    Try

        Using firstImageStream As New FileStream(firstImage.FullName, FileMode.Open)
            Using secondImageStream As New FileStream(secondImage.FullName, FileMode.Open)

                hashingTool = SHA256Managed.Create()

                Dim imageOneHash As Byte() = hashingTool.ComputeHash(firstImageStream)
                Dim imageTwoHash As Byte() = hashingTool.ComputeHash(secondImageStream)

                hashingTool.Clear()

                If (imageOneHash.Length = imageTwoHash.Length) Then

                    For length As Integer = 0 To (imageOneHash.Length - 1)

                        If imageOneHash(length) <> imageTwoHash(length) Then
                            imagesMatch = False

                            Exit For
                        End If

                    Next

                    CompareTwoImageHashes = imagesMatch
                Else
                    CompareTwoImageHashes = False
                End If

            End Using
        End Using

    Catch ex As Exception

        Console.WriteLine("Error during compare: {0}", ex.Message)

    End Try

End Function
巾帼英雄 2024-08-22 07:32:11

用于检索图像的像素数据;您可以使用 Bitmap.GetPixelBitmap.LockBits,这将为您提供 BitmapData(链接有示例代码) 类作为回报。

For retrieving the pixel data of an image; you can either use Bitmap.GetPixel or Bitmap.LockBits, which will give you a BitmapData (link has example code) class in return.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文