2013-07-09 33 views
0

我正在計算具有移動矩形的ROI並提取ROI以通過單擊鼠標來計算單獨格式2中的標準偏差,平均值,面積和像素值座標X和Y.在這個時候,我試圖從加載圖像的主窗體傳遞一個函數,並將矩形顯示到具有顯示的平均值和標準偏差等屬性的另一個窗體。但是,我在函數的運行時接收到錯誤包含標準偏差。顯示的錯誤是運行時錯誤消息索引超出了數組的範圍。對於Visual Basic 2010

索引超出了數組的範圍。

它在功能STD中的代碼的該部分的端部顯示,即在平均部分」

SD的端部(計數)= Double.Parse(pixelcolor.R)+雙人。解析(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 

回答

1

可能的問題顯示是因爲這些行:

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

嘗試使用這個:

For i = 0 To SquareWidth - 1 
     For j = 0 To SquareHeight - 1 
+0

是,由於這個工程,但我有一個問題要問你,我與已經光集中在一端黑暗的以外的灰度圖像加工結束。將矩形移動到黑色部分時,我的意思是0,但是我的標準偏差也應該是0.但是我的標準偏差是27.65。這是不正確的,它也應該是0。從我的標準偏差函數。我已經掃描它的錯誤,但一切似乎都很好,而且當我在黑色部分的圖像的下半部分,我在運行時得到這個錯誤。參數必須是正數並且<高度。參數名稱:y –

相關問題