By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,652 Members | 1,905 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,652 IT Pros & Developers. It's quick & easy.

Can someone help me duplicate the effect now that I achieved speed ?

P: 7
Look guys i'm at a total loss. My effect is nice and it runs fast but the effect does not match the one i'm trying to duplicate wondering if anyone can help me.


That is the effect i'm trying to create. This is my effect currently


Am I even on the right path ?
Here is the finalized code.
Expand|Select|Wrap|Line Numbers
  1.  Private Function EdgeDetect(bmp As Bitmap) As Bitmap
  2.         'convert any input bitmap into jpg just to make sure to have the correct byte order
  3.         If Not bmp.RawFormat.Equals(ImageFormat.Jpeg) Then
  4.             Using msJPG As New IO.MemoryStream()
  5.                 bmp.Save(msJPG, ImageFormat.Jpeg)
  6.                 bmp = CType(Image.FromStream(msJPG), Bitmap)
  7.             End Using
  8.         End If
  9.         'setting up the lockbits
  10.         Dim raz As Integer = bmp.Height \ 3
  11.         Dim height As Integer = bmp.Height
  12.         Dim width As Integer = bmp.Width
  13.         Dim rect As New Rectangle(Point.Empty, bmp.Size)
  14.         Dim bmpData As BitmapData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat)
  15.         Dim bpp As Integer = If((bmp.PixelFormat = PixelFormat.Format32bppArgb), 2, 3)
  16.         Dim size As Integer = bmpData.Stride * bmpData.Height
  17.         Dim data As Byte() = New Byte(size - 1) {}
  18.         Dim result As Byte() = New Byte(size - 1) {}
  19.         Marshal.Copy(bmpData.Scan0, data, 0, size)
  20.         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
  21.         'edge detection
  22.         Dim stride As Integer = bmpData.Stride
  23.         'convert to grayscale first, makes finding edges cheaper later
  24.         Parallel.For(0, width, Sub(totallynewX)
  25.                                    Dim totallynewi As Integer
  26.                                    For totallynewY As Integer = 0 To height - 1
  27.                                        totallynewi = totallynewY * stride + totallynewX * bpp
  28.                                        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
  29.                                        result(totallynewi) = avg
  30.                                        result(totallynewi + 1) = avg
  31.                                        result(totallynewi + 2) = avg
  32.                                    Next
  33.                                End Sub)
  34.  
  35.         'now find edges
  36.         Parallel.For(0, height, Sub(horizontal)
  37.                                     Dim pixel1, pixel2 As Integer
  38.                                     Dim total, index As Integer
  39.                                     For vertical As Integer = 0 To width - 1
  40.                                         Dim offset As Integer = 1
  41.                                         If horizontal = height - 1 OrElse vertical = width - 1 Then offset = 0
  42.                                         index = horizontal * stride + vertical * bpp
  43.                                         pixel1 = data(index)
  44.                                         index = (horizontal + offset) * stride + (vertical + offset) * bpp
  45.                                         pixel2 = data(index)
  46.                                         total = Math.Min(Math.Abs(pixel1 - pixel2) + 128, 255)
  47.                                         index = horizontal * stride + vertical * bpp
  48.                                         result(index) = CByte(total)
  49.                                         result(index + 1) = CByte(total)
  50.                                         result(index + 2) = CByte(total)
  51.                                     Next
  52.                                 End Sub)
  53.  
  54.         Marshal.Copy(result, 0, bmpData.Scan0, data.Length)
  55.         bmp.UnlockBits(bmpData)
  56.         Return bmp
  57.     End Function
This provides me with the second image effect.
Feb 5 '16 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.