Results 1 to 3 of 3

Thread: Using vb.net in image processing

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2020
    Posts
    2

    Using vb.net in image processing

    Hi everyone this my first time in VBForums
    I have homework to do before Monday
    The question is how to know the most color appears in image automatically and then change this color to red in vb.net and I'm using visual studio
    Please answer if can

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Using vb.net in image processing

    Well, the policy is to not do someones homework.
    You write the code and if you have some sticking point, you can ask a question about the part of the code that isn't doing what you want.

    Since this is homework, it would seem that the work should reflect what you've been taught, and since we don't know what you've been taught, it is hard to even make suggestions that would fit your current expected experience.

    If it were me, I might try creating a dictionary whose key is a color type, whatever type is returned by the GetPixel call on a bitmap.
    And the value associated with the dictionary would an Integer.
    I would have a loop, loop through each pixel of the image, use GetPixel to get the value, see if it is in the dictionary, and if it is, increment the value at that key, and if it isn't, add that key and set the value to 1.

    After all the pixel colors have been added to the dictionary and the count of those pixels are in the dictionary, find the largest value in the dictionary, and that is the color to be changed.

    You can then make a final loop through the image, and if the pixel color matches the one to be change, set it to red.

    Does any of that sound like anything you've been taught at this point?
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2020
    Posts
    2

    Re: Using vb.net in image processing

    I am sorry you are right
    I have study vb.net only in Gui so I'm not good with it .
    This is the code I find

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles image_processing.Click
    Dim ofd As New OpenFileDialog()
    Dim R(), G(), B() As Integer
    ReDim R(255), G(255), B(255)
    Dim br, bg, bb As Integer
    Dim counterR, counterG, counterB As Integer
    counterR = 0 : counterG = 0 : counterB = 0

    If ofd.ShowDialog() = DialogResult.OK Then
    Dim bmp As New Bitmap(ofd.FileName)
    PictureBox1.Image = bmp
    Dim wid As Integer = bmp.Width
    Dim hgt As Integer = bmp.Height
    Dim bmr As New Bitmap(wid, hgt)
    For i = 0 To wid - 1
    For j = 0 To hgt - 1
    Dim c As Color = bmp.GetPixel(i, j)
    R(c.R) = R(c.R) + 1
    G(c.G) = G(c.G) + 1
    B(c.R) = B(c.B) + 1
    Next
    Next

    Dim f As StreamWriter = New StreamWriter("his.csv", True)
    For x = 0 To 255
    f.WriteLine(R(x) & "," & G(x) & "," & B(x))
    Next
    f.Close()

    For i = 0 To wid - 1
    For j = 0 To hgt - 1
    Dim c As Color = bmp.GetPixel(i, j)
    br = c.R : bg = c.G : bb = c.B
    If br > bg And br > bb Then
    counterR += 1
    ElseIf bg > br And bg > bb Then
    counterG += 1
    ElseIf bb > br And bb > bg Then
    counterB += 1
    End If
    Next
    Next
    For i = 0 To wid - 1
    For j = 0 To hgt - 1
    Dim c As Color = bmp.GetPixel(i, j)
    br = c.R : bg = c.G : bb = c.B
    If counterG > counterR And counterG > counterB Then
    If (bg <= 255 And bg >= 180 And bb <= 160 And br <= 160) Or (bg >= 60 And bg <= 255 And br <= 10 And bb <= 10) Then
    bmr.SetPixel(i, j, Color.Red)
    Else
    bmr.SetPixel(i, j, c)
    End If
    ElseIf counterB > counterR And counterB > counterG Then
    If bb > br And bb > bg Then
    bmr.SetPixel(i, j, Color.Red)
    Else
    bmr.SetPixel(i, j, c)
    End If
    End If
    Next
    Next

    PictureBox2.Image = bmr
    bmr.Save("test.jpg", Imaging.ImageFormat.Jpeg)
    End If
    End Sub


    I need to use maximum function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width