如何获得两幅图像的相似度百分比

如何获得两幅图像的相似度百分比

本文介绍了如何获得两幅图像的相似度百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码,但是使用小位图时总是返回50%(错误)

I have this code, but while using small bitmaps always return 50% ( error)

Public Function Equality(ByVal I1 As Image, ByVal I2 As Image, ByVal Resize As Boolean, ByVal colorsenser As Boolean) As String
       Dim BM1 As Bitmap
       Dim BM2 As Bitmap

       Dim different_pixel As Integer = 0
       Dim BM1Area As Integer = (I1.Width * I1.Height)
       Dim BM2Area As Integer = (I2.Width * I2.Height)
       Dim total As Integer = BM1Area + BM2Area

       Dim remove_pixel_Width As Integer = 1
       Dim remove_pixel_height As Integer = 1
       If Resize = True Then
           BM1 = ResizeBitmap(I1, 200, 300)
           BM2 = ResizeBitmap(I2, 200, 300)
       Else
           BM1 = I1
           BM2 = I2
       End If
       If BM1.Width <> BM2.Width Then
           If BM1.Width > BM2.Width Then
               remove_pixel_Width = BM1.Width - BM2.Width
           Else
               remove_pixel_Width = BM2.Width - BM2.Width

           End If

       End If
       If BM1.Height <> BM2.Height Then
           If BM1.Height > BM2.Height Then
               remove_pixel_Width = BM1.Height - BM2.Height
           Else
               remove_pixel_Width = BM2.Height - BM2.Height

           End If

       End If
       If BM1Area <> BM2Area Then
           If BM1Area < BM2Area Then
               different_pixel += BM2Area - BM1Area


           Else
               different_pixel += BM1Area - BM2Area
           End If
       End If


       For X = 0 To BM1.Width - remove_pixel_Width
           For y = 0 To BM2.Height - remove_pixel_height

               If BM1.GetPixel(X, y) <> (BM2.GetPixel(X, y)) Then
                   If colorsenser = True Then
                       Dim redr As Integer = 0

                       Dim greenr As Integer = 0
                       Dim bluer As Integer = 0
                       Dim somer As Integer = 0

                       Dim geto1 As Color = BM1.GetPixel(X, y)
                       Dim geto2 As Color = BM2.GetPixel(X, y)

                       somer = 0


                       If geto1.R - 2 < geto2.R < geto1.R + 2 Then
                       Else : somer += 1
                       End If
                       If geto1.B - 2 < geto2.B < geto1.B + 2 Then
                       Else : somer += 1
                       End If
                       If geto1.G - 2 < geto2.G < geto1.G + 2 Then
                       Else : somer += 1
                       End If

                       If somer <> 0 Then
                           different_pixel += 1
                       End If


                   Else
                       different_pixel += 1
                   End If


               End If

           Next
       Next
       Return 100 - ((different_pixel * 100) / total)

   End Function

推荐答案



这篇关于如何获得两幅图像的相似度百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-21 12:41