[FFmpeg-devel] [PATCH 2/4] Implement ocv_dilate libopencv filter wrapper.

Stefano Sabatini stefano.sabatini-lala
Sat Sep 11 15:59:18 CEST 2010


---
 configure                  |    1 +
 libavfilter/Makefile       |    1 +
 libavfilter/allfilters.c   |    1 +
 libavfilter/vf_libopencv.c |  158 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 161 insertions(+), 0 deletions(-)

diff --git a/configure b/configure
index 26db167..9453fc9 100755
--- a/configure
+++ b/configure
@@ -1394,6 +1394,7 @@ udp_protocol_deps="network"
 
 # filters
 ocv_smooth_filter_deps="libopencv"
+ocv_dilate_filter_deps="libopencv"
 
 # libraries
 avdevice_deps="avcodec avformat"
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index a15c3f1..394b674 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -23,6 +23,7 @@ OBJS-$(CONFIG_FORMAT_FILTER)                 += vf_format.o
 OBJS-$(CONFIG_HFLIP_FILTER)                  += vf_hflip.o
 OBJS-$(CONFIG_NOFORMAT_FILTER)               += vf_format.o
 OBJS-$(CONFIG_NULL_FILTER)                   += vf_null.o
+OBJS-$(CONFIG_OCV_DILATE_FILTER)             += vf_libopencv.o
 OBJS-$(CONFIG_OCV_SMOOTH_FILTER)             += vf_libopencv.o
 OBJS-$(CONFIG_PAD_FILTER)                    += vf_pad.o
 OBJS-$(CONFIG_PIXDESCTEST_FILTER)            += vf_pixdesctest.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index fb84968..da1fedb 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -43,6 +43,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER (HFLIP,       hflip,       vf);
     REGISTER_FILTER (NOFORMAT,    noformat,    vf);
     REGISTER_FILTER (NULL,        null,        vf);
+    REGISTER_FILTER (OCV_DILATE,  ocv_dilate,  vf);
     REGISTER_FILTER (OCV_SMOOTH,  ocv_smooth,  vf);
     REGISTER_FILTER (PAD,         pad,         vf);
     REGISTER_FILTER (PIXDESCTEST, pixdesctest, vf);
diff --git a/libavfilter/vf_libopencv.c b/libavfilter/vf_libopencv.c
index 63d5b61..6c535f0 100644
--- a/libavfilter/vf_libopencv.c
+++ b/libavfilter/vf_libopencv.c
@@ -63,6 +63,164 @@ static int query_formats(AVFilterContext *ctx)
 
 static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
 
