2013-07-08 103 views
0

移动矩形我有一个灰度图像的平均值,我一直是能够在图像上显示的矩形提取ROI和计算标准偏差和平均。但是,当我移动矩形时,我注意到图像有错误的值,尤其是图像完全黑色的地方。在这个领域,我应该得到一个零均值和标准差。然而,即时通讯获得0的平均值和负值的标准差和这个黑色区域的其他点平均增加到平均值与光主导地区相同。我一直在努力想出一个解决方案。有什么想法可以帮助吗?我会很感激。我的代码为鼠标移动按钮和平均值和标准偏差张贴如下。谢谢。获得不正确的标准偏差,并用在VB2010

Private 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 
      Dim SD = mean - 75 
      Dim area As Integer = (SquareHeight * SquareWidth) 
      Dim anotherForm As Form2 
      anotherForm = New Form2(mean, SD, area, 34) 
      anotherForm.Show() 
     End If 
    End If 

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

Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove 
    Rect.X = e.X + x 
    Rect.Y = e.Y + y 
    Rect.Width = SquareWidth 
    Rect.Height = SquareHeight 
    ' Label1.Text = "XRect: " + Rect.X.ToString() + " YRect: " + Rect.Y.ToString() + " Xmouse " + e.X.ToString() + " Ymouse " + e.Y.ToString() 
    PictureBox1.Refresh() 
End Sub 

Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint 
    e.Graphics.DrawRectangle(Pens.Red, Rect) 
End Sub 

Private Function StandardDeviation(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 

Private Sub PictureBox1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseClick 
    Dim P As Point = e.Location 
    P = New Point 
End Sub 

回答

0

你应该calc下的意思是像 mean = mean/(meancount * 3)(有添加3种颜色),它是更好地运行calc下,而不是平均的汇总所有后来划分。我想可以有更多的数学错误。

+0

我想它提高了平均的结果,但它的关闭一些。不过,我想问一个问题。鉴于我的标准偏差功能我如何传递到了newform看来,如果我能通过标准偏差功能到我的标准偏差值将是正确的平均=平均/ meancount 点心SD =平均值 - 75 昏暗的区域作为整数=(SquareHeight * SquareWidth) 昏暗anotherForm如窗体2 anotherForm =新窗体2(平均,SD,区域,34) anotherForm.Show() –