www.pudn.com > facedetectDLL_expressions.zip > ClassImage.cls
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "ClassImage"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
'Sentience project - image object
'Copyright (C) 2003 Bob Mottram
'This program is free software; you can redistribute it and/or modify
'it under the terms of the GNU General Public License as published by
'the Free Software Foundation; either version 2 of the License, or
'(at your option) any later version.
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
'GNU General Public License for more details.
'You should have received a copy of the GNU General Public License
'along with this program; if not, write to the Free Software
'Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Public Width As Integer
Public Height As Integer
Dim image() As Byte
Dim Integral() As Long
Public Texture As Integer
Dim calibrationActive As Boolean
Dim CalibrationMap() As Integer
Dim RGBsource As RGBthingy
Dim RGBdest As RGBpoint
Public Sub Negative()
Dim x As Integer
Dim y As Integer
Dim c As Integer
For x = 0 To Width - 1
For y = 0 To Height - 1
For c = 0 To 2
image(x, y, c) = 255 - image(x, y, c)
Next
Next
Next
End Sub
Public Sub Rotate()
Dim x As Integer
Dim y As Integer
Dim c As Integer
Dim temp As Byte
For x = 0 To Width - 1
For y = 0 To Height - 1
For c = 0 To 2
temp = image(y, x, c)
image(y, x, c) = image(x, y, c)
image(x, y, c) = temp
Next
Next
Next
End Sub
Public Sub loadCalibration(filename As String)
'loads the camera calibration map
Dim chess_x As Integer
Dim chess_y As Integer
Dim x As Integer
Dim y As Integer
Dim w As Integer
Dim h As Integer
Dim xx As Integer
Dim yy As Integer
Dim rows As Integer
Dim columns As Integer
Dim top_x As Integer
Dim top_y As Integer
Dim bottom_x As Integer
Dim bottom_y As Integer
calibrationActive = True
Open filename For Input As #1
Input #1, top_x
Input #1, top_y
Input #1, bottom_x
Input #1, bottom_y
Input #1, rows
Input #1, columns
For chess_x = 0 To columns
For chess_y = 0 To rows
Input #1, x
Input #1, y
Next
Next
Input #1, w
Input #1, h
For x = 0 To w - 1
For y = 0 To h - 1
Input #1, xx
Input #1, yy
CalibrationMap(x, y, 0) = xx
CalibrationMap(x, y, 1) = yy
Next
Next
Close #1
End Sub
Public Sub init(ImageWidth As Integer, ImageHeight As Integer)
Width = ImageWidth
Height = ImageHeight
ReDim image(Width, Height, 3)
ReDim Integral(Width, Height, 2)
calibrationActive = False
ReDim CalibrationMap(Width, Height, 2)
End Sub
Public Sub Update(canvas As PictureBox)
'import a picture
Dim x As Integer
Dim y As Integer
Dim screenX As Integer
Dim screenY As Integer
Dim w As Integer
Dim h As Integer
Dim xx As Integer
Dim yy As Integer
Dim r As Double
Dim g As Double
Dim b As Double
Dim RGBval As Long
Dim pixels As Double
Dim maxCol As Long
Dim edgeValue As Single
Dim screenWidth As Single
Dim screenHeight As Single
Dim screenLeft As Single
Dim screenTop As Single
Dim dX As Integer
Dim dy As Integer
Dim px As Integer
Dim py As Integer
screenLeft = 0
screenTop = 0
screenWidth = canvas.ScaleWidth
screenHeight = canvas.ScaleHeight
w = CInt(screenWidth / Width)
If (w < 1) Then
w = 1
End If
h = CInt(screenHeight / Height)
If (h < 1) Then
h = 1
End If
pixels = w * h
maxCol = RGB(255, 255, 255)
For x = 0 To Width - 1
For y = 0 To Height - 1
If (calibrationActive) Then
dX = CalibrationMap(x, y, 0)
dy = CalibrationMap(x, y, 1)
Else
dX = 0
dy = 0
End If
screenX = screenLeft + ((x / Width) * screenWidth)
screenY = screenTop + ((y / Height) * screenHeight)
r = 0
g = 0
b = 0
For xx = screenX To screenX + w - 1
For yy = screenY To screenY + h - 1
px = xx + (dX * (screenWidth / Width))
py = yy + (dy * (screenHeight / Height))
'If (px < 0) Then px = 0
'If (py < 0) Then py = 0
'If (px > width - 1) Then px = width - 1
'If (py > height - 1) Then py = height - 1
RGBval = canvas.Point(px, py)
RGBsource.value = RGBval
Call CopyMemory(RGBdest, RGBsource, 3)
r = r + RGBdest.Red
g = g + RGBdest.Green
b = b + RGBdest.Blue
Next
Next
r = (r / pixels)
g = (g / pixels)
b = (b / pixels)
image(x, y, 0) = r
image(x, y, 1) = g
image(x, y, 2) = b
Next
Next
Call updateIntegralImage
End Sub
Public Sub Show(canvas As PictureBox)
Dim x As Integer
Dim y As Integer
Dim screenX(2) As Single
Dim screenY(2) As Single
Dim r As Byte
Dim g As Byte
Dim b As Byte
Dim c As Long
Dim i As Integer
canvas.FillStyle = 0
For x = 0 To Width - 1
For y = 0 To Height - 1
r = image(x, y, 0)
g = image(x, y, 1)
b = image(x, y, 2)
c = RGB(r, g, b)
canvas.FillColor = c
screenX(0) = (x / Width) * canvas.ScaleWidth
screenY(0) = (y / Height) * canvas.ScaleHeight
screenX(1) = ((x + 1) / Width) * canvas.ScaleWidth
screenY(1) = ((y + 1) / Height) * canvas.ScaleHeight
canvas.Line (screenX(0), screenY(0))-(screenX(1), screenY(1)), c, B
Next
Next
End Sub
Public Function setPoint(x As Integer, y As Integer, col As Integer, value As Byte)
image(x, y, col) = value
End Function
Public Function getPoint(x As Integer, y As Integer, col As Integer) As Byte
getPoint = image(x, y, col)
End Function
Private Sub updateIntegralImage()
Dim x As Integer
Dim y As Integer
Dim p As Long
Dim p2 As Long
Dim textureVal As Long
Dim v1 As Integer
Dim v2 As Integer
Dim tVal As Integer
textureVal = 0
For y = 0 To Height - 1
p = 0
p2 = 0
For x = 0 To Width - 1
v1 = image(x, y, 0)
If (x > 0) Then v2 = image(x - 1, y, 0) Else v2 = 0
tVal = Abs(v1 - v2)
If (y > 0) Then v2 = image(x, y - 1, 0) Else v2 = 0
tVal = tVal + Abs(v1 - v2)
textureVal = textureVal + tVal
p = p + image(x, y, 0)
p2 = p2 + tVal
If (y > 0) Then
Integral(x, y, 0) = p + Integral(x, y - 1, 0)
Integral(x, y, 1) = p2 + Integral(x, y - 1, 1)
Else
Integral(x, y, 0) = p
Integral(x, y, 1) = p2
End If
Next
Next
Texture = textureVal / (Width * Height)
End Sub
Private Function getIntegral(tx As Integer, ty As Integer, bx As Integer, by As Integer, index As Integer) As Long
getIntegral = Integral(bx, by, index) + Integral(tx, ty, index) - (Integral(tx, by, index) + Integral(bx, ty, index))
End Function
Public Function detectFeature(x As Integer, y As Integer, wdth As Integer, hght As Integer, featureType As Integer) As Long
Dim area(4) As Long
Dim v As Long
Select Case featureType
Case 0 'A
area(0) = getIntegral(x, y, x + wdth, y + hght + hght, 0)
area(1) = getIntegral(x + wdth, y, x + wdth + wdth, y + hght + hght, 0)
v = Abs(area(0) - area(1))
Case 1 'B
area(0) = getIntegral(x, y, x + wdth, y + hght, 0)
area(1) = getIntegral(x, y + hght, x + wdth, y + hght + hght, 0)
v = Abs(area(0) - area(1))
Case 2 'C
area(0) = getIntegral(x, y, x + wdth, y + hght, 0)
area(1) = getIntegral(x + wdth, y, x + wdth + wdth, y + hght, 0)
area(2) = getIntegral(x + wdth + wdth, y, x + wdth + wdth + wdth, y + hght, 0)
v = Abs((area(1) * 2) - (area(0) + area(2)))
Case 3 'D
area(0) = getIntegral(x, y, x + wdth, y + hght, 0)
area(1) = getIntegral(x + wdth, y, x + wdth + wdth, y + hght, 0)
area(2) = getIntegral(x, y + hght, x + wdth, y + hght + hght, 0)
area(3) = getIntegral(x + wdth, y + hght, x + wdth + wdth, y + hght + hght, 0)
v = Abs((area(1) + area(2)) - (area(0) + area(3)))
Case 4 'A
area(0) = getIntegral(x, y, x + wdth, y + hght + hght, 1)
area(1) = getIntegral(x + wdth, y, x + wdth + wdth, y + hght + hght, 1)
v = Abs(area(0) - area(1))
Case 5 'B
area(0) = getIntegral(x, y, x + wdth, y + hght, 1)
area(1) = getIntegral(x, y + hght, x + wdth, y + hght + hght, 1)
v = Abs(area(0) - area(1))
Case 6 'C
area(0) = getIntegral(x, y, x + wdth, y + hght, 1)
area(1) = getIntegral(x + wdth, y, x + wdth + wdth, y + hght, 1)
area(2) = getIntegral(x + wdth + wdth, y, x + wdth + wdth + wdth, y + hght, 1)
v = Abs((area(1) * 2) - (area(0) + area(2)))
Case 7 'D
area(0) = getIntegral(x, y, x + wdth, y + hght, 1)
area(1) = getIntegral(x + wdth, y, x + wdth + wdth, y + hght, 1)
area(2) = getIntegral(x, y + hght, x + wdth, y + hght + hght, 1)
area(3) = getIntegral(x + wdth, y + hght, x + wdth + wdth, y + hght + hght, 1)
v = Abs((area(1) + area(2)) - (area(0) + area(3)))
End Select
detectFeature = v
End Function
Public Sub sampleFromImage(img As ClassImage, tx As Integer, ty As Integer, bx As Integer, by As Integer)
Dim x As Integer
Dim y As Integer
Dim xx As Integer
Dim yy As Integer
Dim dX As Integer
Dim dy As Integer
Dim c As Integer
dX = bx - tx
dy = by - ty
For x = 0 To Width - 1
xx = tx + ((x * dX) / Width)
For y = 0 To Height - 1
yy = ty + ((y * dy) / Height)
For c = 0 To 2
image(x, y, c) = img.getPoint(xx, yy, c)
Next
Next
Next
Call updateIntegralImage
End Sub
Public Sub CG(tx As Integer, ty As Integer, bx As Integer, by As Integer, targ_r As Byte, targ_g As Byte, targ_b As Byte, ByRef cx As Integer, ByRef cy As Integer, maxval As Integer)
'returns the centre of gravity for the given colour
Dim x As Integer
Dim y As Integer
Dim tot As Long
Dim tot_x As Long
Dim tot_y As Long
Dim p(3) As Integer
Dim c As Integer
Dim dp As Integer
tot = 1
tot_x = 0
tot_y = 0
For x = tx To bx
For y = ty To by
For c = 0 To 2
p(c) = image(x, y, c)
Next
dp = Abs(p(0) - targ_r)
dp = dp + Abs(p(1) - targ_g)
dp = dp + Abs(p(2) - targ_b)
'If (dp < 200) Then
'dp = 765 - dp
dp = maxval - dp
If (dp < 0) Then dp = 0
' Else
' dp = 0
'End If
tot = tot + dp
tot_x = tot_x + (x * dp)
tot_y = tot_y + (y * dp)
Next
Next
cx = tot_x / tot
cy = tot_y / tot
End Sub
Public Function relativeThreshold(value As Integer) As Integer
Dim x As Integer
Dim y As Integer
Dim av As Long
Dim min As Integer
Dim max As Integer
Dim p As Integer
Dim hits As Integer
Dim pixels As Integer
pixels = Width * Height
hits = 0
av = 0
For x = 0 To Width - 1
For y = 0 To Height - 1
av = av + image(x, y, 0)
Next
Next
av = av / pixels
min = (av * (100 - value)) / 100
For x = 0 To Width - 1
For y = 0 To Height - 1
p = image(x, y, 0)
If (p < min) Then
hits = hits + 1
'image(x, y, 0) = 0
'image(x, y, 1) = 255
'image(x, y, 2) = 0
End If
Next
Next
relativeThreshold = (hits * 100) / pixels
End Function