Vb.net 在VB2010中使用移动矩形获得了不正确的标准偏差和平均值
我有一个灰度图像,我能够在图像上显示一个矩形来提取ROI并计算标准偏差和平均值。但是,当我移动矩形时,我注意到图像的值是错误的,尤其是在图像完全为黑色的情况下。在这个区域,平均值和标准偏差应该为零。然而,对于标准偏差和该黑色区域的其他点,我获得的平均值和负值为0,平均值增加到与光主导区域相同的平均值。我一直在努力想出一个解决办法。有什么办法可以帮助你吗。我会很感激的。我的mousemove按钮代码以及平均值和标准偏差发布在下面。谢谢Vb.net 在VB2010中使用移动矩形获得了不正确的标准偏差和平均值,vb.net,image-processing,Vb.net,Image Processing,我有一个灰度图像,我能够在图像上显示一个矩形来提取ROI并计算标准偏差和平均值。但是,当我移动矩形时,我注意到图像的值是错误的,尤其是在图像完全为黑色的情况下。在这个区域,平均值和标准偏差应该为零。然而,对于标准偏差和该黑色区域的其他点,我获得的平均值和负值为0,平均值增加到与光主导区域相同的平均值。我一直在努力想出一个解决办法。有什么办法可以帮助你吗。我会很感激的。我的mousemove按钮代码以及平均值和标准偏差发布在下面。谢谢 Private Sub PictureBox1_MouseDo
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
Private Sub PictureBox1\u MouseDown(ByVal sender作为System.Object,ByVal e作为System.Windows.Forms.MouseEventArgs)处理PictureBox1.MouseDown
双精度平均值=0
Dim meancount为整数=0
将bmap变暗为新位图(400400)
bmap=PictureBox1.Image
Dim colorpixel As Color=bmap.GetPixel(e.X,e.Y)
'Dim pixels As Double=colorpixel.R+colorpixel.G+colorpixel.B
如果e.Button=Windows.Forms.MouseButtons.Left和also Rect.Contains(e.Location),则
如果(PictureBox1.Image为Nothing)或(PictureBox1.Height-(e.Y+SquareHeight)<0)或(PictureBox1.Width-(e.X+SquareWidth)<0),则
其他的
将ROI变暗为新位图(400400)
尺寸x为整数=0
整数形式的Dim countx=0
作为整数的Dim县=0
对于i=e.X至(e.X+平方宽度)
对于j=(e.Y+x)到(e.Y+SquareHeight)
Dim pixelcolor As Color=bmap.GetPixel(i,j)
ROI.SetPixel(countx、county、pixelcolor)
平均值=平均值+像素颜色.R+像素颜色.G+像素颜色.B
县+=1
平均计数+=1
下一个
县=0
countx+=1
x=x+1
下一个
平均数=平均数/平均数
尺寸SD=平均值-75
作为整数的变暗区域=(平方高度*平方宽度)
另一种形式如表2所示
另一种形式=新形式2(平均值,标准差,面积,34)
另一个表单Show()
如果结束
如果结束
“特例
'MessageBox.Show(例如Message())
“结束尝试
端接头
私有子PictureBox1\u MouseMove(ByVal sender作为System.Object,ByVal e作为System.Windows.Forms.MouseEventArgs)处理PictureBox1.MouseMove
矩形X=e.X+X
矩形Y=e.Y+Y
矩形宽度=方形宽度
矩形高度=方形高度
'Label1.Text=“XRect:”+Rect.X.ToString()+“YRect:”+Rect.Y.ToString()+“Xmouse”+e.X.ToString()+“Ymouse”+e.Y.ToString())
PictureBox1.Refresh()
端接头
私有子PictureBox1_Paint(ByVal sender作为System.Object,ByVal e作为System.Windows.Forms.PaintEventArgs)处理PictureBox1.Paint
e、 图形.绘图矩形(Pens.Red,Rect)
端接头
专用函数标准偏差(ByVal图像为位图,ByVal均值为双精度,ByVal均值为整数)为双精度
尺寸SD(平方高度*平方宽度)为双精度
Dim计数为整数=0
对于i=0到平方宽度
对于j=0到平方高度
Dim pixelcolor As Color=image.GetPixel(i,j)
SD(count)=Double.Parse(pixelcolor.R)+Double.Parse(pixelcolor.G)+Double.Parse(pixelcolor.B)-平均值
计数+=1
下一个
下一个
点心SDsum作为双精度=0
对于i=0进行计数
SDsum=SDsum+SD(一)
下一个
SDsum=SDsum/(平方高度*平方宽度)
SDsum=((SDsum)^(1/2))
返回SDsum
端函数
私有子PictureBox1\u鼠标单击(ByVal sender作为System.Object,ByVal e作为System.Windows.Forms.MouseEventArgs)处理PictureBox1.MouseClick
尺寸P作为点=e位置
P=新点
端接头
你应该这样计算
mean=mean/(meancount*3)
(添加了3种颜色),最好计算运行的平均值,而不是将所有值相加,然后再除以。我想可能会有更多的数学错误。我试过了,平均值的结果有所改善,但有点不对劲。不过,我想问一个问题。给定我的标准偏差函数,我如何将其传递给新的形式,如果我能将标准偏差函数传递到,我的标准偏差值将是正确的,平均值=平均值/平均计数Dim SD=平均值-75 Dim area As Integer=(平方高*平方宽)Dim anotherForm As Form2 anotherForm=新的Form2(平均值、标准差、面积、34)anotherForm.Show()