count black & white pixels in a bitmap

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fordinario
    New Member
    • May 2012
    • 2

    count black & white pixels in a bitmap

    hi everyone, i am writing a program for my research project and i need to count
    the number of black & white pixels in an image inside a picturebox. I am basically a beginner in visual basic. I already have a picture viewer. Please help me devise a visual basic code to count the number of black & white pixels inside the picturebox
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    I'm going to assume you're using the old VB, version 6 or earlier. If you're using the later dotnet-based ones (VB2008, etc), the logic may work, but any language syntax will probably be totally wrong. In that case I recommend posting your question in our VB.Net forum.

    As usual in programming, there are probably lots of ways to achieve what you want. Here's one fairly simple (though possibly a bit slow) method that should work.

    First of all, make sure the .Scalemode property of the picturebox is set to 3 (pixel).

    Use the .Width and .Height properties (or maybe the .Scalewidth and .ScaleHeight) of the picturebox to get the limits of where you'll be looking. Experiment to see what works better.

    Then do two nested loops to go across and down, and use the .Point property (or is that a method? I forget) of the picturebox to get the colour of each pixel.

    Here's an experiment that might help in building an understanding of what's going on. Paste this line into the MouseMove event of the picturebox...
    Code:
    Debug.Print X, Y, Picture1.Point (X,Y)
    (That's if the name of the control is Picture1, of course. :)

    Comment

    • PsychoCoder
      Recognized Expert Contributor
      • Jul 2010
      • 465

      #3
      I'm going on the presumption that you're using VB6 and have a solution for VB6. If you're not using VB6 (like maybe VB8) let us know so we can find a .NET solution.

      First we use the Win32 API GetPixel.

      Here's the code:

      Code:
      Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
      
      Private Sub Form_Load()
          pixBox.Picture = LoadPicture("YourPicturePath")
          Dim i As Long, j As Long, nBlack As Long, nWhite As Long, pHdc As Long, tempColor As Long
          Show
          With pixBox
              pHdc = .hdc
              .ScaleMode = 3
              For i = 0 To .ScaleWidth
                  For j = 0 To .ScaleHeight
                      tempColor = GetPixel(pHdc, i, j)
                      If tempColor = vbWhite Then
                          nWhite = nWhite + 1
                      ElseIf tempColor = vbBlack Then
                          nBlack = nBlack + 1
                      End If
                  Next
              Next
          End With
          MsgBox "White : " & nWhite & vbCrLf & "Black : " & nBlack, vbInformation
      End Sub
      Hope that helps :)

      Comment

      • fordinario
        New Member
        • May 2012
        • 2

        #4
        thanks for the replies. I really appreciate your help to a beginner like me. I have inputted the code but some terms like the vbblack is not recognized by visual basic 2010 express. Before creating the form.vb, is there such a file as program.vb as equivalent to program.cs of visual c#? How to convert the codes above to visual basic 2010 express? Thanks

        Comment

        • PsychoCoder
          Recognized Expert Contributor
          • Jul 2010
          • 465

          #5
          @fordinario

          That's because you posted your question in the Visual Basic 4/5/6 forum, when it should have been in the VB.NET forum (VB 2010 is VB.NET.

          I'm moving this to the proper (VB.NET forum) forum, then try to work out a solution in VB.NET

          Comment

          • PsychoCoder
            Recognized Expert Contributor
            • Jul 2010
            • 465

            #6
            Here's a function for counting black pixels, should only require a few modifications to make it count both black & white (I jut wanted to help you get started)

            Code:
            Function countPixels() As Integer
                Dim bmp As Bitmap = Image.FromFile(TextBox3.Text)
            
                ' Make a difference image.
                Dim w As Integer = bmp.Width ', bmp.Width)
                Dim h As Integer = bmp.Height ', bm2.Height)
            
                ' Create the difference image.
                Dim count As Integer = 0
                Dim blackPixel As Color
                    
                For x As Integer = 0 To w - 1
            	    For y As Integer = 0 To h - 1
            	        blackPixel = bmp.GetPixel(x, y)
            	            If blackPixel.R = 0 AndAlso blackPixel.G = 0 AndAlso blackPixel.B = 0 Then
            	                count += 1
            	            End If
            
                    Next 'y
                Next 'x
            
                bmp.Dispose()
                Return count
            End Function
            That should get you down the right path (If I did it all for you you wouldn't learn anything.
            Last edited by PsychoCoder; Jun 7 '12, 03:39 AM.

            Comment

            Working...