Aurigma Forums
 » 
Graphics Mill
 » 
Discussions – Graphics Mill
 » 
Detect percentage of white pixels in jpg image
 
		
        
            
            
	
    | 
Rank: Member
 Groups: Guest
Joined: 8/15/2007(UTC)
 Posts: 14
 
 | 
            
	      
                I was using the following code: Code:    Public Function isWhiteImage(ByVal theImage As System.Drawing.Bitmap) As Boolean
        Dim bitmap As New Aurigma.GraphicsMill.Bitmap(theImage)
        Dim data As BitmapData = bitmap.LockBits()
        'Number of rows
        Dim height As Integer = data.Height
        'Number of columns
        Dim width As Integer = data.Width
        Dim countOfWhitePixels As Integer = 0
        Dim i, j As Integer
        For i = 0 To height - 1
            For j = 0 To width - 1
                'Read a single pixel
                Dim color As Aurigma.GraphicsMill.Color = data.GetPixel(i, j)
                If (color.GetChannel32(ColorChannel.Red) = color.GetChannel32(ColorChannel.Green)) Then
                    If (color.GetChannel32(ColorChannel.Red) = color.GetChannel32(ColorChannel.Blue)) Then
                        countOfWhitePixels = countOfWhitePixels + 1
                        If countOfWhitePixels > (data.Height / 2) Then
                            i = height - 1
                            j = width - 1
                        End If
                    End If
                End If
            Next j
        Next i
        bitmap.UnlockBits(data)
        If (countOfWhitePixels >= data.Height / 2) Then
            Return True
        Else
            Return False
        End If
    End Function
 to determine white space but it thinks every image has all white pixels.  What would the proper code to do this look like?  The example provided here in VB ->http://www.aurigma.com/d...AccessingtoPixelData.htm gets an error on Marshal.  What is Marshal??? Thanks, Linda Edited by user Wednesday, October 29, 2008 1:32:32 PM(UTC)
 | Reason: Not specified | 
|  | 
	
    | 
             | 
            
         | 
    |  | 
        
        
        
         
		   
        
            
            
	
    | 
Rank: Advanced Member
 Groups: Guest
Joined: 1/31/2005(UTC)
 Posts: 458
 
 Was thanked: 5 time(s) in 5 post(s)
 | 
            
	      
                Hello Linda, There is no need to call LockBits() method is you are working via GetPixel(x, y) method of the Bitmap. Here is my version of your code:  Code:    Public Function MyIsWhiteImage(ByVal bitmap As Aurigma.GraphicsMill.Bitmap) As Boolean
        Dim countOfWhitePixels As Integer
        Dim i, j As Integer
        Dim continueLoop As Boolean = True
        While i < bitmap.Height And continueLoop
            j = 0
            While j < bitmap.Width And continueLoop
                Dim color As Aurigma.GraphicsMill.Color = bitmap.GetPixel(j, i)
                Dim r As Integer = color.GetChannel32(Aurigma.GraphicsMill.ColorChannel.Red)
                Dim g As Integer = color.GetChannel32(Aurigma.GraphicsMill.ColorChannel.Green)
                Dim b As Integer = color.GetChannel32(Aurigma.GraphicsMill.ColorChannel.Blue)
                If r = g And r = b Then
                    countOfWhitePixels += 1
                    If countOfWhitePixels > (bitmap.Height / 2) Then
                        Return True
                    End If
                End If
                j += 1
            End While
            i += 1
        End While
        Return False
    End Function
 If you are satisfied with the speed of this implementation - I recommend use it. It is the simplest one. But if need more speed (e.g. if you are processing middle or large-size images) - you should use direct access to the pixels as described in the article you've mentioned.  Could you provide me with more detail on Marshar error? What exception did you get, on which line of code? I'll do my best to help you with this task.  By the way, its rather strange method: it counts number of all possible gray pixels (from black to white) - if you are trying to count pure white pixels you should check that values or all channels are not only equal, but also they should be maximal. And another strange point - this method stops counting pixels when finds just (height / 2) pixels. This means that for 800x600 image which contains 480000 pixels it stops after 300 found pixels... Maybe it will be better find at least (width * height / 2) white pixels to identify image as white?  Edited by user Monday, December 17, 2007 12:38:55 PM(UTC)
 | Reason: Not specified | 
|  | 
	
    | 
             | 
            
         | 
    |  | 
        
        
        
    
		
        
            
            
	
    | 
Rank: Member
 Groups: Guest
Joined: 8/15/2007(UTC)
 Posts: 14
 
 | 
            
	      
                Thanks Alex.
 In the example there is this line:
 
 Marshal.Copy(pointer, pixel, 0, pixelSize)
 
 I have the following import
 
 Imports Aurigma.GraphicsMill
 
 It does not find Marshal.  What library is it located in?  Is it new to version 4.0 of the software???
 
 Thanks,
 
 | 
|  | 
	
    | 
             | 
            
         | 
    |  | 
        
        
        
         
		   
        
            
            
	
    | 
Rank: Advanced Member
 Groups: Guest
Joined: 1/31/2005(UTC)
 Posts: 458
 
 Was thanked: 5 time(s) in 5 post(s)
 | 
            
	      
                Hello Linda,
 Marshal is .NET Framework class which provides methods for interaction with unmanaged code. You should import System.Runtime.InteropServices namespace or specify full name of the class.
 | 
|  | 
	
    | 
             | 
            
         | 
    |  | 
        
        
        
    
Aurigma Forums
 » 
Graphics Mill
 » 
Discussions – Graphics Mill
 » 
Detect percentage of white pixels in jpg image
 
    
        Forum Jump
         
    
    You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.