📄 imageprocess.bas
字号:
Attribute VB_Name = "ImageProcess"
'ImageProcess.bas
'Some functions about image porcess
'- - - - - - - - - - - - - - - - - - - - - - - - - -
'捕捉并处理图像
'ProcessImage and Capture
'used the VideoOCX.ocx in this program
Public Sub ProcessImage()
Dim t As Integer
Dim px, py As Integer
Dim X, Y As Integer
Dim u, v As Integer
'define std+color
Dim stdNone As Col
Dim stdBlack As Col
Dim stdWhite As Col
'a num between none and white
Dim avNW As Integer
Dim avBN As Integer
'How many stones do GoRo see?
Dim NumofStones As Integer
NumofStones = 0
'The number of either color
Dim isB, isW, isN As Integer
'Initialize Camera
Call InitCam
'Capture One Image
GRVBMain.VideoBOX.Capture (Board_Image)
'Get access to the image pixel matrix
matrix = GRVBMain.VideoBOX.GetMatrix(Board_Image)
'看辅助点,求平均值
Dim fx, fy As Integer
Dim av As Long
''look white-------------------------------------------------------------------------------
av = 0
For fx = (stdPWhite.X - 6) / kCam To (stdPWhite.X + 6) / kCam
For fy = (stdPWhite.Y - 6) / kCam To (stdPWhite.Y + 6) / kCam
av = av + matrix(fx, fy, R):
matrix(fx, fy, G) = 255
Next fy:
Next fx
stdWhite.R = av / ((6 / kCam) * 2 + 1) / ((6 / kCam) * 2 + 1)
''look black-------------------------------------------------------------------------------
av = 0
For fx = (stdPBlack.X - 6) / kCam To (stdPBlack.X + 6) / kCam
For fy = (stdPBlack.Y - 6) / kCam To (stdPBlack.Y + 6) / kCam
av = av + matrix(fx, fy, R):
matrix(fx, fy, G) = 255
Next fy:
Next fx
stdBlack.R = av / ((6 / kCam) * 2 + 1) / ((6 / kCam) * 2 + 1)
''look none-------------------------------------------------------------------------------
av = 0
For fx = (stdPNone.X - 6) / kCam To (stdPNone.X + 6) / kCam
For fy = (stdPNone.Y - 6) / kCam To (stdPNone.Y + 6) / kCam
av = av + matrix(fx, fy, R):
matrix(fx, fy, G) = 255
Next fy:
Next fx
stdNone.R = av / ((6 / kCam) * 2 + 1) / ((6 / kCam) * 2 + 1)
'-----------------------------------------------------------------------------------------
'show
ToList "W-" & str$(stdWhite.R) & " | N-" & str$(stdNone.R) & " | B-" & str$(stdBlack.R)
'求均值
avNW = (stdWhite.R + stdNone.R) / 2
avBN = (stdNone.R + stdBlack.R) / 4
'Process the image
For posx = 1 To BSize
For posy = 1 To BSize
u = Pos(posx, posy).X
v = Pos(posx, posy).Y
If u < W Or v < H Then Over
av = 0
'Calculate the each point
For X = u - W / kCam To u + W / kCam
For Y = v - H / kCam To v + H / kCam
av = av + matrix(X, Y, R)
matrix(X, Y, b) = 255
Next
Next
av = av / (2 * W + 1) / (2 * W + 1)
'You can see the Color Info & Number of a point from List Box
If av > avNW Then
ToList "(" & str$(posx) & "," & str$(posy) & ")-白-" & str$(av)
State(posx, posy) = WHITE
End If
If av < avBN Then
ToList "(" & str$(posx) & "," & str$(posy) & ")-黑-" & str$(av)
State(posy, posx) = BLACK
End If
'If av < avNW Then GRVBMain.List.AddItem "(" & str$(posx) & "," & str$(posy) & ")-无-" & str$(av)
Next 'posx
Next 'posy'
'end out
GRVBMain.List.AddItem "__________________"
'You can see output image file
'Call GRVBMain.VideoBOX.SaveBMP(Board_Image, "f:\output.bmp")
'Release the matrix and update the image handle
GRVBMain.VideoBOX.ReleaseMatrixToImageHandle Board_Image
'Display the processing result in Result_Image window
GRVBMain.Result_Image = GRVBMain.VideoBOX.ToPicture(Board_Image)
'GRVBMain.Enabled = False
End Sub
'初始化摄像头
'Init the camera
Public Sub InitCam()
If Cam_Has_Init Then Exit Sub
If Not GRVBMain.VideoBOX.Init _
Then
GRSpeak "找不到摄像头!"
'MsgBox "Error About Camera!", , "Camera's Error"
'MsgBox GRVBMain.VideoBOX.GetLastErrorString
Else
Board_Image = GRVBMain.VideoBOX.GetColorImageHandle
GRVBMain.VideoBOX.SetPreview (True)
GRVBMain.VideoBOX.Start
Cam_Has_Init = True
CamWidth = GRVBMain.VideoBOX.GetWidth
End If
'Camera is opened
CamClosed = True
End Sub
'- - - - - - - - - - - - - - - - - - - - - - - - - -
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -