How to duplicate emboss effect into second function

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

    How to duplicate emboss effect into second function

    I have two functions both Emboss and image but the first one works incredibly slow and the second doesn't match the desired effect from the first. I would like the image from the second function to be more "Dark Grey" like the first function. Can someone please help me.
    #1 http://pastebin.com/QRX07iH9
    #2 http://pastebin.com/1VQzQizy
  • IronRazer
    New Member
    • Jan 2013
    • 83

    #2
    Perhaps this will help. I cleaned the code up a bit and fixed a few things and it seems to produce the same exact image as the slow GetPixel and SetPixel methods.

    You where stepping through the pixels incorrectly which i fixed in the example below. Also a 32bppArgb image has 4 bytes per pixel, not 2 which you where using if the PixelFormat of the image was 32bppArgb.

    You will also notice that it will now keep the transparency of a 32BppArgb image such as a Png image often has instead of turning it gray too.

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim bmp As New Bitmap(PictureBox5.Image)
            Dim bmpData As BitmapData = bmp.LockBits(New Rectangle(Point.Empty, bmp.Size), ImageLockMode.ReadWrite, bmp.PixelFormat)
            Dim bpp As Integer = If((bmp.PixelFormat = PixelFormat.Format32bppArgb), 4, 3)
            Dim data((bmpData.Stride * bmpData.Height) - 1) As Byte
            Marshal.Copy(bmpData.Scan0, data, 0, data.Length)
    
            For i As Integer = 0 To data.Length - ((bpp + 1) + bmpData.Stride) Step bpp
                data(i) = CByte(Math.Min(Math.Abs(CInt(data(i)) - CInt(data(i + bpp + bmpData.Stride))) + 128, 255)) 'Blue
                data(i + 1) = CByte(Math.Min(Math.Abs(CInt(data(i + 1)) - CInt(data(i + bpp + 1 + bmpData.Stride))) + 128, 255)) 'Green
                data(i + 2) = CByte(Math.Min(Math.Abs(CInt(data(i + 2)) - CInt(data(i + bpp + 2 + bmpData.Stride))) + 128, 255)) 'Red
            Next
    
            Marshal.Copy(data, 0, bmpData.Scan0, data.Length)
            bmp.UnlockBits(bmpData)
    
            If PictureBox5.Image IsNot Nothing Then
                Try
                    PictureBox5.Image.Dispose()
                Catch ex As Exception
                End Try
            End If
            PictureBox5.Image = bmp
        End Sub

    Comment

    • SteveIT3
      New Member
      • Jan 2016
      • 7

      #3
      Wow thank you so much is there any way I can tell the FPS like a timer ?

      Comment

      • IronRazer
        New Member
        • Jan 2013
        • 83

        #4
        I don`t quite understand what you mean. FPS would indicate frames per second to me so, do you mean you want to know how many images can be converted per second using this method?

        If so, you can use the StopWatch class to measure how many milliseconds it takes from the start to the end of the whole conversion. Below is a link to the msdn documents on the StopWatch class. You can read about it and find an example of using it there.

        Provides a set of methods and properties that you can use to accurately measure elapsed time.

        Comment

        • SteveIT3
          New Member
          • Jan 2016
          • 7

          #5
          Yeah I am applying this to a live image feed. It gets about 60 FPS now.

          Comment

          • SteveIT3
            New Member
            • Jan 2016
            • 7

            #6
            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.

            Comment

            Working...