 |
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
|
|
|

February 10th, 2012, 08:01 PM
|
Authorized User
|
|
Join Date: Nov 2011
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
|
|
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
|

February 11th, 2012, 01:23 PM
|
 |
Wrox Author
|
|
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
|
|
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.
|

February 12th, 2012, 12:27 PM
|
Authorized User
|
|
Join Date: Nov 2011
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
|
|
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
|

February 12th, 2012, 04:13 PM
|
 |
Wrox Author
|
|
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
|
|
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.
|

February 12th, 2012, 05:13 PM
|
Authorized User
|
|
Join Date: Nov 2011
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
|
|
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)?
|

February 12th, 2012, 05:17 PM
|
 |
Wrox Author
|
|
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
|
|
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).
|

February 15th, 2012, 01:10 AM
|
Authorized User
|
|
Join Date: Nov 2011
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
|
|
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
|

February 15th, 2012, 11:58 AM
|
 |
Wrox Author
|
|
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
|
|
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.
|

February 15th, 2012, 07:31 PM
|
Authorized User
|
|
Join Date: Nov 2011
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
|
|
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
|

February 15th, 2012, 08:14 PM
|
 |
Wrox Author
|
|
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
|
|
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.
|
|
 |
|