Counting black object on Image?

wjdelcampo

New member
Joined
Nov 19, 2023
Messages
2
Programming Experience
Beginner
Hi, I am a beginner on VB and looking for a source code to count the black objects on images using visual basic picture box. Sample image here


Thanks,

4.jpg
 
You're unlikely to get an answer to questions like "I want to do X, give me the code". Have you done any research? Do you have any idea of how it might be achieved? If not, you probably need to do that first.
 
You're unlikely to get an answer to questions like "I want to do X, give me the code". Have you done any research? Do you have any idea of how it might be achieved? If not, you probably need to do that first.

Yes, I had searched for and found it, but it is in another language I also don't understand.


Ex: I uploaded an image in a picture box with black area and clicked a button to print in the label.text the total count of those black area. Is it possible on VB?

here is my work base on what I research on.

VB.NET:
  Private Sub ButtonUpload_Click(sender As Object, e As EventArgs) Handles ButtonUpload.Click
      If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
          PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
      End If
      Dim grayscale As New Imaging.ColorMatrix(New Single()() _
      {
          New Single() {0.299, 0.299, 0.299, 0, 0},
          New Single() {0.587, 0.587, 0.587, 0, 0},
          New Single() {0.114, 0.114, 0.114, 0, 0},
          New Single() {0, 0, 0, 1, 0},
          New Single() {0, 0, 0, 0, 1}
      })

      Dim bmp As New Bitmap(PictureBox1.Image)
      Dim imgattr As New Imaging.ImageAttributes()
      imgattr.SetColorMatrix(grayscale)
      Using g As Graphics = Graphics.FromImage(bmp)
          g.DrawImage(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height),
                      0, 0, bmp.Width, bmp.Height,
                      GraphicsUnit.Pixel, imgattr)
      End Using
      PictureBox1.Image = bmp
  End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim bmp As New Bitmap(PictureBox1.Image)
        Dim visited As New HashSet(Of Point)
        Dim blackAreasCount As Integer = 0

        For x As Integer = 0 To bmp.Width - 1
            For y As Integer = 0 To bmp.Height - 1
                If bmp.GetPixel(x, y).ToArgb() = Color.Black.ToArgb() AndAlso Not visited.Contains(New Point(x, y)) Then
                    blackAreasCount += 1
                    Fill(bmp, x, y, visited)
                End If
            Next
        Next

        Label1.Text = blackAreasCount.ToString()
    End Sub
    Private Sub Fill(bmp As Bitmap, x As Integer, y As Integer, visited As HashSet(Of Point))
        If x < 0 OrElse x >= bmp.Width OrElse y < 0 OrElse y >= bmp.Height Then Return
        If visited.Contains(New Point(x, y)) OrElse bmp.GetPixel(x, y).ToArgb() <> Color.Black.ToArgb() Then Return

        ' Mark the current pixel as visited
        visited.Add(New Point(x, y))

        ' Recursively fill the neighboring pixels
        Fill(bmp, x - 1, y, visited)
        Fill(bmp, x + 1, y, visited)
        Fill(bmp, x, y - 1, visited)
        Fill(bmp, x, y + 1, visited)
    End Sub
 
Last edited by a moderator:
One option is you could use OpenAI Vision (https://platform.openai.com/docs/guides/vision) to upload an image and then ask it questions about the image. I don't have a subscription to it but if this is something you're going to be doing often maybe that would interest you.

Here is a vb.net winforms app I did this morning for it GitHub - jdelano0310/VBWFCountBlackObjects it isn't 100% complete with error handling and converting the response. I just wanted to see if I could talk to it. This does but it says I don't have permissions (which is correct)
 
Back
Top