1 /*
2 * Copyright (c) 2010 Stefano Sabatini
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * libopencv wrapper functions
24 */
25
26 #include "config.h"
27 #if HAVE_OPENCV2_CORE_CORE_C_H
28 #include <opencv2/core/core_c.h>
29 #include <opencv2/imgproc/imgproc_c.h>
30 #else
31 #include <opencv/cv.h>
32 #include <opencv/cxcore.h>
33 #endif
43
45 {
46 IplImage *tmpimg;
47 int depth, channels_nb;
48
52 else return;
53
54 tmpimg = cvCreateImageHeader((CvSize){
frame->width,
frame->height}, depth, channels_nb);
56 img->imageData =
img->imageDataOrigin =
frame->data[0];
57 img->dataOrder = IPL_DATA_ORDER_PIXEL;
58 img->origin = IPL_ORIGIN_TL;
60 }
61
63 {
66 }
67
77
83
85 {
88 char type_str[128] = "gaussian";
89
94
95 if (args)
96 sscanf(args,
"%127[^|]|%d|%d|%lf|%lf", type_str, &
smooth->param1, &
smooth->param2, &
smooth->param3, &
smooth->param4);
97
98 if (!strcmp(type_str,
"blur" ))
smooth->type = CV_BLUR;
99 else if (!strcmp(type_str,
"blur_no_scale"))
smooth->type = CV_BLUR_NO_SCALE;
100 else if (!strcmp(type_str,
"median" ))
smooth->type = CV_MEDIAN;
101 else if (!strcmp(type_str,
"gaussian" ))
smooth->type = CV_GAUSSIAN;
102 else if (!strcmp(type_str,
"bilateral" ))
smooth->type = CV_BILATERAL;
103 else {
106 }
107
110 "Invalid value '%d' for param1, it has to be a positive odd number\n",
113 }
114 if ((
smooth->type == CV_BLUR ||
smooth->type == CV_BLUR_NO_SCALE ||
smooth->type == CV_GAUSSIAN) &&
117 "Invalid value '%d' for param2, it has to be zero or a positive odd number\n",
120 }
121
124 return 0;
125 }
126
128 {
132 }
133
135 void *log_ctx)
136 {
137 uint8_t *buf, *p, *pend;
140
143
144 /* prescan file to get the number of lines and the maximum width */
147 if (buf[
i] ==
'\n') {
148 if (*rows == INT_MAX) {
151 goto end;
152 }
153 ++(*rows);
156 }
else if (
w == INT_MAX) {
159 }
161 }
162 if (*rows > (SIZE_MAX / sizeof(int) / *cols)) {
164 *rows, *cols);
166 goto end;
167 }
170 goto end;
171 }
172
173 /* fill *values */
174 p = buf;
176 for (
i = 0;
i < *rows;
i++) {
177 for (j = 0;; j++) {
178 if (p > pend || *p == '\n') {
179 p++;
180 break;
181 } else
183 }
184 }
185
186 end:
188
189 #ifdef DEBUG
190 {
194 for (
i = 0;
i < *rows;
i++) {
195 for (j = 0; j < *cols; j++)
199 }
201 }
202 #endif
203
204 return 0;
205 }
206
208 {
209 char shape_filename[128] = "", shape_str[32] = "rect";
210 int cols = 0, rows = 0, anchor_x = 0, anchor_y = 0, shape = CV_SHAPE_RECT;
212
213 sscanf(buf, "%dx%d+%dx%d/%32[^=]=%127s", &cols, &rows, &anchor_x, &anchor_y, shape_str, shape_filename);
214
215 if (!strcmp(shape_str, "rect" )) shape = CV_SHAPE_RECT;
216 else if (!strcmp(shape_str, "cross" )) shape = CV_SHAPE_CROSS;
217 else if (!strcmp(shape_str, "ellipse")) shape = CV_SHAPE_ELLIPSE;
218 else if (!strcmp(shape_str, "custom" )) {
219 shape = CV_SHAPE_CUSTOM;
222 } else {
224 "Shape unspecified or type '%s' unknown.\n", shape_str);
227 }
228
229 if (rows <= 0 || cols <= 0) {
231 "Invalid non-positive values for shape size %dx%d\n", cols, rows);
234 }
235
236 if (anchor_x < 0 || anchor_y < 0 || anchor_x >= cols || anchor_y >= rows) {
238 "Shape anchor %dx%d is not inside the rectangle with size %dx%d.\n",
239 anchor_x, anchor_y, cols, rows);
242 }
243
244 *kernel = cvCreateStructuringElementEx(cols, rows, anchor_x, anchor_y, shape,
values);
245 if (!*kernel) {
248 }
249
251 rows, cols, anchor_x, anchor_y, shape_str);
255 }
256
261
263 {
266 char default_kernel_str[] = "3x3+0x0/rect";
267 char *kernel_str =
NULL;
268 const char *buf = args;
270
271 if (args) {
273
274 if (!kernel_str)
276 }
277
279 (!kernel_str || !*kernel_str) ? default_kernel_str
280 : kernel_str,
285
286 if (!buf || sscanf(buf,
"|%d", &
dilate->nb_iterations) != 1)
287 dilate->nb_iterations = 1;
289 if (
dilate->nb_iterations <= 0) {
293 }
294 return 0;
295 }
296
298 {
301
302 cvReleaseStructuringElement(&
dilate->kernel);
303 }
304
306 {
309 cvDilate(inimg, outimg,
dilate->kernel,
dilate->nb_iterations);
310 }
311
313 {
316 cvErode(inimg, outimg,
dilate->kernel,
dilate->nb_iterations);
317 }
318
326
331 };
332
334 {
337
341 }
344 if (!strcmp(
s->name,
entry->name)) {
346 s->uninit =
entry->uninit;
347 s->end_frame_filter =
entry->end_frame_filter;
348
351 return s->init(
ctx,
s->params);
352 }
353 }
354
357 }
358
360 {
362
366 }
367
369 {
374 IplImage inimg, outimg;
375
380 }
382
385 s->end_frame_filter(
ctx, &inimg, &outimg);
387
389
391 }
392
393 #define OFFSET(x) offsetof(OCVContext, x)
394 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
399 };
400
402
404 {
408 },
409 };
410
412 {
415 },
416 };
417
422 .priv_class = &ocv_class,
428 };