TizzyT

Locality Processor -TizzyT

Oct 15th, 2017
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 8.80 KB | None | 0 0
  1. Imports System.Drawing
  2.  
  3. Public Class Localizer
  4.     Public ReadOnly GFP As Bitmap
  5.     Public ReadOnly GBL As Bitmap
  6.     Public ReadOnly RED As Bitmap
  7.     Public ReadOnly RBL As Bitmap
  8.     Public ReadOnly DPI As Bitmap
  9.     Public ReadOnly DBL As Bitmap
  10.     Public ReadOnly OVR As Bitmap
  11.     Public ReadOnly OBL As Bitmap
  12.     Public ReadOnly LCL As Bitmap
  13.  
  14.     Private RHig As Byte = 0
  15.     Private GHig As Byte = 0
  16.     Private BHig As Byte = 0
  17.  
  18.     Dim Ravg As Double
  19.     Dim Rupp As Double
  20.     Dim Rcnt As Integer = 0
  21.  
  22.     Dim Gavg As Double
  23.     Dim Gupp As Double
  24.     Dim Gcnt As Integer = 0
  25.  
  26.     Dim Bavg As Double
  27.     Dim Bupp As Double
  28.     Dim Bcnt As Integer = 0
  29.  
  30.     Dim Width As Integer
  31.     Dim Height As Integer
  32.  
  33.     Public Sub New(ByVal RED As Bitmap, ByVal GFP As Bitmap, ByVal DPI As Bitmap, Optional ByVal Intensity As Double = 1.0)
  34.         If RED Is Nothing Then
  35.             If GFP Is Nothing Then
  36.                 RED = New Bitmap(DPI.Width, DPI.Height)
  37.             Else
  38.                 RED = New Bitmap(GFP.Width, GFP.Height)
  39.             End If
  40.         End If
  41.         If GFP Is Nothing Then
  42.             If RED Is Nothing Then
  43.                 GFP = New Bitmap(DPI.Width, DPI.Height)
  44.             Else
  45.                 GFP = New Bitmap(RED.Width, RED.Height)
  46.             End If
  47.         End If
  48.         If DPI Is Nothing Then
  49.             If RED Is Nothing Then
  50.                 DPI = New Bitmap(GFP.Width, GFP.Height)
  51.             Else
  52.                 DPI = New Bitmap(RED.Width, RED.Height)
  53.             End If
  54.         End If
  55.  
  56.         Width = RED.Width
  57.         Height = RED.Height
  58.  
  59.         If GFP.Size <> RED.Size OrElse RED.Size <> DPI.Size Then Throw New Exception("Input images do not match in size")
  60.  
  61.         Me.RED = RED.Clone
  62.         Me.GFP = GFP.Clone
  63.         Me.DPI = DPI.Clone
  64.         OVR = Me.RED.Clone
  65.  
  66.         Dim OVRlm As New LockBitmap(OVR)
  67.         OVRlm.LockBits()
  68.         Dim GFPlm As New LockBitmap(Me.GFP)
  69.         GFPlm.LockBits()
  70.         Dim DPIlm As New LockBitmap(Me.DPI)
  71.         DPIlm.LockBits()
  72.  
  73.         Dim rc As Integer = 0
  74.         Dim gc As Integer = 0
  75.         Dim bc As Integer = 0
  76.  
  77.         For y = 0 To OVRlm.Height - 1
  78.             For x = 0 To OVRlm.Width - 1
  79.                 Dim r As Byte = OVRlm.GetPixel(x, y).R
  80.                 Dim g As Byte = GFPlm.GetPixel(x, y).G
  81.                 Dim b As Byte = DPIlm.GetPixel(x, y).B
  82.                 If r > RHig Then RHig = r
  83.                 If g > GHig Then GHig = g
  84.                 If b > BHig Then BHig = b
  85.                 If r > 0 Then
  86.                     Ravg += r
  87.                     rc += 1
  88.                 End If
  89.                 If g > 0 Then
  90.                     Gavg += g
  91.                     gc += 1
  92.                 End If
  93.                 If b > 0 Then
  94.                     Bavg += g
  95.                     bc += 1
  96.                 End If
  97.             Next
  98.         Next
  99.         If rc = 0 Then rc = 1
  100.         If gc = 0 Then gc = 1
  101.         If bc = 0 Then bc = 1
  102.         Ravg /= rc
  103.         Gavg /= gc
  104.         Bavg /= bc
  105.  
  106.         RBL = New Bitmap(OVRlm.Width, OVRlm.Height)
  107.         Dim RBLlm As New LockBitmap(RBL)
  108.         RBLlm.LockBits()
  109.  
  110.         GBL = New Bitmap(OVRlm.Width, OVRlm.Height)
  111.         Dim GBLlm As New LockBitmap(GBL)
  112.         GBLlm.LockBits()
  113.  
  114.         DBL = New Bitmap(OVRlm.Width, OVRlm.Height)
  115.         Dim DBLlm As New LockBitmap(DBL)
  116.         DBLlm.LockBits()
  117.  
  118.         OBL = New Bitmap(OVRlm.Width, OVRlm.Height)
  119.         Dim OBLlm As New LockBitmap(OBL)
  120.         OBLlm.LockBits()
  121.  
  122.         Dim Gsp As Double = GHig - Gavg
  123.         Dim Rsp As Double = RHig - Ravg
  124.         Dim Bsp As Double = BHig - Bavg
  125.         Dim calcIntensity As Double = 255 * Intensity
  126.  
  127.         Parallel.For(0, OVRlm.Height, Sub(y)
  128.                                           For x = 0 To OVRlm.Width - 1
  129.                                               Dim rp As Byte = OVRlm.GetPixel(x, y).R
  130.                                               Dim gp As Byte = GFPlm.GetPixel(x, y).G
  131.                                               Dim bp As Byte = DPIlm.GetPixel(x, y).B
  132.                                               OVRlm.SetPixel(x, y, Color.FromArgb(255, rp, gp, bp))
  133.                                               If rp > Ravg Then
  134.                                                   Dim meh As Double = ((rp - Ravg) * calcIntensity) / Rsp
  135.                                                   If meh > 255 Then meh = 255
  136.                                                   rp = meh
  137.                                               Else
  138.                                                   rp = 0
  139.                                               End If
  140.                                               If gp > Gavg Then
  141.                                                   Dim meh As Double = ((gp - Gavg) * calcIntensity) / Gsp
  142.                                                   If meh > 255 Then meh = 255
  143.                                                   gp = meh
  144.                                               Else
  145.                                                   gp = 0
  146.                                               End If
  147.                                               If bp > Bavg Then
  148.                                                   Dim meh As Double = ((bp - Bavg) * calcIntensity) / Bsp
  149.                                                   If meh > 255 Then meh = 255
  150.                                                   bp = meh
  151.                                               Else
  152.                                                   bp = 0
  153.                                               End If
  154.                                               OBLlm.SetPixel(x, y, Color.FromArgb(255, rp, gp, bp))
  155.                                               RBLlm.SetPixel(x, y, Color.FromArgb(255, rp, 0, 0))
  156.                                               GBLlm.SetPixel(x, y, Color.FromArgb(255, 0, gp, 0))
  157.                                               DBLlm.SetPixel(x, y, Color.FromArgb(255, 0, 0, bp))
  158.                                           Next
  159.                                       End Sub)
  160.         RBLlm.UnlockBits()
  161.         GBLlm.UnlockBits()
  162.         OVRlm.UnlockBits()
  163.         DPIlm.UnlockBits()
  164.         DBLlm.UnlockBits()
  165.  
  166.         LCL = New Bitmap(OVR)
  167.         Dim LCLlm As New LockBitmap(LCL)
  168.         LCLlm.LockBits()
  169.  
  170.         Parallel.For(0, GFPlm.Height, Sub(y)
  171.                                           For x = 0 To GFPlm.Width - 1
  172.                                               Dim o As Color = OBLlm.GetPixel(x, y)
  173.                                               Dim Dif As Byte = (CInt(o.R) * o.G) / 255
  174.                                               LCLlm.SetPixel(x, y, Color.FromArgb(Dif, Dif, 0))
  175.                                           Next
  176.                                       End Sub)
  177.         GFPlm.UnlockBits()
  178.         OBLlm.UnlockBits()
  179.         LCLlm.UnlockBits()
  180.     End Sub
  181.  
  182.     Public Sub Cleanup()
  183.         GFP.Dispose()
  184.         GBL.Dispose()
  185.         RED.Dispose()
  186.         RBL.Dispose()
  187.         DPI.Dispose()
  188.         DBL.Dispose()
  189.         OVR.Dispose()
  190.         OBL.Dispose()
  191.         LCL.Dispose()
  192.     End Sub
  193.  
  194.     Public Function ComputeLocalization() As Double
  195.         Dim Obllm As New LockBitmap(OBL)
  196.         Obllm.LockBits()
  197.  
  198.         Dim avg As Byte = (Ravg + Gavg) / 2
  199.         Dim Gvg As Double = 0
  200.         Dim Gcn As Integer = 0
  201.         Dim Rvg As Double = 0
  202.         Dim Rcn As Integer = 0
  203.         Dim R As Double = 0
  204.         Dim R2 As Double = 0
  205.         Dim G As Double = 0
  206.         Dim G2 As Double = 0
  207.  
  208.         Dim h As Byte = 0
  209.         For y = 0 To Obllm.Height - 1
  210.             For x = 0 To Obllm.Width - 1
  211.                 Dim l As Color = Obllm.GetPixel(x, y)
  212.                 If l.R >= avg Then
  213.                     Rvg += l.R
  214.                     Rcn += 1
  215.                 End If
  216.                 If l.G >= avg Then
  217.                     Gvg += l.G
  218.                     Gcn += 1
  219.                 End If
  220.             Next
  221.         Next
  222.         Rvg /= Rcn
  223.         Gvg /= Gcn
  224.         avg = (Rvg + Gvg + 255) / 3
  225.  
  226.         For y = 0 To Obllm.Height - 1
  227.             For x = 0 To Obllm.Width - 1
  228.                 Dim l As Color = Obllm.GetPixel(x, y)
  229.                 If l.G >= avg Then
  230.                     If l.G >= Gvg Then
  231.                         G += 2
  232.                         If l.R >= Rvg Then
  233.                             R += 2
  234.                         Else
  235.                             R += 1
  236.                         End If
  237.                     Else
  238.                         G += 1
  239.                         If l.R > Rvg Then R += 1
  240.                     End If
  241.                     G2 += 255
  242.                     If l.R > 0 Then R2 += l.R
  243.                 End If
  244.             Next
  245.         Next
  246.  
  247.         Obllm.UnlockBits()
  248.         Return ((((((R / G) * 100) - 50) * 100) / 50) + ((R2 / G2) * 100)) / 2
  249.     End Function
  250. End Class
Advertisement
Add Comment
Please, Sign In to add comment