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
42
44 {
45 IplImage *tmpimg;
46 int depth, channels_nb;
47
51 else return;
52
53 tmpimg = cvCreateImageHeader((CvSize){
frame->width,
frame->height}, depth, channels_nb);
55 img->imageData =
img->imageDataOrigin =
frame->data[0];
56 img->dataOrder = IPL_DATA_ORDER_PIXEL;
57 img->origin = IPL_ORIGIN_TL;
59 }
60
62 {
65 }
66
76
82
84 {
87 char type_str[128] = "gaussian";
88
93
94 if (args)
95 sscanf(args,
"%127[^|]|%d|%d|%lf|%lf", type_str, &
smooth->param1, &
smooth->param2, &
smooth->param3, &
smooth->param4);
96
97 if (!strcmp(type_str,
"blur" ))
smooth->type = CV_BLUR;
98 else if (!strcmp(type_str,
"blur_no_scale"))
smooth->type = CV_BLUR_NO_SCALE;
99 else if (!strcmp(type_str,
"median" ))
smooth->type = CV_MEDIAN;
100 else if (!strcmp(type_str,
"gaussian" ))
smooth->type = CV_GAUSSIAN;
101 else if (!strcmp(type_str,
"bilateral" ))
smooth->type = CV_BILATERAL;
102 else {
105 }
106
109 "Invalid value '%d' for param1, it has to be a positive odd number\n",
112 }
113 if ((
smooth->type == CV_BLUR ||
smooth->type == CV_BLUR_NO_SCALE ||
smooth->type == CV_GAUSSIAN) &&
116 "Invalid value '%d' for param2, it has to be zero or a positive odd number\n",
119 }
120
123 return 0;
124 }
125
127 {
131 }
132
134 void *log_ctx)
135 {
136 uint8_t *buf, *p, *pend;
139
142
143 /* prescan file to get the number of lines and the maximum width */
146 if (buf[
i] ==
'\n') {
147 if (*rows == INT_MAX) {
150 goto end;
151 }
152 ++(*rows);
155 }
else if (
w == INT_MAX) {
158 }
160 }
161 if (*rows > (SIZE_MAX / sizeof(int) / *cols)) {
163 *rows, *cols);
165 goto end;
166 }
169 goto end;
170 }
171
172 /* fill *values */
173 p = buf;
175 for (
i = 0;
i < *rows;
i++) {
176 for (j = 0;; j++) {
177 if (p > pend || *p == '\n') {
178 p++;
179 break;
180 } else
182 }
183 }
184
185 end:
187
188 #ifdef DEBUG
189 {
193 for (
i = 0;
i < *rows;
i++) {
194 for (j = 0; j < *cols; j++)
198 }
200 }
201 #endif
202
203 return 0;
204 }
205
207 {
208 char shape_filename[128] = "", shape_str[32] = "rect";
209 int cols = 0, rows = 0, anchor_x = 0, anchor_y = 0, shape = CV_SHAPE_RECT;
211
212 sscanf(buf, "%dx%d+%dx%d/%32[^=]=%127s", &cols, &rows, &anchor_x, &anchor_y, shape_str, shape_filename);
213
214 if (!strcmp(shape_str, "rect" )) shape = CV_SHAPE_RECT;
215 else if (!strcmp(shape_str, "cross" )) shape = CV_SHAPE_CROSS;
216 else if (!strcmp(shape_str, "ellipse")) shape = CV_SHAPE_ELLIPSE;
217 else if (!strcmp(shape_str, "custom" )) {
218 shape = CV_SHAPE_CUSTOM;
221 } else {
223 "Shape unspecified or type '%s' unknown.\n", shape_str);
226 }
227
228 if (rows <= 0 || cols <= 0) {
230 "Invalid non-positive values for shape size %dx%d\n", cols, rows);
233 }
234
235 if (anchor_x < 0 || anchor_y < 0 || anchor_x >= cols || anchor_y >= rows) {
237 "Shape anchor %dx%d is not inside the rectangle with size %dx%d.\n",
238 anchor_x, anchor_y, cols, rows);
241 }
242
243 *kernel = cvCreateStructuringElementEx(cols, rows, anchor_x, anchor_y, shape,
values);
244 if (!*kernel) {
247 }
248
250 rows, cols, anchor_x, anchor_y, shape_str);
254 }
255
260
262 {
265 char default_kernel_str[] = "3x3+0x0/rect";
266 char *kernel_str =
NULL;
267 const char *buf = args;
269
270 if (args) {
272
273 if (!kernel_str)
275 }
276
278 (!kernel_str || !*kernel_str) ? default_kernel_str
279 : kernel_str,
284
285 if (!buf || sscanf(buf,
"|%d", &
dilate->nb_iterations) != 1)
286 dilate->nb_iterations = 1;
288 if (
dilate->nb_iterations <= 0) {
292 }
293 return 0;
294 }
295
297 {
300
301 cvReleaseStructuringElement(&
dilate->kernel);
302 }
303
305 {
308 cvDilate(inimg, outimg,
dilate->kernel,
dilate->nb_iterations);
309 }
310
312 {
315 cvErode(inimg, outimg,
dilate->kernel,
dilate->nb_iterations);
316 }
317
325
330 };
331
333 {
336
340 }
343 if (!strcmp(
s->name, entry->
name)) {
344 s->init = entry->
init;
347
350 return s->init(
ctx,
s->params);
351 }
352 }
353
356 }
357
359 {
361
365 }
366
368 {
373 IplImage inimg, outimg;
374
379 }
381
384 s->end_frame_filter(
ctx, &inimg, &outimg);
386
388
390 }
391
392 #define OFFSET(x) offsetof(OCVContext, x)
393 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
398 };
399
401
403 {
407 },
408 };
409
411 {
414 },
415 };
416
421 .priv_class = &ocv_class,
427 };