From 6626fbec0ec638fee13c465f4497eebd280ce590 Mon Sep 17 00:00:00 2001 From: Douglas Bagnall Date: Sun, 13 Jun 2010 22:03:25 +1200 Subject: [PATCH] add dither (or proper rounding) to map --- edges.c | 24 ++++++++++++++++++++--- edges.h | 69 +++++++++++++++++++++++++++++++++-------------------------------- 2 files changed, 56 insertions(+), 37 deletions(-) diff --git a/edges.c b/edges.c index cccfb43..13a97af 100644 --- a/edges.c +++ b/edges.c @@ -88,10 +88,22 @@ coord_to_int_clamp(coord_t x, const int max_plus_one){ return 0; if (x >= max_plus_one - 1.5) return max_plus_one - 1; - return (int)(x); + return (int)(x + 0.5); } static inline int +coord_to_int_clamp_dither(sparrow_find_lines_t *fl, coord_t x, + const int max_plus_one, const int i){ + if (x < 0) + return 0; + x += fl->dither[i]; + if (x >= max_plus_one) + return max_plus_one - 1; + return (int)x; +} + + +static inline int coord_in_range(coord_t x, const int max_plus_one){ return x >= 0 && (x + 0.5 < max_plus_one); } @@ -150,8 +162,8 @@ corners_to_full_lut(GstSparrow *sparrow, sparrow_find_lines_t *fl){ coord_t iy = mesh_square->y + mmy * mesh_square->dyd; coord_t ix = mesh_square->x + mmy * mesh_square->dxd; for (mmx = 0; mmx < LINE_PERIOD; mmx++, i++){ - int ixx = coord_to_int_clamp(ix, sparrow->in.width); - int iyy = coord_to_int_clamp(iy, sparrow->in.height); + int ixx = coord_to_int_clamp_dither(fl, ix, sparrow->in.width, i); + int iyy = coord_to_int_clamp_dither(fl, iy, sparrow->in.height, i); if(sparrow->screenmask[iyy * sparrow->in.width + ixx]){ map_lut[i].x = ixx; map_lut[i].y = iyy; @@ -1095,6 +1107,7 @@ finalise_find_edges(GstSparrow *sparrow){ free(fl->map); free(fl->mesh_mem); free(fl->clusters); + free(fl->dither); cvReleaseImage(&fl->threshold); cvReleaseImage(&fl->working); cvReleaseImageHeader(&fl->input); @@ -1130,6 +1143,11 @@ init_find_edges(GstSparrow *sparrow){ gint v_lines = (sparrow->out.width + LINE_PERIOD - 1) / LINE_PERIOD; gint n_lines_max = (h_lines + v_lines); gint n_corners = (h_lines * v_lines); + + /*set up dither here, rather than in the busy time */ + fl->dither = malloc_aligned_or_die(sparrow->out.pixcount * sizeof(double)); + dsfmt_fill_array_close_open(sparrow->dsfmt, fl->dither, sparrow->out.pixcount); + fl->n_hlines = h_lines; fl->n_vlines = v_lines; diff --git a/edges.h b/edges.h index f5c0c4c..078108e 100644 --- a/edges.h +++ b/edges.h @@ -15,8 +15,6 @@ #define COLOUR_QUANT 1 #define COLOUR_MASK (0xff >> COLOUR_QUANT) -/*XXX should dither */ - typedef enum corner_status { CORNER_UNUSED, CORNER_PROJECTED, @@ -129,6 +127,7 @@ typedef struct sparrow_find_lines_s { sparrow_corner_t *mesh; sparrow_corner_t *mesh_next; sparrow_cluster_t *clusters; + double *dither; IplImage *debug; IplImage *threshold; IplImage *working; @@ -137,38 +136,40 @@ typedef struct sparrow_find_lines_s { } sparrow_find_lines_t; -#define DEBUG_FIND_LINES(fl)GST_DEBUG( \ - "fl:\n" \ - " sparrow_line_t *h_lines: %p\n" \ - " sparrow_line_t *v_lines: %p\n" \ - " sparrow_line_t **shuffled_lines: %p\n" \ - " int current: %d\n" \ - " int n_lines: %d\n" \ - " int n_vlines: %d\n" \ - " int n_hlines: %d\n" \ - " gint shift1: %d\n" \ - " gint shift2: %d\n" \ - " sparrow_intersect_t *map: %p\n" \ - " sparrow_corner_t *mesh: %p\n" \ - " sparrow_cluster_t *clusters: %p\n" \ - " IplImage *debug: %p\n" \ - " IplImage *threshold: %p\n" \ - " IplImage *working: %p\n" \ - " IplImage *input: %p\n" \ - " edges_state_t state: %d\n" \ - , \ - (fl)->h_lines, \ - (fl)->v_lines, \ - (fl)->shuffled_lines, \ - (fl)->current, \ - (fl)->n_lines, \ - (fl)->n_vlines, \ - (fl)->n_hlines, \ - (fl)->shift1, \ - (fl)->shift2, \ - (fl)->map, \ - (fl)->mesh, \ - (fl)->clusters, \ +#define DEBUG_FIND_LINES(fl)GST_DEBUG( \ + "fl:\n" \ + " sparrow_line_t *h_lines: %p\n" \ + " sparrow_line_t *v_lines: %p\n" \ + " sparrow_line_t **shuffled_lines: %p\n" \ + " int current: %d\n" \ + " int n_lines: %d\n" \ + " int n_vlines: %d\n" \ + " int n_hlines: %d\n" \ + " gint shift1: %d\n" \ + " gint shift2: %d\n" \ + " sparrow_intersect_t *map: %p\n" \ + " sparrow_corner_t *mesh: %p\n" \ + " sparrow_cluster_t *clusters: %p\n" \ + " double *dither: %p \n" \ + " IplImage *debug: %p\n" \ + " IplImage *threshold: %p\n" \ + " IplImage *working: %p\n" \ + " IplImage *input: %p\n" \ + " edges_state_t state: %d\n" \ + , \ + (fl)->h_lines, \ + (fl)->v_lines, \ + (fl)->shuffled_lines, \ + (fl)->current, \ + (fl)->n_lines, \ + (fl)->n_vlines, \ + (fl)->n_hlines, \ + (fl)->shift1, \ + (fl)->shift2, \ + (fl)->map, \ + (fl)->mesh, \ + (fl)->clusters, \ + (fl)->dither, \ (fl)->debug, \ (fl)->threshold, \ (fl)->working, \ -- 2.11.4.GIT