+static int read_shape_from_file(int *cols, int *rows, int **values, const char *filename, void *log_ctx)
+{
+    char *p, *buf;
+    size_t size;
+    int i, j, w;
+    FILE *f = fopen(filename, "rb");
+    char *line;
+
+    *cols = *rows = 0;
+
+    if (!f) {
+        av_log(log_ctx, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename, strerror(errno));
+        return AVERROR(errno);
+    }
+    fseek(f, 0, SEEK_END);
+    size = ftell(f);
+    fseek(f, 0, SEEK_SET);
+    buf = av_malloc(size + 1);
+    if (!buf) {
+        fclose(f);
+        return AVERROR(ENOMEM);
+    }
+    fread(buf, 1, size, f);
+    buf[size++] = 0;
+    fclose(f);
+
+    /* prescan file to get the number of lines and the maximum width */
+    w = 0;
+    for (i = 0; i < size; i++) {
+        if (buf[i] == '\n') {
+            (*rows)++;
+            *cols = FFMAX(*cols, w); w = 0;
+        } else
+            w++;
+    }
+    av_log(log_ctx, AV_LOG_DEBUG, "rows:%d cols:%d\n", *rows, *cols);
+
+    if (!(*values = av_mallocz(sizeof(int) * *rows * *cols)))
+        return AVERROR(ENOMEM);
+
+    /* fill *values */
+    p = buf;
+    for (i = 0; i < *rows; i++) {
+        for (j = 0;; j++) {
+            /* av_log(log_ctx, AV_LOG_DEBUG, "%d:%d -> '%c'\n", i, j, *p == '\n' ? 'N' : *p); */
+            if (*p == '\n') {
+                p++; break;
+            } else
+                (*values)[*cols*i + j] = !!isgraph(*(p++));
+        }
+    }
+
+    if (!(line = av_malloc(*cols + 1)))
+        return AVERROR(ENOMEM);
+    for (i = 0; i < *rows; i++) {
+        for (j = 0; j < *cols; j++)
+            line[j] = (*values)[i * *cols + j] ? '@' : ' ';
+        line[j] = 0;
+        av_log(log_ctx, AV_LOG_DEBUG, "%3d: %s\n", i, line);
+    }
+    av_free(line);
+
+    return 0;
+}
+
+static int parse_structuring_elem(IplConvKernel **kernel, char *buf, void *log_ctx)
+{
+    char values_file_str[128], shape_str[128];
+    int cols, rows, anchor_x, anchor_y, shape = CV_SHAPE_RECT;
+    int *values, ret;
+
+    sscanf(buf, "%dx%d+%dx%d/%127[^=]=%127s", &cols, &rows, &anchor_x, &anchor_y, shape_str, values_file_str);
+    if      (!strcmp(shape_str, "rect"   )) shape = CV_SHAPE_RECT;
+    else if (!strcmp(shape_str, "cross"  )) shape = CV_SHAPE_CROSS;
+    else if (!strcmp(shape_str, "ellipse")) shape = CV_SHAPE_ELLIPSE;
+    else if (!strcmp(shape_str, "custom" )) {
+        shape = CV_SHAPE_CUSTOM;
+        if ((ret = read_shape_from_file(&cols, &rows, &values, values_file_str, log_ctx)) < 0)
+            return ret;
+    } else {
+        av_log(log_ctx, AV_LOG_ERROR, "Shape type '%s' unknown\n.", shape_str);
+        return AVERROR(EINVAL);
+    }
+
+    *kernel = cvCreateStructuringElementEx(cols, rows, anchor_x, anchor_y, shape, values);
+    if (!*kernel)
+        return AVERROR(ENOMEM);
+
+    av_log(log_ctx, AV_LOG_INFO, "Structuring element: w:%d h:%d x:%d y:%d shape:%s\n",
+           rows, cols, anchor_x, anchor_y, shape_str);
+    return 0;
+}
+
+#if CONFIG_OCV_DILATE_FILTER
+
+typedef struct {
+    int iterations_nb;
+    IplConvKernel *kernel;
+} DilateContext;
+
+static av_cold int dilate_init(AVFilterContext *ctx, const char *args, void *opaque)
+{
+    DilateContext *dilate = ctx->priv;
+    char kernel_str[256] = "3x3+0x0/rect";
+    dilate->iterations_nb = 1;
+
+    if (args)
+        sscanf(args, "%d:%255c", &dilate->iterations_nb, kernel_str);
+
+    if (parse_structuring_elem(&dilate->kernel, kernel_str, ctx) < 0)
+        return AVERROR(EINVAL);
+
+    av_log(ctx, AV_LOG_INFO, "iterations_nb:%d\n", dilate->iterations_nb);
+    return 0;
+}
+
+static void dilate_end_frame(AVFilterLink *inlink)
+{
+    DilateContext *dilate = inlink->dst->priv;
+    AVFilterLink *outlink= inlink->dst->outputs[0];
+    AVFilterBufferRef *inpicref  = inlink ->cur_buf;
+    AVFilterBufferRef *outpicref = outlink->out_buf;
+    IplImage inimg, outimg;
+
+    fill_iplimage_from_picref(&inimg , inpicref , inlink->format);
+    fill_iplimage_from_picref(&outimg, outpicref, inlink->format);
+    cvDilate(&inimg, &outimg, NULL, dilate->iterations_nb);
+    fill_picref_from_iplimage(outpicref, &outimg, inlink->format);
+
+    avfilter_unref_buffer(inpicref);
+    avfilter_draw_slice(outlink, 0, outlink->h, 1);
+    avfilter_end_frame(outlink);
+    avfilter_unref_buffer(outpicref);
+}
+
+AVFilter avfilter_vf_ocv_dilate = {
+    .name        = "ocv_dilate",
+    .description = "Apply dilate transform using libopencv.",
+
+    .priv_size = sizeof(DilateContext),
+
+    .query_formats = query_formats,
+    .init = dilate_init,
+
+    .inputs    = (AVFilterPad[]) {{ .name             = "default",
+                                    .type             = AVMEDIA_TYPE_VIDEO,
+                                    .draw_slice       = null_draw_slice,
+                                    .end_frame        = dilate_end_frame,
+                                    .min_perms        = AV_PERM_READ },
+                                  { .name = NULL}},
+
+    .outputs   = (AVFilterPad[]) {{ .name             = "default",
+                                    .type             = AVMEDIA_TYPE_VIDEO, },
+                                  { .name = NULL}},
+};
+
+#endif /* CONFIG_OCV_DILATE_FILTER */
+
 #if CONFIG_OCV_SMOOTH_FILTER
 
 typedef struct {
-- 
1.7.1




More information about the ffmpeg-devel mailing list