-
Sep 23rd, 2021, 03:39 PM
#1
Thread Starter
Addicted Member
Image Comparison with marked locations
Hello All, I am quite stuck with this code I am using to compare differences between two images. I downloaded this code and it seems to work really well for basic images with large features. It does well for marking the difference in a bright color. My issue with it besides not having a good understanding of how it works is, I do not see anywhere in the code where it gets the location on the image (I guess in pixels x & y) so that I could mark like an arrow or something larger to show me small portions that have changed. Does anyone have any idea how this could be done? If the image compare shows a spot several pixels big, that is very difficult to find on the final image. I have ZERO experience with this, but am anxious to learn something here.
Code:
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Runtime.InteropServices
Public Class Form1
Dim ToolTip1 As New ToolTip
Dim W As Integer = 459
Dim H As Integer = 700
Dim Ratio As Single
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ToolTip1.SetToolTip(TrackBar1, "Compensate For Image Compression Or Decompression")
ToolTip1.SetToolTip(Label1, "Show Tolerance Value")
End Sub
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
Dim FDialog As New OpenFileDialog
Try
With FDialog
'Select file extension filter
.Filter = "Image Files|*.bmp;*.gif;*.jpg;*.png;*.tif"
If .ShowDialog = DialogResult.OK Then
'Move a copy of the image from picturebox1 to Picturebox2
PictureBox2.Image = PictureBox1.Image.Clone
'Read File Info And Assign Image to PictureBox1
PictureBox1.Image = Image.FromFile(.FileName)
'Find And Save Image Dimensions
W = System.Drawing.Image.FromFile(.FileName).Width
H = System.Drawing.Image.FromFile(.FileName).Height
' Keep Proportion On Resizing
Ratio = CSng(W / H)
'Resize Pictureboxes to New dimensions
PictureBox2.Height = 320
PictureBox1.Height = 320
PictureBox1.Width = CInt(320 * Ratio)
PictureBox2.Width = CInt(320 * Ratio)
'Form's Title
Me.Text = "Differ ->" & Path.GetFileName(.FileName) & " " & W & "x" & H
End If
End With
Catch ex As Exception
MessageBox.Show(ex.ToString)
Finally
If Not FDialog Is Nothing Then
FDialog.Dispose()
FDialog = Nothing
End If
End Try
End Sub
Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
'Show Trackbar's Value
Label1.Text = TrackBar1.Value.ToString
End Sub
'Function to clycle through the two images pixels
Private Function Diff1(ByVal imgx As Bitmap, ByVal imgY As Bitmap) As Image
'If the two images are of different dimension, tell so and exit function returning something to PictureBox3
If imgx.Size <> imgY.Size Then
MessageBox.Show("Images Are Of Different Sizes")
Return imgY
End If
'Use LockBits for Processing
Dim x As Integer 'Counter For Loop
Try
'Get Data For Both Images For Processing For Rectangular Area(Full Image)
Dim bmpSr1 As BitmapData = imgx.LockBits(New Rectangle(0, 0, W, H), ImageLockMode.ReadWrite, imgx.PixelFormat)
Dim bmpSr2 As BitmapData = imgY.LockBits(New Rectangle(0, 0, W, H), ImageLockMode.ReadWrite, imgx.PixelFormat)
'Find Initial Point For Checking
Dim ptrSr1 As IntPtr = bmpSr1.Scan0
Dim ptrSr2 As IntPtr = bmpSr2.Scan0
'Position of color bits and values of intensity and color for pixels On Images 1 and 2
Dim A, R, G, B, A1, A2 As Integer
B = 0
G = 1
R = 2
A = 3
'Lenght of Data
Dim bytesSr1 As Integer = bmpSr1.Stride * H
Dim bytesSr2 As Integer = bmpSr2.Stride * H
'Type Of Data
Dim rgbvaluesSr1(bytesSr1) As Byte
Dim rgbvaluesSr2(bytesSr1) As Byte
'Lock Rectangular Section While Processing
System.Runtime.InteropServices.Marshal.Copy(ptrSr1, rgbvaluesSr1, 0, bytesSr1)
System.Runtime.InteropServices.Marshal.Copy(ptrSr2, rgbvaluesSr2, 0, bytesSr2)
'Loop Thru And Compare
For x = 0 To bytesSr1 - 4 Step 4
'What Value For First Image
A1 = rgbvaluesSr1(x + R)
A1 = A1 + rgbvaluesSr1(x + G)
A1 = A1 + rgbvaluesSr1(x + B)
'What Value For Second Image
A2 = rgbvaluesSr2(x + R)
A2 = A2 + rgbvaluesSr2(x + G)
A2 = A2 + rgbvaluesSr2(x + B)
'Set The Alpha Value
rgbvaluesSr2(x + A) = 255
'Compare Difference With A Tolerance
If A1 - A2 > TrackBar1.Value Then
'If There Is A Difference Mark It On Second Image With A Color That Shows
' Original Numbers: R=0, G=185, B=255
rgbvaluesSr2(x + R) = 51
rgbvaluesSr2(x + G) = 255
rgbvaluesSr2(x + B) = 0
ElseIf A2 - A1 > TrackBar1.Value Then
'Or With Another
rgbvaluesSr2(x + R) = 255
rgbvaluesSr2(x + G) = 0
rgbvaluesSr2(x + B) = 200
Else
'If There Is No Difference Invert Color Of Pixel On Second Image
rgbvaluesSr2(x + R) = 255 - rgbvaluesSr2(x + R)
rgbvaluesSr2(x + G) = 255 - rgbvaluesSr2(x + G)
rgbvaluesSr2(x + B) = 255 - rgbvaluesSr2(x + B)
End If
Next
'Loop Ends
'Copy Locked Data To Byte Array
System.Runtime.InteropServices.Marshal.Copy(rgbvaluesSr2, 0, ptrSr2, bytesSr2)
'Unlock Both Data Objects
imgx.UnlockBits(bmpSr1)
imgY.UnlockBits(bmpSr1)
'And Return Image
Return imgY
Catch ex As Exception
'If There Was A Problem, Say So And Return Something To Image3
MessageBox.Show("There Was An Error")
Return PictureBox1.Image
End Try
MessageBox.Show("Can't use" & PictureBox1.Image.PixelFormat.ToString())
Return imgx
End Function
Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
'Display Results On PictureBox3
Me.Cursor = Cursors.WaitCursor
PictureBox3.Image = Diff1(ConvertToARGB(PictureBox1.Image), ConvertToARGB(PictureBox2.Image))
Me.Cursor = Cursors.Default
'Show It
PictureBox3.Refresh()
End Sub
Public Shared Function ConvertToARGB(ByVal original As Bitmap) As Bitmap
Dim newImage As New Bitmap(original.Width, original.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
newImage.SetResolution(original.HorizontalResolution, original.VerticalResolution)
Dim g As Graphics = Graphics.FromImage(newImage)
g.DrawImageUnscaled(original, 0, 0)
g.Dispose()
Return newImage
End Function
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Application.Exit()
End Sub
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|