This repository has been archived by the owner on Sep 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
separate out non-max suppression overlap code
- Loading branch information
Showing
2 changed files
with
85 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,73 @@ | |||
/* non-maximum suppression - combine overlapping rects into the strongest one */ | |||
|
|||
exports.combineOverlaps = combineOverlaps; | |||
|
|||
/* rects is an array of objects that look like: | |||
* { | |||
* x: // top left x coordinate, | |||
* y: // lop left y coordinate, | |||
* width: // width of rect, | |||
* height: // height of rect, | |||
* prob: // the higher, the more likely it will suppress other rects | |||
* } | |||
* | |||
* minRatio is the min ratio of intersection area to union area of two rects | |||
* to qualify them as overlapping. | |||
* | |||
* minOverlaps is the number of suppressions required for a rect | |||
* to be included in the final returned set. | |||
*/ | |||
function combineOverlaps(rects, minRatio, minOverlaps) { | |||
console.log(minRatio, minOverlaps); | |||
minRatio = minRatio || 0.5; | |||
minOverlaps = minOverlaps || 1; | |||
|
|||
for (var i = 0; i < rects.length; i++) { | |||
var r1 = rects[i]; | |||
r1.tally = 0; // number of rects it's suppressed | |||
|
|||
for (var j = 0; j < i; j++) { | |||
var r2 = rects[j]; | |||
|
|||
if (doesOverlap(r1, r2, minRatio)) { | |||
if (r1.prob > r2.prob) { | |||
r2.suppressed = true; | |||
r1.tally += 1 + r2.tally; | |||
} | |||
else { | |||
r1.suppressed = true; | |||
r2.tally += 1 + r1.tally; | |||
} | |||
} | |||
} | |||
} | |||
// only take a rect if it wasn't suppressed by any other rect | |||
return rects.filter(function(rect) { | |||
return !rect.suppressed && rect.tally >= minOverlaps; | |||
}) | |||
} | |||
|
|||
function doesOverlap(r1, r2, minRatio) { | |||
var overlapW, overlapH; | |||
if (r1.x > r2.x) { | |||
overlapW = Math.min((r2.x + r2.width) - r1.x, r1.width); | |||
} | |||
else { | |||
overlapW = Math.min((r1.x + r1.width) - r2.x, r2.width); | |||
} | |||
|
|||
if (r1.y > r2.y) { | |||
overlapH = Math.min((r2.y + r2.height) - r1.y, r1.height); | |||
} | |||
else { | |||
overlapH = Math.min((r1.y + r1.height) - r2.y, r2.height); | |||
} | |||
|
|||
if (overlapW <= 0 || overlapH <= 0) { | |||
return false; | |||
} | |||
var intersect = overlapW * overlapH; | |||
var union = (r1.width * r1.height) + (r2.width * r2.height) - (intersect * 2); | |||
|
|||
return intersect / union > minRatio; | |||
} |