Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 10th, 2012, 08:01 PM
Authorized User
Points: 193, Level: 3
Points: 193, Level: 3 Points: 193, Level: 3 Points: 193, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Question Changing Pixel Color

Hi Rod,


I tried what you previously suggested in order to change the pixel color of my image. This is the code that I use. It is working to some extent but I am still having trouble with it. By changing the Clr.R, Clr.G, and Clr.B Values I can adjust which pixel colors are changed to black (255,0,0,0). The strange thing is that it only changes the pixels to black (255,0,0,0) when the clr.R Values are with an = sign. For example, it will change the white background to all black. However, if I enter clr.R < 200, etc., although it will change some of the image to look black, it does not register as (255,0,0,0,) as it does with an equal sign. Do you have any idea what could cause these results? Thank you for you ongoing help.

Gary

'Change Selected Pixels to Black (255,0,0,0)
'Process the images pixels
For y As Integer = 0 To bm_source.Height - 1
For x As Integer = 0 To bm_source.Width - 1
'Get this pixel's color
Dim clr As Color = bm_source.GetPixel(x, y)



'Set the Pixels Color to Black
Dim clr2 As Color
clr2 = Color.FromArgb(255, 0, 0, 0)

If clr.R = 255 And
clr.G = 255 And
clr.B = 255 Then
bm_source.SetPixel(x, y, clr2)
End If
Next x
Next

'End change Pixels to Black
Reply With Quote
  #2 (permalink)  
Old February 11th, 2012, 01:23 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Hi Gary,

I can't think why it would do that. If you email me the example program (RodStephens@vb-helper.com) I'll take a look.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
Reply With Quote
  #3 (permalink)  
Old February 12th, 2012, 12:27 PM
Authorized User
Points: 193, Level: 3
Points: 193, Level: 3 Points: 193, Level: 3 Points: 193, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Question Another approach

Hi Rod,

Thank you for your reply. After further consideration, I think I should approach this application in another way. In this particular application I will have a simple scanned image that is drawn in one color... say black. I know that in jpg images, there will probably be several pixels of different colors that make up that solid color that I see as black.

I would like to select a small section of the black image (say 10 pixels by 10 pixels) and get a list of the different colored pixels, in RGB format 255,255,255,255) that make up that section. Then I would like to convert each of those colors into Black (255,0,0,0) in the original image. It seems that that would be a more exact approach with more predictable results. Perhaps I would use GetPixelSetPixel for each of the pixels listed. Can you give me any insight on this? Thank you for your ongoing help.


Private Sub InvertImage(ByVal bm As Bitmap)

'Process the images pixels
For y As Integer = 0 To bm.Height - 1
For x As Integer = 0 To bm.Width - 1
'Get this pixel's color
Dim clr As Color = bm.GetPixel(x, y)

'Convert colors to Black
clr = Color.FromArgb(255,0,0,0,)

'Set the Pixels color
bm.SetPixel(x,y,clr)


Next x
Next
End Sub
Reply With Quote
  #4 (permalink)  
Old February 12th, 2012, 04:13 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Sound like you want binary contrast enhancement. You loop over every pixel and convert them all to either black or white.

It's not too hard. It should look something like this:

Code:
For y As Integer = 0 To bm_source.Height - 1
    For x As Integer = 0 To bm_source.Width - 1
        Dim clr As Color = bm_source.GetPixel(x, y)
        If (clr.R + clr.G + clr.B) > 384 Then
            bm_source.SetPixel(x, y, Color.White)
        Else
            bm_source.SetPixel(x, y, Color.Black)
        End If
    Next x
Next y
If you're processing big pictures, it may be too slow for your application. If so, let me know. There's a faster method for accessing the pixels but it's more complicated so I don't usually bother with it unless I need to process a lot of large pictures.

After conversion if you want to save the new image, be sure to save it in png format so you don't lose color resolution like you do with jpgs.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
Reply With Quote
  #5 (permalink)  
Old February 12th, 2012, 05:13 PM
Authorized User
Points: 193, Level: 3
Points: 193, Level: 3 Points: 193, Level: 3 Points: 193, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Question Arithmetic Overflow

Hi Rod,

I tried this. I am getting an error message that says, "Arithmetic operation resulted in overflow." It seems to be having trouble with the (clr.R + clr.G + clr.B) > 384.

Also, at least the pixels that are changed to black need to change to exactly (255,0,0,0) to work. Does color.Black in the code you suggested mean Black (255,0,0,0)?
Reply With Quote
  #6 (permalink)  
Old February 12th, 2012, 05:17 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Sorry, it's probably trying to add the three colors in a byte. Try:

(CInt(clr.R) + clr.G + clr.B) > 384

