Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Drawing
- Public Class Localizer
- Public ReadOnly GFP As Bitmap
- Public ReadOnly GBL As Bitmap
- Public ReadOnly RED As Bitmap
- Public ReadOnly RBL As Bitmap
- Public ReadOnly DPI As Bitmap
- Public ReadOnly DBL As Bitmap
- Public ReadOnly OVR As Bitmap
- Public ReadOnly OBL As Bitmap
- Public ReadOnly LCL As Bitmap
- Private RHig As Byte = 0
- Private GHig As Byte = 0
- Private BHig As Byte = 0
- Dim Ravg As Double
- Dim Rupp As Double
- Dim Rcnt As Integer = 0
- Dim Gavg As Double
- Dim Gupp As Double
- Dim Gcnt As Integer = 0
- Dim Bavg As Double
- Dim Bupp As Double
- Dim Bcnt As Integer = 0
- Dim Width As Integer
- Dim Height As Integer
- Public Sub New(ByVal RED As Bitmap, ByVal GFP As Bitmap, ByVal DPI As Bitmap, Optional ByVal Intensity As Double = 1.0)
- If RED Is Nothing Then
- If GFP Is Nothing Then
- RED = New Bitmap(DPI.Width, DPI.Height)
- Else
- RED = New Bitmap(GFP.Width, GFP.Height)
- End If
- End If
- If GFP Is Nothing Then
- If RED Is Nothing Then
- GFP = New Bitmap(DPI.Width, DPI.Height)
- Else
- GFP = New Bitmap(RED.Width, RED.Height)
- End If
- End If
- If DPI Is Nothing Then
- If RED Is Nothing Then
- DPI = New Bitmap(GFP.Width, GFP.Height)
- Else
- DPI = New Bitmap(RED.Width, RED.Height)
- End If
- End If
- Width = RED.Width
- Height = RED.Height
- If GFP.Size <> RED.Size OrElse RED.Size <> DPI.Size Then Throw New Exception("Input images do not match in size")
- Me.RED = RED.Clone
- Me.GFP = GFP.Clone
- Me.DPI = DPI.Clone
- OVR = Me.RED.Clone
- Dim OVRlm As New LockBitmap(OVR)
- OVRlm.LockBits()
- Dim GFPlm As New LockBitmap(Me.GFP)
- GFPlm.LockBits()
- Dim DPIlm As New LockBitmap(Me.DPI)
- DPIlm.LockBits()
- Dim rc As Integer = 0
- Dim gc As Integer = 0
- Dim bc As Integer = 0
- For y = 0 To OVRlm.Height - 1
- For x = 0 To OVRlm.Width - 1
- Dim r As Byte = OVRlm.GetPixel(x, y).R
- Dim g As Byte = GFPlm.GetPixel(x, y).G
- Dim b As Byte = DPIlm.GetPixel(x, y).B
- If r > RHig Then RHig = r
- If g > GHig Then GHig = g
- If b > BHig Then BHig = b
- If r > 0 Then
- Ravg += r
- rc += 1
- End If
- If g > 0 Then
- Gavg += g
- gc += 1
- End If
- If b > 0 Then
- Bavg += g
- bc += 1
- End If
- Next
- Next
- If rc = 0 Then rc = 1
- If gc = 0 Then gc = 1
- If bc = 0 Then bc = 1
- Ravg /= rc
- Gavg /= gc
- Bavg /= bc
- RBL = New Bitmap(OVRlm.Width, OVRlm.Height)
- Dim RBLlm As New LockBitmap(RBL)
- RBLlm.LockBits()
- GBL = New Bitmap(OVRlm.Width, OVRlm.Height)
- Dim GBLlm As New LockBitmap(GBL)
- GBLlm.LockBits()
- DBL = New Bitmap(OVRlm.Width, OVRlm.Height)
- Dim DBLlm As New LockBitmap(DBL)
- DBLlm.LockBits()
- OBL = New Bitmap(OVRlm.Width, OVRlm.Height)
- Dim OBLlm As New LockBitmap(OBL)
- OBLlm.LockBits()
- Dim Gsp As Double = GHig - Gavg
- Dim Rsp As Double = RHig - Ravg
- Dim Bsp As Double = BHig - Bavg
- Dim calcIntensity As Double = 255 * Intensity
- Parallel.For(0, OVRlm.Height, Sub(y)
- For x = 0 To OVRlm.Width - 1
- Dim rp As Byte = OVRlm.GetPixel(x, y).R
- Dim gp As Byte = GFPlm.GetPixel(x, y).G
- Dim bp As Byte = DPIlm.GetPixel(x, y).B
- OVRlm.SetPixel(x, y, Color.FromArgb(255, rp, gp, bp))
- If rp > Ravg Then
- Dim meh As Double = ((rp - Ravg) * calcIntensity) / Rsp
- If meh > 255 Then meh = 255
- rp = meh
- Else
- rp = 0
- End If
- If gp > Gavg Then
- Dim meh As Double = ((gp - Gavg) * calcIntensity) / Gsp
- If meh > 255 Then meh = 255
- gp = meh
- Else
- gp = 0
- End If
- If bp > Bavg Then
- Dim meh As Double = ((bp - Bavg) * calcIntensity) / Bsp
- If meh > 255 Then meh = 255
- bp = meh
- Else
- bp = 0
- End If
- OBLlm.SetPixel(x, y, Color.FromArgb(255, rp, gp, bp))
- RBLlm.SetPixel(x, y, Color.FromArgb(255, rp, 0, 0))
- GBLlm.SetPixel(x, y, Color.FromArgb(255, 0, gp, 0))
- DBLlm.SetPixel(x, y, Color.FromArgb(255, 0, 0, bp))
- Next
- End Sub)
- RBLlm.UnlockBits()
- GBLlm.UnlockBits()
- OVRlm.UnlockBits()
- DPIlm.UnlockBits()
- DBLlm.UnlockBits()
- LCL = New Bitmap(OVR)
- Dim LCLlm As New LockBitmap(LCL)
- LCLlm.LockBits()
- Parallel.For(0, GFPlm.Height, Sub(y)
- For x = 0 To GFPlm.Width - 1
- Dim o As Color = OBLlm.GetPixel(x, y)
- Dim Dif As Byte = (CInt(o.R) * o.G) / 255
- LCLlm.SetPixel(x, y, Color.FromArgb(Dif, Dif, 0))
- Next
- End Sub)
- GFPlm.UnlockBits()
- OBLlm.UnlockBits()
- LCLlm.UnlockBits()
- End Sub
- Public Sub Cleanup()
- GFP.Dispose()
- GBL.Dispose()
- RED.Dispose()
- RBL.Dispose()
- DPI.Dispose()
- DBL.Dispose()
- OVR.Dispose()
- OBL.Dispose()
- LCL.Dispose()
- End Sub
- Public Function ComputeLocalization() As Double
- Dim Obllm As New LockBitmap(OBL)
- Obllm.LockBits()
- Dim avg As Byte = (Ravg + Gavg) / 2
- Dim Gvg As Double = 0
- Dim Gcn As Integer = 0
- Dim Rvg As Double = 0
- Dim Rcn As Integer = 0
- Dim R As Double = 0
- Dim R2 As Double = 0
- Dim G As Double = 0
- Dim G2 As Double = 0
- Dim h As Byte = 0
- For y = 0 To Obllm.Height - 1
- For x = 0 To Obllm.Width - 1
- Dim l As Color = Obllm.GetPixel(x, y)
- If l.R >= avg Then
- Rvg += l.R
- Rcn += 1
- End If
- If l.G >= avg Then
- Gvg += l.G
- Gcn += 1
- End If
- Next
- Next
- Rvg /= Rcn
- Gvg /= Gcn
- avg = (Rvg + Gvg + 255) / 3
- For y = 0 To Obllm.Height - 1
- For x = 0 To Obllm.Width - 1
- Dim l As Color = Obllm.GetPixel(x, y)
- If l.G >= avg Then
- If l.G >= Gvg Then
- G += 2
- If l.R >= Rvg Then
- R += 2
- Else
- R += 1
- End If
- Else
- G += 1
- If l.R > Rvg Then R += 1
- End If
- G2 += 255
- If l.R > 0 Then R2 += l.R
- End If
- Next
- Next
- Obllm.UnlockBits()
- Return ((((((R / G) * 100) - 50) * 100) / 50) + ((R2 / G2) * 100)) / 2
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment