Skip to content

Commit

Permalink
Add compare function in ObjectDetector
Browse files Browse the repository at this point in the history
  • Loading branch information
WedgeSama committed Apr 13, 2013
1 parent 4bc7e08 commit 6c226d9
Showing 1 changed file with 87 additions and 5 deletions.
92 changes: 87 additions & 5 deletions class/ObjectDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,28 @@ class ObjectDetector {
* @var number
*/
const onde_base = 2;

/**
* Ondelette increment
*
* @var number
*/
const onde_inc = 1.25;

/**
* Pixel increment
*
* @var number
*/
const increment = 0.1;

/**
* Min number of neighbors
*
* @var int
*/
const min_neighbors = 3;

/**
* Haar loader
*
Expand Down Expand Up @@ -73,8 +80,10 @@ public function detectObject(Image $img) {
}
// add object detected
if ($detect_ok)
$res[] = array('x' => $x, 'y' => $y, 'w' => $size_w,
'h' => $size_h);
$res[] = array(
'x' => $x, 'y' => $y, 'w' => $size_w,
'h' => $size_h
);
}
}
}
Expand All @@ -89,6 +98,79 @@ public function detectObject(Image $img) {
* @return array
*/
private function compare($objs) {
return $objs;
$res = array();
$temp = array();
$nb_classes = 0;

for ($i = 0; $i < count($objs); $i++) {
$found = false;
for ($j = 0; $j < $i; $j++) {
if ($this->equal($objs[$j], $objs[$i])) {
$found = true;
$temp[$i] = $temp[$j];
}
}

if (!$found) {
$temp[$i] = $nb_classes;
$nb_classes++;
}
}

$neighbors = array();
$rect = array();
for ($i = 0; $i < $nb_classes; $i++) {
$neighbors[$i] = 0;
$rect[$i] = array(
'x' => 0, 'y' => 0, 'w' => 0, 'h' => 0
);
}

for ($i = 0; $i < count($objs); $i++) {
$neighbors[$temp[$i]]++;
$rect[$temp[$i]]['x'] += $objs[$i]['x'];
$rect[$temp[$i]]['y'] += $objs[$i]['y'];
$rect[$temp[$i]]['w'] += $objs[$i]['w'];
$rect[$temp[$i]]['h'] += $objs[$i]['h'];
}

for ($i = 0; $i < $nb_classes; $i++) {
$n = $neighbors[$i];
if ($n >= self::min_neighbors) {
$r = array(
'x' => 0, 'y' => 0, 'w' => 0, 'h' => 0
);
$r['x'] = ($rect[$i]['x'] * 2 + $n) / (2 * $n);
$r['y'] = ($rect[$i]['y'] * 2 + $n) / (2 * $n);
$r['w'] = ($rect[$i]['w'] * 2 + $n) / (2 * $n);
$r['h'] = ($rect[$i]['h'] * 2 + $n) / (2 * $n);

$res[] = $r;
}
}
return $res;
}

/**
* Check if rect are equivalent
*
* @param array $r1
* @param array $r2
* @return boolean
*/
private function equal($r1, $r2) {
$dist = (int) ($r1['w'] * 0.2);

if (($r2['x'] <= $r1['x'] + $dist && $r2['x'] >= $r1['x'] - $dist
&& $r2['y'] <= $r1['y'] + $dist && $r2['y'] >= $r1['y'] - $dist
&& $r2['w'] <= (int) ($r1['w'] * 1.2)
&& (int) ($r2['w'] * 1.2) >= $r1['w'])
|| ($r1['x'] >= $r2['x']
&& $r1['x'] + $r1['w'] <= $r2['x'] + $r2['w']
&& $r1['y'] >= $r2['y']
&& $r1['y'] + $r1['h'] <= $r2['y'] + $r2['h']))
return true;

return false;
}
}

0 comments on commit 6c226d9

Please sign in to comment.