I’m pretty sure I posted this video months ago when you first started writing your bounding box routines. It was part of my explanation that finding object boundaries against a plain back ground was easy, the problems arise when trying to find boundaries amongst complex backgrounds; tree leaves I believe at the time. I re-posted it to show the HSL pallet usage.
RGB to HSL conversion is very easy.
The colour model has no bearing on the speed of the algorithm that detects the boundaries. The HSL colour model just allows you to select colour groups (ie skin tones) easier. These algorithms are not running especially ‘fast’, I wrote them in VB6 basic for ease.
The basic idea I used was to scan each pixel and note its colour; if the colour was different from the background then I created a bounding box in an array. If the next pixel was within a set bias (colour distance) of the first pixel then I expanded the box to include it, else I created a new box with its own colour. As each pixel is scanned it is checked against the list of bounding boxes within its proximity; if the colour range is similar it’s added and the box expanded.
I’ll post the complete (un-optimized) code… it’s very similar to VB.NET so you should follow it easily. This does not use HSL just difference from back ground as in the example video.
Any antiquated coding techniques are because I understand how the complier optimizes code; its basically the most efficient representation before the complier parses the code.
'// Auto define bounding boxes
'// check each pixels x,y to see if its within the bounds of a box
'// if it is then expand the box to include the pixel
'// else start a new box
Dim BOX(10000, 10) As Long
Dim tnb As Long
bias = 2
'// sum colour instances
For Y = 0 To Picture2.ScaleHeight - 1
For X = 0 To Picture2.ScaleWidth - 1
'// get the colour
RR = ImageData(2, X, Y)
GG = ImageData(1, X, Y)
BB = ImageData(0, X, Y)
'// check for colour other than BLACK
cfnd = 0
If RR > 50 Then cfnd = 1
If GG > 50 Then cfnd = 1
If BB > 50 Then cfnd = 1
bfnd = 0
If cfnd Then
'// check if inside existing bounding box
For dd = 1 To tnb
bl = 0
If X > BOX(dd, 1) - bias Then bl = bl + 1
If X < BOX(dd, 3) + bias Then bl = bl + 1
If Y > BOX(dd, 2) - bias Then bl = bl + 1
If Y < BOX(dd, 4) + bias Then bl = bl + 1
If bl = 4 Then bfnd = dd: Exit For
Next
If bfnd Then
'// Adjust existing box
If X < BOX(bfnd, 1) Then BOX(bfnd, 1) = X
If X > BOX(bfnd, 3) Then BOX(bfnd, 3) = X
If Y < BOX(bfnd, 2) Then BOX(bfnd, 2) = Y
If Y > BOX(bfnd, 4) Then BOX(bfnd, 4) = Y
Else
'// Create new box
tnb = tnb + 1
BOX(tnb, 1) = X
BOX(tnb, 2) = Y
BOX(tnb, 3) = X
BOX(tnb, 4) = Y
End If
End If
Next
Next
'// remove small boxes inside larger boxes
For e = 1 To tnb
bx = BOX(e, 1) + ((BOX(e, 3) - BOX(e, 1)) / 2)
By = BOX(e, 2) + ((BOX(e, 4) - BOX(e, 2)) / 2)
For dd = 1 To tnb
bbw = 0
fnd = 0
If BOX(e, 1) >= BOX(dd, 1) Then
If BOX(e, 1) <= BOX(dd, 3) Then
If BOX(e, 2) >= BOX(dd, 2) Then
If BOX(e, 2) <= BOX(dd, 4) Then fnd = 1
End If
End If
End If
If BOX(e, 3) >= BOX(dd, 1) Then
If BOX(e, 3) <= BOX(dd, 3) Then
If BOX(e, 2) >= BOX(dd, 2) Then
If BOX(e, 2) <= BOX(dd, 4) Then fnd = 1
End If
End If
End If
If BOX(e, 1) >= BOX(dd, 1) Then
If BOX(e, 1) <= BOX(dd, 3) Then
If BOX(e, 4) >= BOX(dd, 2) Then
If BOX(e, 4) <= BOX(dd, 4) Then fnd = 1
End If
End If
End If
If BOX(e, 3) >= BOX(dd, 1) Then
If BOX(e, 3) <= BOX(dd, 3) Then
If BOX(e, 4) >= BOX(dd, 2) Then
If BOX(e, 4) <= BOX(dd, 4) Then fnd = 1
End If
End If
End If
'
If fnd = 1 Then
If BOX(e, 1) < BOX(dd, 1) Then BOX(dd, 1) = BOX(e, 1)
If BOX(e, 3) > BOX(dd, 3) Then BOX(dd, 3) = BOX(e, 3)
If BOX(e, 2) < BOX(dd, 2) Then BOX(dd, 2) = BOX(e, 2)
If BOX(e, 4) > BOX(dd, 4) Then BOX(dd, 4) = BOX(e, 4)
BOX(dd, 5) = 1: Exit For
End If
Next
Next
jump3:
'// plot boxes
Picture2.AutoRedraw = False
For dd = 1 To tnb
If BOX(dd, 5) Then
x1 = BOX(dd, 1)
x2 = BOX(dd, 3)
y1 = BOX(dd, 2)
y2 = BOX(dd, 4)
Picture2.Line (x1, y1)-(x2, y2), &HFF0000, B
End If
Next
Picture2.AutoRedraw = True