[PATCH 2/3] Add dilate libopencv filter.
Stefano Sabatini
stefano.sabatini-lala
Sat Oct 2 17:03:38 CEST 2010
---
libavfilter/vf_libopencv.c | 131 ++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 131 insertions(+), 0 deletions(-)
diff --git a/libavfilter/vf_libopencv.c b/libavfilter/vf_libopencv.c
index 0e3da4d..4f787ad 100644
--- a/libavfilter/vf_libopencv.c
+++ b/libavfilter/vf_libopencv.c
@@ -127,6 +127,136 @@ static void smooth_end_frame_filter(AVFilterContext *ctx, IplImage *inimg, IplIm
cvSmooth(inimg, outimg, smooth->type, smooth->param1, smooth->param2, smooth->param3, smooth->param4);
}
+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 (*rows > INT_MAX / *cols) {
+ av_log(log_ctx, AV_LOG_ERROR, "File with size %dx%d is too big\n",
+ *rows, *cols);
+ return AVERROR_INVALIDDATA;
+ }
+ 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_iplconvkernel(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 = NULL, 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);
+ av_freep(&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;
+}
+
+typedef struct {
+ int iterations_nb;
+ IplConvKernel *kernel;
+} DilateContext;
+
+static av_cold int dilate_init(AVFilterContext *ctx, const char *args, void *opaque)
+{
+ OCVContext *ocv = ctx->priv;
+ DilateContext *dilate = ocv->priv;
+ char kernel_str[256] = "3x3+0x0/rect";
+ int ret;
+
+ dilate->iterations_nb = 1;
+
+ if (args)
+ sscanf(args, "%d:%255c", &dilate->iterations_nb, kernel_str);
+
+ if ((ret = parse_iplconvkernel(&dilate->kernel, kernel_str, ctx)) < 0)
+ return ret;
+
+ av_log(ctx, AV_LOG_INFO, "iterations_nb:%d\n", dilate->iterations_nb);
+ return 0;
+}
+
+static void dilate_end_frame_filter(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg)
+{
+ OCVContext *ocv = ctx->priv;
+ DilateContext *dilate = ocv->priv;
+ cvDilate(inimg, outimg, dilate->kernel, dilate->iterations_nb);
+}
+
typedef struct {
const char *name;
size_t priv_size;
@@ -137,6 +267,7 @@ typedef struct {
static OCVFilterEntry ocv_filter_entries[] = {
{ "smooth", sizeof(SmoothContext), smooth_init, NULL, smooth_end_frame_filter },
+ { "dilate", sizeof(DilateContext), dilate_init, NULL, dilate_end_frame_filter },
};
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
--
1.7.1
--J/dobhs11T7y2rNN--
More information about the ffmpeg-devel
mailing list