0

我有一个灰度图像,我已经能够在图像上显示一个矩形来提取 ROI 并计算标准偏差和平均值。但是,当我移动矩形时,我注意到图像的值错误,尤其是在图像完全是黑色的情况下。在这个区域,我的平均值和标准差应该为零。然而,我得到一个 0 的平均值和负值的标准偏差和这个黑色区域的其他点,平均值增加到与光主导区域相同的平均值。我一直在努力想出一个解决方案。有什么想法可以帮忙。我会很感激的。我的mousemove按钮代码以及平均值和标准差发布在下面。谢谢。

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
4

1 回答 1

0

你应该计算平均值 mean = mean / (meancount * 3)(添加了 3 种颜色),最好计算运行平均值,而不是全部相加然后再除。我想可能会有更多的数学错误。

于 2013-07-08T17:33:26.013 回答