That should convince it to convert the numbers to integers before adding them.

Color.Black is the same as (255, 0, 0, 0). Try Color.Black first because it's easier to read. If that doesn't work for some odd reason, then you can try Color.FromArgb(255, 0, 0, 0).
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
Reply With Quote
  #7 (permalink)  
Old February 15th, 2012, 01:10 AM
Authorized User
Points: 193, Level: 3
Points: 193, Level: 3 Points: 193, Level: 3 Points: 193, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Default Still having trouble

Hi Rod,

I am still having trouble with the sequence as a whole. In the code below, what is supposed to be happening is:

1) Open an image file and load it into a picture box called picShowPicture.

2) Count the pixels of the image to see whether they are Black (255,0,0,0) or White (255,255,255,255) and put the results in either txtBlack or txtWhite. It is expected that all of the pixels will be either Black or White.

3) If the number of pixels counted in txtBlack is greater, then the number of pixels in txtWhite, place a 1 in txtTextBox1. Otherwise place a 0 in txtTextBox1.

The image opens without difficulty. However, no numbers show up in txtBlack or txtWhite, or in txtTextBox1. There are no error messages either. My intention is that clicking on the button will initiate the whole sequence. Do I possibly need to do something else to cause the sequence to continue? As always thank you for your valuable help.

Gary

Public Class Form1

Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click


'Show the Open Dialog Box.
If ofdSelectPicture.ShowDialog = DialogResult.OK Then
'Load the picture into the picture box.
picShowPIcture.Image = Image.FromFile(ofdSelectPicture.FileName)

End If

End Sub





' Return the number of matching pixels.
Private Function CountPixels(ByVal ofdSelectPicture As Bitmap, ByVal target_color As Color) As Integer

'Do Loop through the pixels.
Dim matches As Integer = 0
For y As Integer = 0 To ofdSelectPicture.Height - 1
For x As Integer = 0 To ofdSelectPicture.Width - 1
If (ofdSelectPicture.GetPixel(x, y).Equals(target_color)) Then matches += 1
Next x
Next y

Return matches

End Function


Private Sub btnCountPixels_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Dim bm As New Bitmap(picShowPIcture.Image)
txtBlack.Text = CountPixels(bm, Color.FromArgb(255, 0, 0, 0)) & "Black Pixels"
txtWhite.Text = CountPixels(bm, Color.FromArgb(255, 255, 255, 255)) & "White Pixels"

If txtBlack.Text > txtWhite.Text Then
txtTextBox1.Text = 1
Else : txtTextBox1.Text = 0
End If
End Sub




End Class
Reply With Quote
  #8 (permalink)  
Old February 15th, 2012, 11:58 AM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

It looks like you may be close. It looks like your btnCountPixels_Click event handler doesn't have a Handles clause so the button isn't firing it.

Add "Handles btnCountPixels.Click" at the end of the first line (with the Sub statement). Or use the IDE to associate the event handler with the button's Click event.

Let me know if that works.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
Reply With Quote
  #9 (permalink)  
Old February 15th, 2012, 07:31 PM
Authorized User
Points: 193, Level: 3
Points: 193, Level: 3 Points: 193, Level: 3 Points: 193, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Default Still having trouble

Hi Rod,

Thank you for replying. I added the handle at the end but it still does not work. Also, what I am trying to do is to initiate the whole process only by clicking the "Start" button. It appears that with the code that I sent you, I would be clicking two different buttons -- one to start the process and another one to Count the Pixels in the last section of the code. The whole thing was working at one time and I cannot figure out what I changed.

Gary
Reply With Quote
  #10 (permalink)  
Old February 15th, 2012, 08:14 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

You could set a breakpoint in the event handler to make sure it's getting called when you click the button.

It looks like you have two event handlers, btnStart_Click and btnCountPixels_Click. It looks like the first loads the file and the second counts the pixels.

If you want both to happen when you open the file, you should either (1) move the code from btnCountPixels_Click into the other event handler, or (2) convert btnCountPixels_Click into a subroutine that you can call from btnStart_Click.

And if you're tired of the whole exercise, zip up the project and email it to me at RodStephens@vb-helper.com and I'll see if I can figure it out.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing Page Color darkestangel1980 CSS Cascading Style Sheets 6 February 6th, 2008 12:40 PM
color changing windows casemaker Oracle 0 May 28th, 2006 08:29 PM
changing pixel color in if/then construct rharris VB.NET 2002/2003 Basics 1 June 19th, 2005 09:12 AM
Changing color conny Visual C++ 0 April 14th, 2005 07:57 PM
Need help changing color on text, please larry Javascript How-To 1 December 23rd, 2003 10:24 PM



All times are GMT -4. The time now is 05:26 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.