CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2008
    Posts
    142

    Black and white picture

    hi
    i have written program to trun a picture in black and white
    but problem with this code is that it is very slow specialy for high resolution picture
    i have a written a function which gives black and white
    i m running a loop through image height and width and setting each pixel and also getting each pixel
    i think that is problem why it is slow
    when i run my program , i have to wait for minutes and quality of image is not so good also
    code for black and white function
    Code:
    Public Function PureBW(ByVal image As System.Drawing.Bitmap, Optional ByVal Mode As BWMode = BWMode.By_Lightness, Optional ByVal tolerance As Single = 0) As System.Drawing.Bitmap
            image = Me.PictureBox1.Image
            Dim x As Integer
    
            Dim y As Integer
    
            If tolerance > 1 Or tolerance < -1 Then
    
                Throw New ArgumentOutOfRangeException
    
                Exit Function
    
            End If
    
            For x = 0 To image.Width - 1 Step 1
    
                For y = 0 To image.Height - 1 Step 1
    
                    Dim clr As Color = image.GetPixel(x, y)
    
                    If Mode = BWMode.By_RGB_Value Then
    
                        If (CInt(clr.R) + CInt(clr.G) + CInt(clr.B)) > 383 - (tolerance * 383) Then
    
                            image.SetPixel(x, y, Color.White)
    
                        Else
    
                            image.SetPixel(x, y, Color.Black)
    
                        End If
    
                    Else
    
                        If clr.GetBrightness > 0.5 - (tolerance / 2) Then
    
                            image.SetPixel(x, y, Color.White)
    
                        Else
    
                            image.SetPixel(x, y, Color.Black)
    
                        End If
    
                    End If
    
                Next
    
            Next
    
            Return image
    
        End Function
    i want it to change, i want that it should work quickly
    what changes this need and any alternative method available to do that??

    i have an other function for inverting the picture
    it is very fast, here i m using a matrix
    Code:
    Public Function InvertImage(ByVal img As Image) As Boolean
            Dim cm As ColorMatrix = New ColorMatrix(New Single()() _
                                       {New Single() {-1, 0, 0, 0, 0}, _
                                        New Single() {0, -1, 0, 0, 0}, _
                                        New Single() {0, 0, -1, 0, 0}, _
                                        New Single() {0, 0, 0, 1, 0}, _
                                        New Single() {1, 1, 1, 1, 1}})
            Return DrawImage(img, cm)
        End Function
    can i get black and white image from above function,if yes then how??
    what changes i need in above function to get desire result?
    thanks

  2. #2
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: Black and white picture

    Quote Originally Posted by aamir55
    ...
    Code:
    Public Function PureBW(ByVal image As System.Drawing.Bitmap, Optional ByVal Mode As BWMode = BWMode.By_Lightness, Optional ByVal tolerance As Single = 0) As System.Drawing.Bitmap
            image = Me.PictureBox1.Image
            Dim x As Integer
    
            Dim y As Integer
    
            If tolerance > 1 Or tolerance < -1 Then
    
                Throw New ArgumentOutOfRangeException
    
                Exit Function
    
            End If
    
            For x = 0 To image.Width - 1 Step 1 'Step 1 is probably unnecesary
    
                For y = 0 To image.Height - 1 Step 1
    
                    Dim clr As Color = image.GetPixel(x, y)
    
                    If Mode = BWMode.By_RGB_Value Then
    
                        If (CInt(clr.R) + CInt(clr.G) + CInt(clr.B)) > 383 - (tolerance * 383) Then
    
                            image.SetPixel(x, y, Color.White)
    
                        Else
    
                            image.SetPixel(x, y, Color.Black)
    
                        End If
    
                    Else
    
                        If clr.GetBrightness > 0.5 - (tolerance / 2) Then
    
                            image.SetPixel(x, y, Color.White)
    
                        Else
    
                            image.SetPixel(x, y, Color.Black)
    
                        End If
    
                    End If
    
                Next
    
            Next
    
            Return image
    
        End Function
    This should be a bit faster:
    Code:
        Public Function PureBW(ByVal image As System.Drawing.Bitmap, _
                               Optional ByVal Mode As BWMode = BWMode.By_Lightness, _
                               Optional ByVal tolerance As Single = 0) As System.Drawing.Bitmap
            image = Me.PictureBox1.Image
            Dim x As Integer
            Dim y As Integer
    
            If tolerance > 1 Or tolerance < -1 Then
                Throw New ArgumentOutOfRangeException
    
                Exit Function
            End If
    
            Dim clr As Color
            Dim ColorLimit As Integer
            Dim White As Color = Color.White
            Dim Black As Color = Color.Black
    
            If Mode = BWMode.By_RGB_Value Then
                ColorLimit = CInt(383 - (tolerance * 383))
                With image
                    For x = 0 To .Width - 1 'Step 1
                        For y = 0 To .Height - 1 'Step 1
    
                            clr = .GetPixel(x, y)
                            If (CInt(clr.R) + CInt(clr.G) + CInt(clr.B)) > ColorLimit Then
                                .SetPixel(x, y, White)
                            Else
                                .SetPixel(x, y, Black)
                            End If
    
                        Next y
                    Next x
                End With 'image
            Else
                ColorLimit = CInt(0.5 - (tolerance / 2))
                With image
                    For x = 0 To .Width - 1 ' Step 1
                        For y = 0 To .Height - 1 'Step 1
    
                            clr = .GetPixel(x, y)
                            If clr.GetBrightness > ColorLimit Then
                                .SetPixel(x, y, White)
                            Else
                                .SetPixel(x, y, Black)
                            End If
    
                        Next y
                    Next x
                End With 'image
            End If
    
            Return image
        End Function
    Surely all the time is taken by the image.SetPixel, .GetPixel, and clr.GetBrightness function calling.
    If you find a fast mode to store those values in a matrix, you can calculate faster.

    i want it to change, i want that it should work quickly
    what changes this need and any alternative method available to do that??

    i have an other function for inverting the picture
    it is very fast, here i m using a matrix
    Code:
    Public Function InvertImage(ByVal img As Image) As Boolean
            Dim cm As ColorMatrix = New ColorMatrix(New Single()() _
                                       {New Single() {-1, 0, 0, 0, 0}, _
                                        New Single() {0, -1, 0, 0, 0}, _
                                        New Single() {0, 0, -1, 0, 0}, _
                                        New Single() {0, 0, 0, 1, 0}, _
                                        New Single() {1, 1, 1, 1, 1}})
            Return DrawImage(img, cm)
        End Function
    can i get black and white image from above function,if yes then how??
    what changes i need in above function to get desire result?
    thanks
    I have not an idea why that could work (it should not work at all).
    [Vb.NET 2008 (ex Express)]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured