实在找不出问题才到这请教啊!!下面的程序是我用单片机采集的数据传给串口,然后在上位机上做图像还原。单片机每次传送80个数据(采集的图像是80*80的),上位机收到80个数就传回一个02代表本次数据传输正确,然后用个80*80的二维数组盛放采集的数据。现在的问题是,只要我一点击Command3,,也就是调用Bayer to RGB的算法处理,就会弹出“实时错误 溢出 6”,可是我调试的时候发现每个Bayer的值都是小于255的啊,怎么会产生溢出错误呢?实在想不明白啊!!!!!!下面是程序:
Dim Bayer(0 To 79, 0 To 79) As Byte
Dim R As Byte, G As Byte, B As Byte
Dim x As Integer, y As Integer
'以上均是全局变量
'下面是bayer to RGB 的算法
Function BayerToRGB24(Column As Integer, Line As Integer) As Long 'Column,Line分别代表行 列
'~~~~~~~~~~~~~~~~~~~处理图像的四个顶点~~~~~~~~~~~~~~~~~
If (Column = 0 And Line = 0) Then
R = Bayer(1, 1)
G = (Bayer(1, 0) + Bayer(0, 1)) \ 2
B = Bayer(0, 0)
ElseIf (Column = 79 And Line = 0) Then
R = Bayer(79, 1)
G = (Bayer(78, 1) + Bayer(79, 0)) \ 2
B = Bayer(78, 0)
ElseIf (Column = 0 And Line = 79) Then
R = Bayer(1, 79)
G = (Bayer(0, 79) + Bayer(1, 78)) \ 2
B = Bayer(0, 78)
ElseIf (Column = 79 And Line = 79) Then
R = Bayer(79, 79)
G = (Bayer(78, 79) + Bayer(79, 78)) \ 2
B = Bayer(78, 78)
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'~~~~~~~~~~~~~~处理四个边框处的数据~~~~~~~~~~~~~~~~~~~~~~
ElseIf (Line = 0 And (Column Or 0 And Column <> 79)) Then
If (Column Mod 2 <> 0) Then '处理第一行偶数列的数据
R = Bayer(Column, Line + 1)
G = Bayer(Column, Line)
B = (Bayer(Column + 1, Line) + Bayer(Column - 1, Line)) \ 2
Else '处理第一行奇数列的数据
R = (Bayer(Column - 1, Line + 1) + Bayer(Column + 1, Line + 1)) \ 2
G = (Bayer(Column, Line + 1) + Bayer(Column - 1, Line) + Bayer(Column + 1, Line)) \ 3
B = Bayer(Column, Line)
End If
ElseIf (Line = 79 And (Column <> 0 Or Column <> 79)) Then
If (Column Mod 2 = 0) Then '处理最后一行奇数列的数据
R = Bayer(Column, Line)
G = (Bayer(Column - 1, Line) + Bayer(Column + 1, Line) + Bayer(Column, Line - 1)) \ 3
B = (Bayer(Column - 1, Line - 1) + Bayer(Column + 1, Line - 1)) \ 2
Else
R = (Bayer(Column - 1, Line) + Bayer(Column + 1, Line)) \ 2
G = Bayer(Column, Line)
B = Bayer(Column, Line - 1)
End If
ElseIf (Column = 0 And (Line <> 0 Or Line <> 79)) Then
If (Line Mod 2 <> 0) Then
R = Bayer(Column + 1, Line)
G = Bayer(Column, Line)
B = (Bayer(Column, Line - 1) + Bayer(Column, Line + 1)) \ 2
Else
R = (Bayer(Column + 1, Line - 1) + Bayer(Column + 1, Line + 1)) \ 2
G = (Bayer(Column, Line - 1) + Bayer(Column, Line + 1) + Bayer(Column + 1, Line)) \ 3
B = Bayer(Column, Line)
End If
ElseIf (Column = 79 And (Line <> 0 Or Line <> 79)) Then
If (Line Mod 2 <> 0) Then
R = Bayer(Column, Line)
G = (Bayer(Column - 1, Line) + Bayer(Column, Line - 1) + Bayer(Column, Line + 1)) \ 3
B = (Bayer(Column - 1, Line - 1) + Bayer(Column - 1, Line + 1)) \ 2
Else
R = (Bayer(Column, Line - 1) + Bayer(Column, Line + 1)) \ 2
G = Bayer(Column, Line)
B = Bayer(Column - 1, Line)
End If
'~~~~~~~~~~~~~~~~~~常规处理~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Else
If (Line Mod 2 <> 0) Then '偶数行
If (Column Mod 2 <> 0) Then '偶数行偶数列
R = Bayer(Column, Line)
G = (Bayer(Column - 1, Line) + Bayer(Column + 1, Line) + Bayer(Column, Line - 1) + Bayer(Column, Line + 1)) \ 4
B = (Bayer(Column - 1, Line - 1) + Bayer(Column - 1, Line + 1) + Bayer(Column + 1, Line - 1) + Bayer(Column + 1, Line + 1)) \ 4
Else '偶数行奇数列
R = (Bayer(Column - 1, Line) + Bayer(Column + 1, Line)) \ 2
G = Bayer(Column, Line)
B = (Bayer(Column, Line - 1) + Bayer(Column, Line + 1)) \ 2
End If
Else '奇数行
If (Column Mod 2 <> 0) Then '奇数行偶数列
R = (Bayer(Column, Line - 1) + Bayer(Column, Line + 1)) \ 2
G = Bayer(Column, Line)
B = (Bayer(Column - 1, Line) + Bayer(Column + 1, Line)) \ 2
Else '奇数行奇数列
R = (Bayer(Column - 1, Line - 1) + Bayer(Column + 1, Line - 1) + Bayer(Column - 1, Line + 1) + Bayer(Column + 1, Line + 1)) \ 4
End If
End If
End If
End Function
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'~~~~~~~~~~~~~~~~下面是触发算法还原并将每个像素的RGB显示在PICTURE1控件上的事件~~~~~~~
Private Sub Command3_Click()
Dim i As Integer, j As Integer
For j = 0 To 79
For i = 0 To 79
Call BayerToRGB24(i, j)
Picture1.PSet (i, j), RGB(R, G, B)
Next i
Next j
’Picture2.Picture = LoadPicture("E:\文件\西华杯\航拍系统\VB上位机\white.bmp")
End Sub
'~~~~~~~~~~~~~~~~~~~~下面是接受数据的函数~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Private Sub MSComm1_OnComm()
Dim bytInput() As Byte
Dim intInputLen As Integer
Dim n As Integer
Dim teststring As String
Select Case MSComm1.CommEvent
Case comEvReceive
If Option1.Value = True Then
MSComm1.InputMode = 1 '0:文本方式,1:二进制方式
Else
MSComm1.InputMode = 0 '0:文本方式,1:二进制方式
End If
intInputLen = MSComm1.InBufferCount
bytInput = MSComm1.Input
If Option1.Value = True Then
For n = 0 To intInputLen - 1
Bayer(x, y) = bytInput(n)
x = x + 1
If x = 80 Then
x = 0
y = y + 1
If y = 80 Then
' y = 0
MSComm1.RThreshold = 0 '禁止事件发生
Picture2.Picture = LoadPicture("E:\文件\西华杯\航拍系统\VB上位机\red.bmp")
End If
End If
Next n
Else
teststring = bytInput
Text1.Text = Text1.Text + teststring
End If
'~~~~~~~~~~~~接受一行数据后,返回成功接受指令0x02~~~~~~~~~~~~~~
Dim lens As Integer
strSendText = "02"
lens = strHexToByteArray(strSendText, bytSendByte())
If lens > 0 Then
MSComm1.Output = bytSendByte
End If
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
End Select
End Sub
各位高手帮帮忙啊,这个问题一直没解决掉。 |