-2

There is no error in the following code which convert the gray image to binary image but it doesn't work... I need help to determine why the code doesn't work...I,m using vb,net 2012

    Dim mp3 As Double
    mp2 = 0
    Dim i, j As Integer
    Dim img As New Bitmap(600, 600)
    ' PictureBox2.Cls()
    For i = 0 To w
        For j = 0 To h
            ab(i, j) = 0
            zxc(i, j) = 0
            zxc1(i, j) = 0
            ssc(i, j) = 0
        Next j

    Next i
    For i = 0 To w - 17 Step 16
        For j = 0 To h - 17 Step 16
            mp1 = 0
            For k = i To i + 15
                For l = j To j + 15
                    mp1 = mp1 + imageArray(k, l)
            Next l, k
            mp1 = mp1 / (16 ^ 2)
            For k = i To i + 15
                For l = j To j + 15
                    If imageArray(k, l) >= mp1 Then
                        ssc(k, l) = 255
                    Else
                        ssc(k, l) = 0
                    End If

                    img.SetPixel(k, l, Color.FromArgb(ssc(k, l), ssc(k, l), ssc(k, l)))
            Next l, k
        Next j

        PictureBox2.Refresh()
    Next i
    mp3 = 0 : nd = 1
    For k = 0 To w
        For l = j To h
            mp3 = mp3 + imageArray(k, l)
            nd = nd + 1
    Next l, k
    mp3 = mp3 / nd
    For k = 0 To w
        For l = j To h
            If imageArray(k, l) >= mp3 Then ssc(k, l) = 255 Else ssc(k, l) = 0
                            img.SetPixel(k, l, Color.FromArgb(ssc(k, l), ssc(k, l), ssc(k, l)))
    Next l, k
    mp3 = 0 : nd = 0
    For k = i To w
        For l = 0 To h
            mp3 = mp3 + imageArray(k, l)
            nd = nd + 1
    Next l, k
    mp3 = mp3 / nd
    For k = i To w
        For l = 0 To h
            If imageArray(k, l) >= mp3 Then ssc(k, l) = 255 Else ssc(k, l) = 0
                            img.SetPixel(k, l, Color.FromArgb(ssc(k, l), ssc(k, l), ssc(k, l)))

    Next l, k
    hpp = 0
    PictureBox2.Image = img

End Sub

....... THE code of loading the image is working ,but Idont know if it store the values of the array properly or not ,and it is as following

    Dim ofd As New OpenFileDialog
    ofd.Filter = "Bitmap files (*.bmp)|*.bmp"
    If ofd.ShowDialog = DialogResult.OK Then
        If ofd.FileName <> String.Empty Then
            Me.PictureBox1.Image = Bitmap.FromFile(ofd.FileName)
        End If
    End If
    Dim img As New Bitmap(ofd.FileName)
    Dim imageArray(img.Width, img.Height) As Integer
    Dim i, j As Integer
    Dim k As Double
    For i = 0 To img.Width - 1
        For j = 0 To img.Height - 1
            Dim pixel As Color = img.GetPixel(i, j)
            imageArray(i, j) = pixel.ToArgb()
            z(i, j) = imageArray(i, j)
            k = k + imageArray(i, j)
        Next j
    Next i

End Sub
  • It would make things much easier for some to understand if you used proper variable names :( – the_lotus Jan 07 '19 at 16:02
  • Is this method used to convert a color image to grayscale? If so, you can simply apply a [ColorMatrix](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.imaging.colormatrix.-ctor) to the Bitmap [ImageAttributes](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.imaging.imageattributes). It's really fast, also because it doesn't use `.SetPixel()`. – Jimi Jan 07 '19 at 16:36

1 Answers1

0

try this function

Public Shared Function GetArrayFromImage(image As Image) As Byte()
    If image IsNot Nothing Then
        Dim ic As New ImageConverter()
        Dim buffer As Byte() = DirectCast(ic.ConvertTo(image, GetType(Byte())), Byte())
        Return buffer
    Else
        Return Nothing
    End If
End Function
patel
  • 430
  • 1
  • 4
  • 9
  • Why The ToArgb() method return always -1 value in the following code? Pleas help .For i = 0 To img.Width - 1 For j = 0 To img.Height - 1 Dim pixel As Color = img.GetPixel(i, j) imageArray(i, j) = pixel.ToArgb() – dyar mohammad Jan 08 '19 at 11:08
  • I changed my function, try it – patel Jan 08 '19 at 16:05