Skip to content

Commit 3385174

Browse files
committed
Pass on min_cluster_pixels for early gradient cluster rejection
Replace the hardcoded threshold of 25 pixels in do_gradient_clusters with the configurable min_cluster_pixels parameter. This rejects clusters earlier, reducing work later. Is 1.18x faster in a bench with a 3088x2064 image with min_cluster_pixels=100
1 parent 3057666 commit 3385174

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

apriltag_quad_thresh.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ struct cluster_task
102102
int w;
103103
int s;
104104
int nclustermap;
105+
int min_cluster_pixels;
105106
unionfind_t* uf;
106107
image_u8_t* im;
107108
zarray_t* clusters;
@@ -1557,7 +1558,7 @@ unionfind_t* connected_components(apriltag_detector_t *td, image_u8_t* threshim,
15571558
return uf;
15581559
}
15591560

1560-
zarray_t* do_gradient_clusters(image_u8_t* threshim, int ts, int y0, int y1, int w, int nclustermap, unionfind_t* uf, zarray_t* clusters) {
1561+
zarray_t* do_gradient_clusters(image_u8_t* threshim, int ts, int y0, int y1, int w, int nclustermap, int min_cluster_pixels, unionfind_t* uf, zarray_t* clusters) {
15611562
struct uint64_zarray_entry **clustermap = calloc(nclustermap, sizeof(struct uint64_zarray_entry*));
15621563

15631564
int mem_chunk_size = 2048;
@@ -1578,7 +1579,7 @@ zarray_t* do_gradient_clusters(image_u8_t* threshim, int ts, int y0, int y1, int
15781579

15791580
// XXX don't query this until we know we need it?
15801581
uint64_t rep0 = unionfind_get_representative(uf, y*w + x);
1581-
if (unionfind_get_set_size(uf, rep0) < 25) {
1582+
if ((int)unionfind_get_set_size(uf, rep0) < min_cluster_pixels) {
15821583
connected_last = false;
15831584
continue;
15841585
}
@@ -1610,7 +1611,7 @@ zarray_t* do_gradient_clusters(image_u8_t* threshim, int ts, int y0, int y1, int
16101611
\
16111612
if (v0 + v1 == 255) { \
16121613
uint64_t rep1 = unionfind_get_representative(uf, (y + dy)*w + x + dx); \
1613-
if (unionfind_get_set_size(uf, rep1) > 24) { \
1614+
if ((int)unionfind_get_set_size(uf, rep1) >= min_cluster_pixels) { \
16141615
uint64_t clusterid; \
16151616
if (rep0 < rep1) \
16161617
clusterid = (rep1 << 32) + rep0; \
@@ -1705,7 +1706,7 @@ static void do_cluster_task(void *p)
17051706
{
17061707
struct cluster_task *task = (struct cluster_task*) p;
17071708

1708-
do_gradient_clusters(task->im, task->s, task->y0, task->y1, task->w, task->nclustermap, task->uf, task->clusters);
1709+
do_gradient_clusters(task->im, task->s, task->y0, task->y1, task->w, task->nclustermap, task->min_cluster_pixels, task->uf, task->clusters);
17091710
}
17101711

17111712
zarray_t* merge_clusters(zarray_t* c1, zarray_t* c2) {
@@ -1768,6 +1769,7 @@ zarray_t* gradient_clusters(apriltag_detector_t *td, image_u8_t* threshim, int w
17681769
tasks[ntasks].uf = uf;
17691770
tasks[ntasks].im = threshim;
17701771
tasks[ntasks].nclustermap = nclustermap/(sz / chunksize + 1);
1772+
tasks[ntasks].min_cluster_pixels = td->qtp.min_cluster_pixels;
17711773
tasks[ntasks].clusters = zarray_create(sizeof(struct cluster_hash*));
17721774

17731775
workerpool_add_task(td->wp, do_cluster_task, &tasks[ntasks]);

0 commit comments

Comments
 (0)