hello,I tried to apply mean filter to the image to remove the noise from the image but when i execute the programme the image that i remove the noise from it does not upload on the picturebox2 and this is the code that i used ,please help me.

 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim picture1 As New Bitmap(PictureBox1.Image)
        Dim x, y As Integer
        x = picture1.Width
        y = picture1.Height
        PictureBox2.Width = picture1.Width
        PictureBox2.Height = picture1.Height
        Dim pic2 As New Bitmap(PictureBox2.Width, PictureBox2.Height)
        Dim i, j As Integer
        Dim maskr(8) As Integer
        Dim maskg(8) As Integer
        Dim maskb(8) As Integer
        Dim sumr As Integer = 0
        Dim sumg As Integer = 0
        Dim sumb As Integer = 0
        Dim u As Integer = 0
        Dim c As Color
        Dim c2 As Color
        For i = 0 To x - 1
            For j = 0 To y - 1
                If ((i - 1) >= 0) And ((j - 1) >= 0) Then
                    c = picture1.GetPixel(i - 1, j - 1)
                    If c.R > 255 Then
                        maskr(0) = 255
                    ElseIf c.R < 0 Then
                        maskr(0) = 0
                    Else
                        maskr(0) = Convert.ToInt16(c.R)
                    End If
                    If c.G > 255 Then
                        maskg(0) = 255
                    ElseIf c.G < 0 Then
                        maskg(0) = 0
                    Else
                        maskg(0) = Convert.ToInt16(c.G)
                    End If
                    If c.B > 255 Then
                        maskb(0) = 255
                    ElseIf c.B < 0 Then
                        maskb(0) = 0
                    Else
                        maskb(0) = Convert.ToInt16(c.B)
                    End If
                End If
                If ((i - 1) >= 0) Then
                    c = picture1.GetPixel(i - 1, j)

                    If c.R > 255 Then
                        maskr(1) = 255
                    ElseIf c.R < 0 Then
                        maskr(1) = 0
                    Else
                        maskr(1) = Convert.ToInt16(c.R)
                    End If
                    If c.G > 255 Then
                        maskg(1) = 255
                    ElseIf c.G < 0 Then
                        maskg(1) = 0
                    Else
                        maskg(1) = Convert.ToInt16(c.G)
                    End If
                    If c.B > 255 Then
                        maskb(1) = 255
                    ElseIf c.B < 0 Then
                        maskb(1) = 0
                    Else
                        maskb(1) = Convert.ToInt16(c.B)
                    End If
                End If
                If ((i - 1) >= 0) And (j + 1 < picture1.Height) Then
                    c = picture1.GetPixel(i - 1, j + 1)

                    If c.R > 255 Then
                        maskr(2) = 255
                    ElseIf c.R < 0 Then
                        maskr(2) = 0
                    Else
                        maskr(2) = Convert.ToInt16(c.R)
                    End If
                    If c.G > 255 Then
                        maskg(2) = 255
                    ElseIf c.G < 0 Then
                        maskg(2) = 0
                    Else
                        maskg(2) = Convert.ToInt16(c.G)
                    End If
                    If c.B > 255 Then
                        maskb(2) = 255
                    ElseIf c.B < 0 Then
                        maskb(2) = 0
                    Else
                        maskb(2) = Convert.ToInt16(c.B)
                    End If
                End If
                If ((j - 1) >= 0) Then
                    c = picture1.GetPixel(i, j - 1)

                    If c.R > 255 Then
                        maskr(3) = 255
                    ElseIf c.R < 0 Then
                        maskr(3) = 0
                    Else
                        maskr(3) = Convert.ToInt16(c.R)
                    End If
                    If c.G > 255 Then
                        maskg(3) = 255
                    ElseIf c.G < 0 Then
                        maskg(3) = 0
                    Else
                        maskg(3) = Convert.ToInt16(c.G)
                    End If
                    If c.B > 255 Then
                        maskb(3) = 255
                    ElseIf c.B < 0 Then
                        maskb(3) = 0
                    Else
                        maskb(3) = Convert.ToInt16(c.B)
                    End If
                End If
                c = picture1.GetPixel(i, j)
                If c.R > 255 Then
                    maskr(4) = 255
                ElseIf c.R < 0 Then
                    maskr(4) = 0
                Else
                    maskr(4) = Convert.ToInt16(c.R)
                End If
                If c.G > 255 Then
                    maskg(4) = 255
                ElseIf c.G < 0 Then
                    maskg(4) = 0
                Else
                    maskg(4) = Convert.ToInt16(c.G)
                End If
                If c.B > 255 Then
                    maskb(4) = 255
                ElseIf c.B < 0 Then
                    maskb(4) = 0
                Else
                    maskb(4) = Convert.ToInt16(c.B)
                End If
                If ((j + 1) < picture1.Height) Then
                    c = picture1.GetPixel(i, j + 1)

                    If c.R > 255 Then
                        maskr(5) = 255
                    ElseIf c.R < 0 Then
                        maskr(5) = 0
                    Else
                        maskr(5) = Convert.ToInt16(c.R)
                    End If
                    If c.G > 255 Then
                        maskg(5) = 255
                    ElseIf c.G < 0 Then
                        maskg(5) = 0
                    Else
                        maskg(5) = Convert.ToInt16(c.G)
                    End If
                    If c.B > 255 Then
                        maskb(5) = 255
                    ElseIf c.B < 0 Then
                        maskb(5) = 0
                    Else
                        maskb(5) = Convert.ToInt16(c.B)
                    End If
                End If
                If ((i + 1) < picture1.Width) And ((j - 1) >= 0) Then
                    c = picture1.GetPixel(i + 1, j - 1)

                    If c.R > 255 Then
                        maskr(6) = 255
                    ElseIf c.R < 0 Then
                        maskr(6) = 0
                    Else
                        maskr(6) = Convert.ToInt16(c.R)
                    End If
                    If c.G > 255 Then
                        maskg(6) = 255
                    ElseIf c.G < 0 Then
                        maskg(6) = 0
                    Else
                        maskg(6) = Convert.ToInt16(c.G)
                    End If
                    If c.B > 255 Then
                        maskb(6) = 255
                    ElseIf c.B < 0 Then
                        maskb(6) = 0
                    Else
                        maskb(6) = Convert.ToInt16(c.B)
                    End If
                End If
                If ((i + 1) < picture1.Width) Then
                    c = picture1.GetPixel(i + 1, j)
                    If c.R > 255 Then
                        maskr(7) = 255
                    ElseIf c.R < 0 Then
                        maskr(7) = 0
                    Else
                        maskr(7) = Convert.ToInt16(c.R)
                    End If
                    If c.G > 255 Then
                        maskg(7) = 255
                    ElseIf c.G < 0 Then
                        maskg(7) = 0
                    Else
                        maskg(7) = Convert.ToInt16(c.G)
                    End If
                    If c.B > 255 Then
                        maskb(7) = 255
                    ElseIf c.B < 0 Then
                        maskb(7) = 0
                    Else
                        maskb(7) = Convert.ToInt16(c.B)
                    End If
                End If
                If ((i + 1) < picture1.Width) And ((j + 1) < picture1.Height) Then
                    c = picture1.GetPixel(i + 1, j + 1)
                    If c.R > 255 Then
                        maskr(8) = 255
                    ElseIf c.R < 0 Then
                        maskr(8) = 0
                    Else
                        maskr(8) = Convert.ToInt16(c.R)
                    End If
                    If c.G > 255 Then
                        maskg(8) = 255
                    ElseIf c.G < 0 Then
                        maskg(8) = 0
                    Else
                        maskg(8) = Convert.ToInt16(c.G)
                    End If
                    If c.B > 255 Then
                        maskb(8) = 255
                    ElseIf c.B < 0 Then
                        maskb(8) = 0
                    Else
                        maskb(8) = Convert.ToInt16(c.B)
                    End If
                End If
                If (u > 9) Then
                    sumr = sumr + maskr(u)
                    sumg = sumg + maskg(u)
                    sumb = sumb + maskb(u)
                    u = u + 1
                End If
                sumr = sumr / 9
                sumg = sumg / 9
                sumb = sumb / 9
                c2 = Drawing.Color.FromArgb(sumr, sumg, sumb)
                pic2.SetPixel(i, j, c2)
            Next j
        Next i
    End Sub

Recommended Answers

All 2 Replies

Where in your code do you set PictureBox2.Image equal to pic2?
Perhaps a Refresh() is also needed.
Success.

The logic

If c.G > 255 Then
    maskg(3) = 255
ElseIf c.G < 0 Then
    maskg(3) = 0
Else
    maskg(3) = Convert.ToInt16(c.G)
End If

is unnecessary. The values returned from c.G, c.R and c.B are byte values and are therefore guaranteed to be non-negative values in the range 0 to 255.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.