Comparing two images and get difference image
The below steps describe how to compare tow images and get difference image
Step 1: Get images to compare, both images shall be of same size
Step 2: Get byte array of each bitmap
2.1 Get first image byte array
2.1 Get second image byte array
Step 3: Create a byte array to hold differences of pixel color between the two images
Step 4: Loop and compare pixel colors of both bitmap, if colors are identical fill diffByte array in step 3 with any color of your choice and if pixel colors are not identical then fill diffByte array with one of bitmap pixel colors
Step 1: Get images to compare, both images shall be of same size
Dim sourceBitmap As Bitmap = PictureBox2.Image
Dim destBitmap As Bitmap = PictureBox1.Image
Step 2: Get byte array of each bitmap
2.1 Get first image byte array
Dim sourceBitmapData As Imaging.BitmapData = sourceBitmap.LockBits(New Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height), Imaging.ImageLockMode.ReadOnly, Imaging.PixelFormat.Format32bppArgb)
Dim sourceBytes As Byte() = New Byte(sourceBitmapData.Stride * sourceBitmapData.Height - 1) {}
Runtime.InteropServices.Marshal.Copy(sourceBitmapData.Scan0, SourceBytes, 0, SourceBytes.Length)
sourceBitmap.UnlockBits(sourceBitmapData)
2.1 Get second image byte array
Dim destBitmapData As Imaging.BitmapData = destBitmap.LockBits(New Rectangle(0, 0, destBitmap.Width, destBitmap.Height), Imaging.ImageLockMode.ReadOnly, Imaging.PixelFormat.Format32bppArgb)
Dim destBytes As Byte() = New Byte(destBitmapData.Stride * destBitmapData.Height - 1) {}
Runtime.InteropServices.Marshal.Copy(destBitmapData.Scan0, destBytes, 0, destBytes.Length)
destBitmap.UnlockBits(destBitmapData)
Step 3: Create a byte array to hold differences of pixel color between the two images
Dim diffBytes As Byte() = New Byte(sourceBytes.Length - 1) {}
Step 4: Loop and compare pixel colors of both bitmap, if colors are identical fill diffByte array in step 3 with any color of your choice and if pixel colors are not identical then fill diffByte array with one of bitmap pixel colors
Dim i As Integer = 0
While i < sourceBytes.Length - 1 ' RGBA: RED Green Blue Alpha
If (sourceBytes(i) = destBytes(i)) AndAlso (sourceBytes(i + 1) = destBytes(i + 1)) AndAlso (sourceBytes(i + 2) = destBytes(i + 2)) AndAlso (sourceBytes(i + 3) = destBytes(i + 3)) Then
Dim clr As Color = Color.Magenta
diffBytes(i) = CByte(clr.R)
diffBytes(i + 1) = CByte(clr.G)
diffBytes(i + 2) = CByte(clr.B)
diffBytes(i + 3) = CByte(clr.A)
Else
diffBytes(i) = sourceBytes(i)
diffBytes(i + 1) = sourceBytes(i + 1)
diffBytes(i + 2) = sourceBytes(i + 2)
diffBytes(i + 3) = sourceBytes(i + 3)
End If
i += 4
End While
Step 5: Convert the byte array which holds different pixel colors to a bitmapDim diffBitmp As New Bitmap(sourceBitmap.Width, sourceBitmap.Height)
Dim data As Imaging.BitmapData = diffBitmp.LockBits(New Rectangle(0, 0, diffBitmp.Width, diffBitmp.Height), Imaging.ImageLockMode.WriteOnly, Imaging.PixelFormat.Format32bppArgb)
Runtime.InteropServices.Marshal.Copy(diffBytes, 0, data.Scan0, diffBytes.Length)
diffBitmp.UnlockBits(data)
Step 6: Show diff bitmap in a picture boxMe.PictureBox3.Image = diffBitmp
Download: Download link
تسلم ايدك انت مبدع والله
ReplyDelete