Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2010 > BOOK: Visual Basic 2010 Programmer's Reference
|
BOOK: Visual Basic 2010 Programmer's Reference
This is the forum to discuss the Wrox book Visual Basic 2010 Programmer's Reference by Rod Stephens; ISBN: 9780470499832
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Visual Basic 2010 Programmer's Reference section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 28th, 2011, 06:49 PM
Authorized User
 
Join Date: Nov 2011
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Question Counting number of pixels in a Bitmap Image

Hello,

First, I would like to thank Rod Stephens for his help regarding my previous question. It was a pleasant surprise to even receive a reply to a question on a forum, what to speak of receiving a reply with a perfect answer.

I have another question related to another part of the same application as before. I have a series of small 10 pixel by 10 pixel bitmaps. The pixels will be either black or white only. I need to write the code to count the number of black pixels and the number of white pixels in each image to compare them. I would then write a statement such as:


If blackpixels > white pixels then

Insert "1" in TextBox 1

If whitepixels > blackpixels then

Insert "0" in TextBox1

It seems that using GetPixel may help but that is only for determining the color of an individual pixel. Can anyone help me?

Thank you,


Gary Tanis
 
Old November 30th, 2011, 02:37 PM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Hi Gary,

You're halfway there. GetPixel gives you a pixel's color. You then need to decide whether the color is black or white.

You can compare two Colors by using = or by using the Color class's Equals method. Unfortunately named colors like Color.Black are different from the same colors created in other ways such as taken from a picture or created with Color.FromArgb. That means you can't compare a Color that you got with GetPixel to a named color such as Color.Black. For example, the following test always thinks the pixel is not black:

Code:
If (bm.GetPixel(10, 10) = Color.Black) Then ...
Instead you can compare the pixel to a color that you create by using Color.FromArgb. The parameters to FromArgb are the color's alpha, red, green, and blue components. Alpha should be 255 to be opaque, 0 to be transparent, and a value in between to be translucent. For black and white you'll want to sue 255.

This method counts the pixels that match a color.

Code:
    ' Return the number of matching pixels.
    Private Function CountPixels(ByVal bm As Bitmap, ByVal target_color As Color) As Integer
        ' Loop through the pixels.
        Dim matches As Integer = 0
        For y As Integer = 0 To bm.Height - 1
            For x As Integer = 0 To bm.Width - 1
                If (bm.GetPixel(x, y).Equals(target_color)) Then matches += 1
            Next x
        Next y
        Return matches
    End Function
You can use it as in:

Code:
Dim bm As New Bitmap(picImage.Image)
lblBlack.Text = CountPixels(bm, Color.FromArgb(255, 0, 0, 0)) & " black pixels"
lblWhite.Text = CountPixels(bm, Color.FromArgb(255, 255, 255, 255)) & " white pixels"
I hope that helps.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
 
Old December 15th, 2011, 12:44 PM
Authorized User
 
Join Date: Nov 2011
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Question Clarification need

Hi Rod,

Thank you for this reply. There is something that confuses me about this code. How do I initialize this procedure? Usually I add my procedures to a Button. That would give me something like this:




Private Sub btnCompareColors_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompareColors.Click

' Return the number of matching pixels.
Private Function CountPixels(ByVal bm As Bitmap, ByVal target_color As Color) As Integer
' Loop through the pixels.
Dim matches As Integer = 0
For y As Integer = 0 To bm.Height - 1
For x As Integer = 0 To bm.Width - 1
If (bm.GetPixel(x, y).Equals(target_color)) Then matches += 1
Next x
Next y
Return matches
End Function


End Sub



However, Visual Studio will not allow this. It gives me an error message "Statement Cannot Appear Within a Method Body. End or Method Assumed."

How can I initialize this procedure? The best case scenario would be to place this code at the end of the procedure in which I created a small image from the bigger picture. This was from one of my previous questions to you.

For example:

Dim strTop2 As Integer
strTop2 = strRow

Dim strLeft2 As Integer
strLeft2 = 11

Dim strWidth2 As Integer
strWidth2 = 10

Dim strHeight2 As Integer
strHeight2 = 10

Dim top2 As Integer = Integer.Parse(strTop2)
Dim left2 As Integer = Integer.Parse(strLeft2)
Dim width2 As Integer = Integer.Parse(strWidth2)
Dim Height2 As Integer = Integer.Parse(strHeight2)

'Make a Bitmap to hold the result
Dim bm2 As New Bitmap(width, Height)

'Associate a Graphics object with the Bitmap
Using gr As Graphics = Graphics.FromImage(bm2)

'Define source and destination rectangles
Dim src_rect As New Rectangle(left2, top2, width2, Height2)
Dim dst_rect As New Rectangle(0, 0, width2, Height2)

'Copy that part of the image
gr.DrawImage(picShowPicture.Image, dst_rect, src_rect, GraphicsUnit.Pixel)
End Using

'Display the result
PBX2.Image = bm2


========= Put it Here ================



========= Put it Here ================

I look forward to your reply. Thanks again.


Gary
 
Old December 15th, 2011, 12:59 PM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Hi Gary,

The CountPixels function is a separate method that cannot be placed inside another method such as the btnCompareColors_Click event handler. You can put it after the End Sub statement for the event handler.

The event handler's code can then call it to get the count. For example, in your code's "Put it Here" spot you could do something like this:

Code:
========= Put it Here ================
TextBox1.Text = CountPixels(bm2, Color.FromArgb(255, 0, 0, 0)) & " black pixels")
========= Put it Here ================
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
 
Old December 16th, 2011, 04:00 AM
Authorized User
 
Join Date: Nov 2011
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Question Declaration Expected

Rod,

When I try adding the following code into my application, I am getting an error message for lblBlack.Text and lblWhite.Text saying "Declaration Expected". It appears that they are both Labels in which to insert the Count of Black Or White Pixels. Why would I get a "Declaration Expected" error here? I normally get such errors related to variables, not text boxes or labels. Once again, thank you for all your help.

Gary


Dim bm As New Bitmap(picImage.Image)
lblBlack.Text = CountPixels(bm, Color.FromArgb(255, 0, 0, 0)) & " black pixels"
lblWhite.Text = CountPixels(bm, Color.FromArgb(255, 255, 255, 255)) & " white pixels"
 
Old December 16th, 2011, 02:23 PM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Hi Gary,

"Declaration expected" is the same error you've seen before. At some point the controls must be declared, too, just like variables. Normally they are declared in automatically-generated code so you don't see it but it needs to be there somewhere.

So what it's saying is that it can't find anything named lblBlack and lblWhite. Look on the form and see if the controls really have those names or if they're named something else such as Label1 or blackLabel and whiteLabel.

And watch out for typos like whiteLable or blaackLabel.

Hint: It you use IntelliSense to type for you, you're less likely to use the wrong names. That's why I usually use control prefixes such as lblBlack. If I type "lbl," IntelliSense gives me a list of possible choices.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Cloning section of a bitmap image Gary108 BOOK: Visual Basic 2010 Programmer's Reference 1 November 23rd, 2011 11:07 AM
Counting pixels shanthi85 VB How-To 0 May 22nd, 2007 02:13 AM
New Image (Bitmap) instance or not? jacob C# 2005 0 April 4th, 2006 04:12 AM
Read bitmap image? RichardRose C# 3 October 9th, 2004 11:45 PM
Prime Number Counting. Help!! ajm235 C++ Programming 3 August 27th, 2004 12:00 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.