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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SteveIT3
    New Member
    • Jan 2016
    • 7

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

    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.
    Code:
     Private Function EdgeDetect(bmp As Bitmap) As Bitmap
            'convert any input bitmap into jpg just to make sure to have the correct byte order
            If Not bmp.RawFormat.Equals(ImageFormat.Jpeg) Then
                Using msJPG As New IO.MemoryStream()
                    bmp.Save(msJPG, ImageFormat.Jpeg)
                    bmp = CType(Image.FromStream(msJPG), Bitmap)
                End Using
            End If
            'setting up the lockbits
            Dim raz As Integer = bmp.Height \ 3
            Dim height As Integer = bmp.Height
            Dim width As Integer = bmp.Width
            Dim rect As New Rectangle(Point.Empty, bmp.Size)
            Dim bmpData As BitmapData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat)
            Dim bpp As Integer = If((bmp.PixelFormat = PixelFormat.Format32bppArgb), 2, 3)
            Dim size As Integer = bmpData.Stride * bmpData.Height
            Dim data As Byte() = New Byte(size - 1) {}
            Dim result As Byte() = New Byte(size - 1) {}
            Marshal.Copy(bmpData.Scan0, data, 0, size)
            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
            'edge detection
            Dim stride As Integer = bmpData.Stride
            'convert to grayscale first, makes finding edges cheaper later
            Parallel.For(0, width, Sub(totallynewX)
                                       Dim totallynewi As Integer
                                       For totallynewY As Integer = 0 To height - 1
                                           totallynewi = totallynewY * stride + totallynewX * bpp
                                           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
                                           result(totallynewi) = avg
                                           result(totallynewi + 1) = avg
                                           result(totallynewi + 2) = avg
                                       Next
                                   End Sub)
    
            'now find edges
            Parallel.For(0, height, Sub(horizontal)
                                        Dim pixel1, pixel2 As Integer
                                        Dim total, index As Integer
                                        For vertical As Integer = 0 To width - 1
                                            Dim offset As Integer = 1
                                            If horizontal = height - 1 OrElse vertical = width - 1 Then offset = 0
                                            index = horizontal * stride + vertical * bpp
                                            pixel1 = data(index)
                                            index = (horizontal + offset) * stride + (vertical + offset) * bpp
                                            pixel2 = data(index)
                                            total = Math.Min(Math.Abs(pixel1 - pixel2) + 128, 255)
                                            index = horizontal * stride + vertical * bpp
                                            result(index) = CByte(total)
                                            result(index + 1) = CByte(total)
                                            result(index + 2) = CByte(total)
                                        Next
                                    End Sub)
    
            Marshal.Copy(result, 0, bmpData.Scan0, data.Length)
            bmp.UnlockBits(bmpData)
            Return bmp
        End Function
    This provides me with the second image effect.
Working...