6 Replies - 811 Views - Last Post: 17 April 2013 - 03:52 PM Rate Topic: -----

#1 fomaibuh265  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-May 12

Face recognition software error array length

Posted 16 April 2013 - 05:55 PM

So I am starting to make a facial recognition software. The way it works so far is it looks through all the pixels in an image the user submits and sees if they have ratios corresponding to skin colors, then it runs a loop to find where that stretch of skin colored pixels ends and adds them to an array. Then it changes all of those pixels into red pixels so I can see if it is working. It works ok with most pictures but with some it gives me errors about the size of the arrays startpointx and endpointx


Public Class Form1

    Public Function GetPixel( _
ByVal x As Integer, _
ByVal y As Integer _
) As Color

    End Function

    Public Function SetPixel( _
ByRef x As Integer, _
ByRef y As Integer, _
ByRef red As Integer, _
ByRef blue As Integer, _
ByRef green As Integer
) As Color

    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim image As New Bitmap(PictureBox1.Image)


        Dim head As Bitmap


        Dim startpointx(0) As Integer
        Dim startpointy(0) As Integer
        Dim endpointx(0) As Integer
        Dim endpointy(0) As Integer
        Dim height As Integer
        Dim width As Integer


        For y As Integer = 0 To image.Height - 1
            For x As Integer = 0 To image.Width - 1
                Dim redval As Integer = image.GetPixel(x, y).R
                Dim blueval As Integer = image.GetPixel(x, y).B
                Dim greenval As Integer = image.GetPixel(x, y).G
                Static countervar As Integer = 0
                If Math.Abs(1.5 - (redval / blueval)) < 0.5 And Math.Abs(1.15 - (greenval / blueval)) < 0.5 Then

                    startpointx(countervar) = x
                    startpointy(countervar) = y
                    countervar += 1
                    ReDim Preserve startpointx(countervar)
                    ReDim Preserve startpointy(countervar)
                    Dim looper As Integer

                    Do While Math.Abs(1.5 - (image.GetPixel(looper, y).R) / (image.GetPixel(looper, y).B)/>) < 0.5 And Math.Abs(1.15 - (image.GetPixel(looper, y).G) / (image.GetPixel(looper, y).B)/>) < 0.5 And looper < image.Width
                        looper = x
                        looper += 1

                    Loop
                    endpointx(countervar - 1) = looper
                    ReDim Preserve endpointx(countervar)
                End If

            Next
        Next


        MessageBox.Show(startpointx.Length)

        For i As Integer = 0 To (startpointx.Length - 1)
            image.SetPixel(startpointx(i), startpointy(i), Color.FromArgb(255, 0, 0))
        Next

        PictureBox1.Image = image

    End Sub

    
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        OpenFileDialog1.Title = "Please Select a File"
        OpenFileDialog1.InitialDirectory = "C:temp"

        OpenFileDialog1.ShowDialog()
        Dim filename As String = OpenFileDialog1.FileName
        Dim image As New Bitmap(filename)
        PictureBox1.Image = image
    End Sub




Is This A Good Question/Topic? 0
  • +

Replies To: Face recognition software error array length

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,357
  • Joined: 02-June 10

Re: Face recognition software error array length

Posted 16 April 2013 - 10:50 PM

What are the exact errors and what lines are they on?
A screen shot of the IDE when the exception occurs would help.

But in general array index errors are very basic debugging so the tutorials below should help.


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Apr 2013
Spoiler



Was This Post Helpful? 0
  • +
  • -

#3 fomaibuh265  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-May 12

Re: Face recognition software error array length

Posted 17 April 2013 - 05:32 AM

I get an error on line 44 saying that the index was outside the bounds of the array
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is online

  • This space intentionally left blank
  • member icon

Reputation: 1533
  • View blog
  • Posts: 4,919
  • Joined: 25-September 09

Re: Face recognition software error array length

Posted 17 April 2013 - 05:36 AM

that's a pretty clear-cut error message.

You have an array initialized with 0

Then you try to add some items to that array but you are not resizing the array to allow for the new information that goes beyond what you initially declared it with.
Let me rephrase that so as not to confuse you with the Redim Preserve lines.

Better yet, put a breakpoint and possibly a watch on countervar and see if it's doing what you think its supposed to.

Remember, even though you declare a variable Static myVar =0, it retains its last value
Example
For x as integer = 1 to 2
  Static myVar as Integer = 0
  myVar = x
  debug.writeline(myVar)
next

The output would be
1
2
Even though with each iteration it would appear that you are setting it back to 0.

This post has been edited by CharlieMay: 17 April 2013 - 05:56 AM

Was This Post Helpful? 1
  • +
  • -

#5 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 154
  • View blog
  • Posts: 570
  • Joined: 01-February 13

Re: Face recognition software error array length

Posted 17 April 2013 - 05:56 AM

View PostCharlieMay, on 17 April 2013 - 07:36 AM, said:

that's a pretty clear-cut error message.

You have an array initialized with 0

Then you try to add some items to that array but you are not resizing the array to allow for the new information that goes beyond what you initially declared it with.

Actually it gets assigned a value and then on the next line he uses (ReDim) to resize it 1 bigger. I tinkered with it last night and declared the variables to the (width * height) value before the loops started and removed the ReDim statements and it kept freezing up on one picture i tried but, i could not figure out if it was freezing in the X Y loops or the Do loop. It was up to about 15735 when it froze on me :unsure:
Was This Post Helpful? 0
  • +
  • -

#6 fomaibuh265  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-May 12

Re: Face recognition software error array length

Posted 17 April 2013 - 07:19 AM

CharlieMay good call on the static variable I changed it to a Dimmed one and changed the code a little. Now I don't get the error but it still takes a very long time. I'm thinking of dividing the pictures into 10 rows and doing the code but ten times. Anybody else have ideas on how to increase efficiency?
Was This Post Helpful? 0
  • +
  • -

#7 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Face recognition software error array length

Posted 17 April 2013 - 03:52 PM

View Postfomaibuh265, on 17 April 2013 - 08:19 AM, said:

CharlieMay good call on the static variable I changed it to a Dimmed one and changed the code a little. Now I don't get the error but it still takes a very long time. I'm thinking of dividing the pictures into 10 rows and doing the code but ten times. Anybody else have ideas on how to increase efficiency?

I might have a few ideas, but it would help to know what you are actually looking for. Is it that you are looking for faces in general, or are you looking to recognize individual faces. In other words, would just finding faces be enough, or would you want to identify the face of a particular person?

Your idea of splitting up the picture is good, but if you are going to scan all the parts anyway, it will take the same time, and the only advantage wil be that you can show progress as it happens to each part.

Another thought would be to sample less often. Say you have a 400*400 pixel image. Insted of sampling every pixel in every row, you might want to sample every row/n for every column/n, where n is a reasonable division that is likely to always hit a face that is of a size below which you wouldn't be interested. When you find a flesh colour, you could use that point as a starting point, sampling up/down/left/right in a pattern that will find all the boundaries of the flesh-coloured areas.

An additional advantage would be that you could probably cut down the range of flesh colours after finding a point that fits the criteria, narrowing in on a range of colours for different races.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1