Advertisement
Guest User

Untitled

a guest
Feb 5th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.19 KB | None | 0 0
  1.  Private Function EdgeDetectWithInvert(bmp As Bitmap) As Bitmap
  2.  
  3.         'convert any input bitmap into jpg just to make sure to have the correct byte order
  4.         If Not bmp.RawFormat.Equals(ImageFormat.Jpeg) Then
  5.             Using msJPG As New IO.MemoryStream()
  6.                 bmp.Save(msJPG, ImageFormat.Jpeg)
  7.                 bmp = CType(Image.FromStream(msJPG), Bitmap)
  8.             End Using
  9.         End If
  10.  
  11.         'setting up the lockbits
  12.         Dim raz As Integer = bmp.Height \ 3
  13.         Dim height As Integer = bmp.Height
  14.         Dim width As Integer = bmp.Width
  15.         Dim rect As New Rectangle(Point.Empty, bmp.Size)
  16.         Dim bmpData As BitmapData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat)
  17.         Dim bpp As Integer = If((bmp.PixelFormat = PixelFormat.Format32bppArgb), 2, 3)
  18.         Dim size As Integer = bmpData.Stride * bmpData.Height
  19.         Dim data As Byte() = New Byte(size - 1) {}
  20.         Dim result As Byte() = New Byte(size - 1) {}
  21.         Marshal.Copy(bmpData.Scan0, data, 0, size)
  22.         Marshal.Copy(bmpData.Scan0, result, 0, size) 'duplicate so that edge finding can run in parallel without using pixels as samples that have already been processed themselves
  23.  
  24.         'edge detection
  25.         Dim stride As Integer = bmpData.Stride
  26.  
  27.         'convert to grayscale first, makes finding edges cheaper later
  28.         Parallel.For(0, width, Sub(totallynewX)
  29.                                    Dim totallynewi As Integer
  30.                                    For totallynewY = 0 To height - 1
  31.                                        totallynewi = totallynewY * stride + totallynewX * bpp
  32.                                        Dim avg As Byte = CByte(Math.Min(data(totallynewi) * 0.299 + data(totallynewi + 1) * 0.587 + data(totallynewi + 2) * 0.114, 255)) 'formula for accurate grayscale
  33.                                        result(totallynewi) = avg
  34.                                        result(totallynewi + 1) = avg
  35.                                        result(totallynewi + 2) = avg
  36.                                    Next
  37.                                End Sub)
  38.  
  39.         'for dynamic ranging later
  40.         Dim brightest, darkest As Integer
  41.         brightest = 0
  42.         darkest = 255
  43.  
  44.         'now find edges
  45.         Parallel.For(0, height, Sub(horizontal)
  46.                                     Dim pixelOriginal, pixelDiagonal, pixelBelow, pixelRight As Integer
  47.                                     Dim total, index, diff1, diff2, diff3 As Integer
  48.                                     For vertical = 0 To width - 1
  49.                                         Dim offset As Integer = 1
  50.                                         If horizontal = height - 1 OrElse vertical = width - 1 Then offset = 0
  51.  
  52.                                         index = horizontal * stride + vertical * bpp
  53.                                         pixelOriginal = data(index)
  54.  
  55.                                         index = (horizontal) * stride + (vertical + offset) * bpp
  56.                                         pixelBelow = data(index)
  57.  
  58.                                         index = (horizontal + offset) * stride + (vertical) * bpp
  59.                                         pixelRight = data(index)
  60.  
  61.                                         index = (horizontal + offset) * stride + (vertical + offset) * bpp
  62.                                         pixelDiagonal = data(index)
  63.  
  64.                                         diff1 = CInt(Math.Floor((pixelOriginal - pixelDiagonal) / 2) + 128)
  65.                                         diff2 = CInt(Math.Floor((pixelOriginal - pixelBelow) / 2) + 128)
  66.                                         diff3 = CInt(Math.Floor((pixelOriginal - pixelRight) / 2) + 128)
  67.                                         'total = Math.Max(Math.Max(diff1, diff2), diff3)
  68.                                         total = (diff1 + diff2 + diff3) \ 3
  69.  
  70.                                         'flip extreme contrasts
  71.                                         'Dim flipOffset As Integer = CInt(NumericUpDown1.Value)
  72.                                         'If total < flipOffset Then total = 255
  73.                                         'If total > 255 - flipOffset Then total = 0
  74.  
  75.                                         If total > brightest Then brightest = total
  76.                                         If total < darkest Then darkest = total
  77.  
  78.                                         index = horizontal * stride + vertical * bpp
  79.                                         result(index) = CByte(total)
  80.                                         result(index + 1) = CByte(total)
  81.                                         result(index + 2) = CByte(total)
  82.                                     Next
  83.                                 End Sub)
  84.  
  85.         'now adjust by darkest and brightest
  86.         If brightest < 255 OrElse darkest > 0 Then
  87.             Dim darknessDiff As Integer = darkest
  88.             Dim brightnessDiff As Integer = 255 - brightest
  89.  
  90.             Parallel.For(0, height, Sub(horizontal)
  91.                                         Dim pixel As Integer
  92.                                         Dim index As Integer
  93.                                         For vertical = 0 To width - 1
  94.                                             index = horizontal * stride + vertical * bpp
  95.                                             pixel = result(index)
  96.  
  97.                                             If pixel < 128 Then
  98.                                                 pixel -= darknessDiff
  99.                                             Else
  100.                                                 pixel += brightnessDiff
  101.                                             End If
  102.  
  103.                                             index = horizontal * stride + vertical * bpp
  104.                                             result(index) = CByte(pixel)
  105.                                             result(index + 1) = CByte(pixel)
  106.                                             result(index + 2) = CByte(pixel)
  107.                                         Next
  108.                                     End Sub)
  109.  
  110.         End If
  111.  
  112.         Marshal.Copy(result, 0, bmpData.Scan0, data.Length)
  113.         bmp.UnlockBits(bmpData)
  114.  
  115.         Return bmp
  116.  
  117.     End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement