9 Replies - 7739 Views - Last Post: 30 October 2013 - 06:34 AM Rate Topic: -----

#1 alapee  Icon User is offline

  • Stressed Out Programmer

Reputation: 214
  • View blog
  • Posts: 2,464
  • Joined: 24-October 13

VB.net Median Filter

Posted 29 October 2013 - 01:31 PM

Ok, I have been Wracking my brain over and over on this. I keep getting a variable error (See Attched JPG)
and can't find it in the code.


 Public Sub MeanFilter(ByVal imgBase As Image)
        Dim img As Bitmap = New Bitmap(imgBase), img2 As Image = New Bitmap(imgBase)
        Dim arIntR As Integer(), arIntA As Integer(), arIntB As Integer(), arIntG As Integer()
        Dim c As Color
        Try
            ReDim arIntR(8) : ReDim arIntA(8) : ReDim arIntB(8) : ReDim arIntG(8)
            For iWidth As Integer = 0 To img.Size.Width - 2
                For iHeight As Integer = 0 To img.Size.Height - 2
                    If (((iHeight - 1) >= 0) AndAlso ((iWidth - 1) >= 0)) Then
                        c = img.GetPixel((iHeight - 1), (iWidth - 1))
                        arIntR(0) = Convert.ToInt16(c.R)
                        arIntA(0) = Convert.ToInt16(c.A)
                        arIntB(0) = Convert.ToInt16(c.B)/>
                        arIntG(0) = Convert.ToInt16(c.G)
                    Else
                        arIntR(0) = 0
                        arIntA(0) = 0
                        arIntB(0) = 0
                        arIntG(0) = 0
                    End If
                    If (((iWidth - 1) >= 0) AndAlso (iHeight + (1 < img.Width))) Then
                        c = img.GetPixel((iHeight + 1), (iWidth - 1))
                        arIntR(1) = Convert.ToInt16(c.R)
                        arIntA(1) = Convert.ToInt16(c.A)
                        arIntB(1) = Convert.ToInt16(c.B)/>
                        arIntG(1) = Convert.ToInt16(c.G)
                    Else
                        arIntR(1) = 0
                        arIntA(1) = 0
                        arIntB(1) = 0
                        arIntG(1) = 0
                    End If
                    If ((iWidth - 1) >= 0) Then
                        c = img.GetPixel(iHeight, (iWidth - 1))
                        arIntR(2) = Convert.ToInt16(c.R)
                        arIntA(2) = Convert.ToInt16(c.A)
                        arIntB(2) = Convert.ToInt16(c.B)/>
                        arIntG(2) = Convert.ToInt16(c.G)
                    Else
                        arIntR(2) = 0
                        arIntA(2) = 0
                        arIntB(2) = 0
                        arIntG(2) = 0
                    End If
                    If (iHeight + (1 < img.Width)) Then
                        c = img.GetPixel((iHeight + 1), iWidth)
                        arIntR(3) = Convert.ToInt16(c.R)
                        arIntA(3) = Convert.ToInt16(c.A)
                        arIntB(3) = Convert.ToInt16(c.B)/>
                        arIntG(3) = Convert.ToInt16(c.G)
                    Else
                        arIntR(3) = 0
                        arIntA(3) = 0
                        arIntB(3) = 0
                        arIntG(3) = 0
                    End If
                    If ((iHeight - 1) >= 0) Then
                        c = img.GetPixel((iHeight - 1), iWidth)
                        arIntR(4) = Convert.ToInt16(c.R)
                        arIntA(4) = Convert.ToInt16(c.A)
                        arIntB(4) = Convert.ToInt16(c.B)/>
                        arIntG(4) = Convert.ToInt16(c.G)
                    Else
                        arIntR(4) = 0
                        arIntA(4) = 0
                        arIntB(4) = 0
                        arIntG(4) = 0
                    End If
                    If (((iHeight - 1) >= 0) AndAlso (iWidth + (1 < img.Height))) Then
                        c = img.GetPixel((iHeight - 1), (iWidth + 1))
                        arIntR(5) = Convert.ToInt16(c.R)
                        arIntA(5) = Convert.ToInt16(c.A)
                        arIntB(5) = Convert.ToInt16(c.B)/>
                        arIntG(5) = Convert.ToInt16(c.G)
                    Else
                        arIntR(5) = 0
                        arIntA(5) = 0
                        arIntB(5) = 0
                        arIntG(5) = 0
                    End If
                    If (iWidth + (1 < img.Height)) Then
                        c = img.GetPixel(iHeight, (iWidth + 1))
                        arIntR(6) = Convert.ToInt16(c.R)
                        arIntA(6) = Convert.ToInt16(c.A)
                        arIntB(6) = Convert.ToInt16(c.B)/>
                        arIntG(6) = Convert.ToInt16(c.G)
                    Else
                        arIntR(6) = 0
                        arIntA(6) = 0
                        arIntB(6) = 0
                        arIntG(6) = 0
                    End If
                    If ((iHeight + (1 < img.Width)) AndAlso (iWidth + (1 < img.Height))) Then
                        c = img.GetPixel((iHeight + 1), (iWidth + 1))
                        arIntR(7) = Convert.ToInt16(c.R)
                        arIntA(7) = Convert.ToInt16(c.A)
                        arIntB(7) = Convert.ToInt16(c.B)/>
                        arIntG(7) = Convert.ToInt16(c.G)
                    Else
                        arIntR(7) = 0
                        arIntA(7) = 0
                        arIntB(7) = 0
                        arIntG(7) = 0
                    End If
                    c = img.GetPixel(iHeight, iWidth)
                    arIntR(8) = Convert.ToInt16(c.R)
                    arIntA(8) = Convert.ToInt16(c.A)
                    arIntB(8) = Convert.ToInt16(c.B)/>
                    arIntG(8) = Convert.ToInt16(c.G)
                    Array.Sort(arIntR) : Array.Sort(arIntA) : Array.Sort(arIntG) : Array.Sort(arIntB)
                    Dim iMidR As Integer = arIntR(4), iMidA As Integer = arIntA(4), iMidB As Integer = arIntB(4), iMidG As Integer = arIntG(4)
                    img.SetPixel(iHeight, iWidth, Color.FromArgb(iMidA, iMidR, iMidG, iMidB))
                Next
            Next
            img2 = New Bitmap(img)
            Array.Clear(arIntR, arIntR.GetLowerBound(0), arIntR.Length)
            Array.Clear(arIntA, arIntA.GetLowerBound(0), arIntA.Length)
            Array.Clear(arIntB, arIntB.GetLowerBound(0), arIntB.Length)
            Array.Clear(arIntG, arIntG.GetLowerBound(0), arIntG.Length)
        Catch ex As Exception
            MsgBox("Error:" & Err.Number & vbCrLf & Err.Description & vbCrLf & Err.HelpFile)
        Finally
            img2.Save("C:\Downloads\MeanTest.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
            img.Dispose() : img2.Dispose()
        End Try
    End Sub



Anyone run into this before?

Is This A Good Question/Topic? 0
  • +

Replies To: VB.net Median Filter

#2 andrewsw  Icon User is offline

  • lashings of ginger beer
  • member icon

Reputation: 6333
  • View blog
  • Posts: 25,530
  • Joined: 12-December 12

Re: VB.net Median Filter

Posted 29 October 2013 - 01:38 PM

There is no image attached but, regardless, what is a "variable error"? Post the full error message together with confirmation as to which line it refers to in your posted code.

When you receive an error you can normally press Break and it takes you to the line that generated the error.

I've moved this to the VB.NET forum. It is not an advanced discussion.
Was This Post Helpful? 0
  • +
  • -

#3 alapee  Icon User is offline

  • Stressed Out Programmer

Reputation: 214
  • View blog
  • Posts: 2,464
  • Joined: 24-October 13

Re: VB.net Median Filter

Posted 29 October 2013 - 01:59 PM

Dang it I thought I posted in the Vb.net forum.
Error:
Posted Image
Occurs before Line 164, width iWidth = 120

This post has been edited by alapee: 29 October 2013 - 02:01 PM

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • lashings of ginger beer
  • member icon

Reputation: 6333
  • View blog
  • Posts: 25,530
  • Joined: 12-December 12

Re: VB.net Median Filter

Posted 29 October 2013 - 02:22 PM

Your code only goes up to line 126.

The error message seems quite clear though.

This is your own error-message prompt, from your handler. If you disable this handler you will get a fuller error message and it can take you to the line that generated the error.

This post has been edited by andrewsw: 29 October 2013 - 02:22 PM

Was This Post Helpful? 0
  • +
  • -

#5 alapee  Icon User is offline

  • Stressed Out Programmer

Reputation: 214
  • View blog
  • Posts: 2,464
  • Joined: 24-October 13

Re: VB.net Median Filter

Posted 29 October 2013 - 02:27 PM

View Postalapee, on 29 October 2013 - 03:59 PM, said:

Dang it I thought I posted in the Vb.net forum.
Error:
Posted Image
Occurs before Line 164, width iWidth = 120

Corrections Should be Line 114, with iWidth = 120, I forgot I didn't copy the rest of the module and then referred back to the module for the line number. I think I am starting to develop coditis.
[Update]
Ok, maybe it was the error handler. It didn't error out on me after taking the Error Handler out

This post has been edited by alapee: 29 October 2013 - 02:31 PM

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • lashings of ginger beer
  • member icon

Reputation: 6333
  • View blog
  • Posts: 25,530
  • Joined: 12-December 12

Re: VB.net Median Filter

Posted 29 October 2013 - 02:31 PM

Line 114 in your posted code just says Next.

As already suggested, if you disable your error handler you will receive more information and it will take you to the specific line that generated the error.
Was This Post Helpful? 0
  • +
  • -

#7 _ELement_8215  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 34
  • Joined: 14-December 11

Re: VB.net Median Filter

Posted 29 October 2013 - 02:33 PM

At first glance, looks like maybe an IndexOutOfRange in one of the loops, but yeah, comment out your own error handling to see Visual Studio's own error message about what's happening.
Was This Post Helpful? 0
  • +
  • -

#8 alapee  Icon User is offline

  • Stressed Out Programmer

Reputation: 214
  • View blog
  • Posts: 2,464
  • Joined: 24-October 13

Re: VB.net Median Filter

Posted 29 October 2013 - 02:46 PM

View Postalapee, on 29 October 2013 - 04:27 PM, said:

[Update]
Ok, maybe it was the error handler. It didn't error out on me after taking the Error Handler out

No error After it was commented out
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: VB.net Median Filter

Posted 29 October 2013 - 03:07 PM

Of course it does if you just done this.
 Catch ex As Exception
   '  MsgBox("Error:" & Err.Number & vbCrLf & Err.Description & vbCrLf & Err.HelpFile)
  Finally


As it essentially says whatever the exception, proceed to the finally.

MessageBox.Show(ex.ToString)


Was This Post Helpful? 1
  • +
  • -

#10 alapee  Icon User is offline

  • Stressed Out Programmer

Reputation: 214
  • View blog
  • Posts: 2,464
  • Joined: 24-October 13

Re: VB.net Median Filter

Posted 30 October 2013 - 06:34 AM

View PostAdamSpeight2008, on 29 October 2013 - 05:07 PM, said:

Of course it does if you just done this.
 Catch ex As Exception
   '  MsgBox("Error:" & Err.Number & vbCrLf & Err.Description & vbCrLf & Err.HelpFile)
  Finally


As it essentially says whatever the exception, proceed to the finally.

MessageBox.Show(ex.ToString)


I figured that one out from your code before you posted lol
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1