0

我正在使用移动矩形计算 ROI,并提取 ROI 以通过单击鼠标以单独的形式 2 计算标准偏差、平均值、面积和像素值坐标 X 和 Y。在这个时刻,我试图将一个函数从加载图像并显示矩形的主窗体传递到另一个窗体,该窗体具有显示的平均值和标准差等属性。但是,我在函数的运行时收到错误包含标准偏差。显示的错误是

指数数组的边界之外。

它显示在函数 StD 中这部分代码的末尾,即平均部分的末尾

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

这实际上是在说什么,我该如何解决这种情况。任何提示和想法,谢谢。

我的代码在底部

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

我想使用下面的代码传递它

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

与此代码一起显示

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
4

1 回答 1

1

问题可能是因为以下几行:

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

尝试改用这个:

For i = 0 To SquareWidth - 1
        For j = 0 To SquareHeight - 1
于 2013-07-09T16:10:02.037 回答