Finding white spots on image

zingalala

New member
Joined
Aug 13, 2007
Messages
3
Programming Experience
Beginner
well i am new to vb.net programmimng and i am having trouble in detecting how many white spots there on the image ( The image is completely black and there are wwhite spots lining exactly in vertical position)

which method i can use to get it right
 
I do not think this is offered in GDI+ or anywhere in .NET. There are many third party tools that could find this type of stuff out. Most of which are written in C++ and break the image down into bits to figure out where the spots are and many of them remove the spots (despeckle, dot removal, line removal) However, there is no proprietary method for this in GDI+ that I know about. This is a pretty complicated calculation to make as far as I know.
 
well i am trying to go with getpixel method here what i have mind is if i go like this

dim b as new Bitmap(Picturebox1.Image)
Dim xw,xh ,x y , k, r as Integer
Dim clr as color
k=0
xw = Picturebox.width
xh = Picturebox.Height
y = 1
For x = 0 to xh-1

clr= b.Getpixel(x,y)
r = clr.R ...............( Red value at a point)

If r <> 0 then
k = k+ 1
end If
Next x
MsgBox(r)


what ia m expecting outcome of this loop would be No. of white spots

but it has not beeen so :(

My logic is that since image is only Black and white when colorvalue at point is not 0 the point must be other than black..........still i am not able to get in the right code
 
I'm not sure why my attempts wouldn't work for me when I tried to say "if mycolor = Color.White", but this code works. It may not be the best way to do it, but you'll get the idea. You'll need a button, a listbox, and two labels. I just used one of the labels to show the X/Y coordinates while I was testing.

VB.NET:
Public Class Form1
    Dim x As Integer
    Dim y As Integer
    Dim pt As Point
    Dim bmp As Bitmap
    Dim count As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim instancecaller As New Threading.Thread(AddressOf countpixel)
        instancecaller.Start()
    End Sub
    Public Sub countpixel()
        bmp = Bitmap.FromFile("C:\Documents and Settings\Owner\Desktop\spots2.png")
        For x = 0 To bmp.Width - 1
            pt.X = x
            For y = 0 To bmp.Height - 1
                pt.Y = y
                Label2.Text = pt.X & "," & pt.Y
                Dim mycolor As Color = DirectCast(bmp, Bitmap).GetPixel(pt.X, pt.Y)
                ListBox1.Items.Add(mycolor.R & "," & mycolor.G & "," & mycolor.B)

                If mycolor.R = 255 And mycolor.G = 255 And mycolor.B = 255 Then
                    count += 1
                End If
            Next
        Next
        Label1.Text = count
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = False
    End Sub
End Class
 
The shared CheckForIllegalCrossThreadCalls property will only turn off the error message informing you about the illegal cross-thread call, it will not prevent the problem that will occur if the call trips. "Don't ever use it in your code." Safe cross-thread calls are done by invoking a delegate on the UI thread or using the BackgroundWorker properly.
 
I just tried this when I got home from work. I haven't used the BackgroundWorker before, but it's pretty easy to use, and it makes threading really easy. Here's the code for my previous post, using a Background worker this time (which you have to drop on the form, or declare programmatically).
VB.NET:
Public Class Form1
    Dim x As Integer
    Dim y As Integer
    Dim pt As Point
    Dim bmp As Bitmap
    Dim count As Integer
    Public Sub countpixel()
        bmp = Bitmap.FromFile("C:\Documents and Settings\Owner\Desktop\spots2.png")
        For x = 0 To bmp.Width - 1
            pt.X = x
            For y = 0 To bmp.Height - 1
                pt.Y = y
                Label2.Text = pt.X & "," & pt.Y
                Dim mycolor As Color = DirectCast(bmp, Bitmap).GetPixel(pt.X, pt.Y)
                ListBox1.Items.Add(mycolor.R & "," & mycolor.G & "," & mycolor.B)

                If mycolor.R = 255 And mycolor.G = 255 And mycolor.B = 255 Then
                    count += 1
                End If
            Next
        Next
        Label1.Text = count
    End Sub

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        countpixel()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        BackgroundWorker1.RunWorkerAsync()
    End Sub
End Class
 
Still wrong, DoWork is different thread. Set BGW to report progress and use the ReportProgress method to post info the safe ProgressChanged event.
 
Back
Top