Question

I am computing the ROI with a moving rectangle and extracting the ROI to compute the standard deviation, mean, area and Pixel value coordinates X and Y in a seperate form2 by clicking the mouse. At this juncture I am trying to pass a function from the main Form that loads the Image and displays the rectangle to another Form that has the displayed properties of the mean and standard deviation etc. However, I'm receiving errors in runtime in the function that contains the standard deviation. The error displayed is

Index was outside the bounds of the array.

It is displayed at the end of this portion of the code in the function StD, i.e at the end of the mean part'

SD(count) = Double.Parse(pixelcolor.R) + Double.Parse(pixelcolor.G) + Double.Parse(pixelcolor.B) - mean

what is this actually saying and how can I fix this situation. Any tips and ideas, thanks.

My code is at the bottom

enterPublic Function StD(ByVal image As Bitmap, ByVal mean As Double, ByVal meancount As Integer) As Double
    Dim SD(SquareHeight * SquareWidth) As Double
    Dim count As Integer = 0
    For i = 0 To SquareWidth
        For j = 0 To SquareHeight
            Dim pixelcolor As Color = image.GetPixel(i, j)

            SD(count) = Double.Parse(pixelcolor.R) + Double.Parse(pixelcolor.G) + Double.Parse(pixelcolor.B) - mean
            count += 1
        Next
    Next

    Dim SDsum As Double = 0
    For i = 0 To count
        SDsum = SDsum + SD(i)
    Next

    SDsum = SDsum / (SquareHeight * SquareWidth)

    SDsum = ((SDsum) ^ (1 / 2))
    Return SDsum

End Function code here

I would like to pass this using the code below

enterPrivate Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown

    Dim mean As Double = 0
    Dim meancount As Integer = 0
    Dim bmap As New Bitmap(400, 400)
    bmap = PictureBox1.Image
    Dim colorpixel As Color = bmap.GetPixel(e.X, e.Y)
    '   Dim pixels As Double = colorpixel.R + colorpixel.G + colorpixel.B
    If e.Button = Windows.Forms.MouseButtons.Left AndAlso Rect.Contains(e.Location) Then
        If (PictureBox1.Image Is Nothing) Or (PictureBox1.Height - (e.Y + SquareHeight) < 0) Or (PictureBox1.Width - (e.X + SquareWidth) < 0) Then
        Else
            Dim ROI As New Bitmap(400, 400)
            Dim x As Integer = 0
            Dim countx As Integer = 0
            Dim county As Integer = 0

            For i = e.X To (e.X + SquareWidth)
                For j = (e.Y + x) To (e.Y + SquareHeight)
                    Dim pixelcolor As Color = bmap.GetPixel(i, j)
                    ROI.SetPixel(countx, county, pixelcolor)
                    mean = mean + pixelcolor.R + pixelcolor.G + pixelcolor.B
                    county += 1
                    meancount += 1
                Next
                county = 0
                countx += 1
                x = x + 1
            Next

            mean = mean / (meancount * 3)
            Dim SD = mean - 75
            Dim area As Integer = (SquareHeight * SquareWidth)
            Dim anotherForm As Form2
            anotherForm = New Form2(mean, StD(bmap, mean, meancount), area, 34)
            anotherForm.Show()
        End If
    End If

    '   Catch ex As Exception
    '   MessageBox.Show(ex.Message())
    '  End Try
End Sub code here

To be displayed with this code

enter Public Sub New(ByVal mean As Double, ByVal StD As Double, ByVal Area As Integer, ByVal pixel As Double)
    MyBase.New()
    InitializeComponent()
    TextBox1.Text = mean.ToString()
    TextBox2.Text = StD.ToString()
    TextBox3.Text = Area.ToString()
    TextBox4.Text = pixel.ToString()

End Sub code here
Was it helpful?

Solution

The problem probably is because of these lines:

For i = 0 To SquareWidth
        For j = 0 To SquareHeight

Try using this instead:

For i = 0 To SquareWidth - 1
        For j = 0 To SquareHeight - 1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top