This prepares real time colorization, by reusing existing structure.
Also an option for quick and dirty colorization is added.
cI = NULL;
/* render u and v change */
- rc = colorize(I, mI, NULL, NULL, 5, 1, 0, feat_names);
+ rc = colorize(I, mI, NULL, NULL, 5, 1, 0, 0, feat_names, NULL);
if (rc < 0) {
printerror("No memory! Use smaller image or add more memory.");
goto error;
* second dimension = height
* fourth dimension = time
*/
-int colorize_prepare(struct colorize *col)
+int colorize_prepare(struct colorize *col, int alloc)
{
int dimensions[4];
int ww, hh, kk, last_kk, d, temp, z;
temp=(temp+1)>>1;
ASSERT(col->max_depth >= 2, "image too small");
- col->nb_list = calloc(col->max_depth, sizeof(struct neighbors *));
+ if (alloc)
+ col->nb_list = calloc(col->max_depth, sizeof(struct neighbors *));
if (!col->nb_list) {
rc = -1;
goto error;
}
- col->marks = calloc(col->max_depth, sizeof(darray_t *));
+ if (alloc)
+ col->marks = calloc(col->max_depth, sizeof(darray_t *));
if (!col->marks) {
rc = -1;
goto error;
}
- col->values = calloc(col->max_depth, sizeof(darray_t *));
+ if (alloc)
+ col->values = calloc(col->max_depth, sizeof(darray_t *));
if (!col->values) {
rc = -1;
goto error;
}
- col->flows = calloc(col->max_depth, sizeof(darray_t *));
+ if (alloc)
+ col->flows = calloc(col->max_depth, sizeof(darray_t *));
if (!col->flows) {
rc = -1;
goto error;
}
- col->flows_i = calloc(col->max_depth, sizeof(darray_t *));
+ if (alloc)
+ col->flows_i = calloc(col->max_depth, sizeof(darray_t *));
if (!col->flows_i) {
rc = -1;
goto error;
}
if (col->scalexyz) {
/* the flows_xy are scaled in xy in the current level */
- col->flows_xy = calloc(col->max_depth, sizeof(darray_t *));
+ if (alloc)
+ col->flows_xy = calloc(col->max_depth, sizeof(darray_t *));
if (!col->flows_xy) {
rc = -1;
goto error;
dimensions[0] = ww;
dimensions[1] = hh;
dimensions[2] = last_kk;
- col->marks[d] = darrayCreate(3, dimensions);
+ if (alloc)
+ col->marks[d] = darrayCreate(3, dimensions);
if (!col->marks[d]) {
rc = -1;
goto error;
dimensions[1] = hh;
dimensions[2] = last_kk-1;
dimensions[3] = 2;
- col->flows[d] = darrayCreate(4, dimensions);
+ if (alloc)
+ col->flows[d] = darrayCreate(4, dimensions);
if (!col->flows[d]) {
rc = -1;
goto error;
}
- col->flows_i[d] = darrayCreate(4, dimensions);
+ if (alloc)
+ col->flows_i[d] = darrayCreate(4, dimensions);
if (!col->flows_i[d]) {
rc = -1;
goto error;
dimensions[1] = (hh+1)>>1;
dimensions[2] = kk-1;
dimensions[3] = 2;
- col->flows_xy[d] = darrayCreate(4, dimensions);
+ if (alloc)
+ col->flows_xy[d] = darrayCreate(4, dimensions);
if (!col->flows_xy[d]) {
rc = -1;
goto error;
dimensions[0] = ww;
dimensions[1] = hh;
dimensions[2] = kk;
- col->values[d] = darrayCreate(3, dimensions);
+ if (alloc)
+ col->values[d] = darrayCreate(3, dimensions);
if (!col->values[d]) {
rc = -1;
goto error;
/* generate array of neighbors */
#ifndef TEST_NO_RENDER
- col->nb_list[d] = gen_neighbor(ww, hh, kk, col->flows[d], col->flows_i[d]);
+ if (alloc)
+ col->nb_list[d] = gen_neighbor(ww, hh, kk, col->flows[d], col->flows_i[d]);
if (!col->nb_list[d]) {
rc = -1;
goto error;
}
/* weighten */
- weighten(luminance, col->nb_list[d]);
+ if (alloc)
+ weighten(luminance, col->nb_list[d]);
#endif
last_kk = kk;
dimensions[0] = col->w;
dimensions[1] = col->h;
dimensions[2] = col->k;
- col->init = darrayCreate(3, dimensions);
+ if (alloc)
+ col->init = darrayCreate(3, dimensions);
if (!col->init) {
rc = -1;
goto error;
darrayDestroy(col->init);
}
-int colorize(const darray_t *image, const darray_t *image_mark, const darray_t *flow, const darray_t *flow_i, int inner_iter, int outer_iter, int scalexyz, char **feat_names)
+int colorize(const darray_t *image, const darray_t *image_mark, const darray_t *flow, const darray_t *flow_i, int inner_iter, int outer_iter, int quick, int scalexyz, char **feat_names, struct colorize **_col)
{
- struct colorize col;
+ struct colorize *col;
int rc = -1;
+ int alloc = 0;
+
+ if (!_col || !(*_col)) {
+ col = calloc(sizeof(*col), 1);
+ memset(col, 0, sizeof(*col));
+ col->image = image;
+ col->image_mark = image_mark;
+ col->flow = flow;
+ col->flow_i = flow_i;
+ col->inner_iter = inner_iter;
+ col->outer_iter = outer_iter;
+ col->scalexyz = scalexyz;
+ alloc = 1;
+ }
+ /* use or assign *_col */
+ if (_col) {
+ if (!(*_col))
+ *_col = col;
+ else
+ col = *_col;
+ }
- memset(&col, 0, sizeof(col));
- col.image = image;
- col.image_mark = image_mark;
- col.flow = flow;
- col.flow_i = flow_i;
- col.inner_iter = inner_iter;
- col.outer_iter = outer_iter;
- col.scalexyz = scalexyz;
-
- rc = colorize_prepare(&col);
+ rc = colorize_prepare(col, alloc);
if (rc < 0)
goto error;
- rc = colorize_solve(&col, 0, feat_names);
+ rc = colorize_solve(col, quick, feat_names);
if (rc < 0)
goto error;
rc = 0;
error:
- colorize_free(&col);
+ if (!_col)
+ colorize_free(col);
return rc;
}
darray_t **flows_xy;
};
-int colorize_prepare(struct colorize *col);
+int colorize_prepare(struct colorize *col, int alloc);
int colorize_solve(struct colorize *col, int quick, char **feat_names);
void colorize_free(struct colorize *col);
-int colorize(const darray_t *image, const darray_t *image_mark, const darray_t *flow, const darray_t *flow_i, int inner_iter, int outer_iter, int scalexyz, char **feat_names);
+int colorize(const darray_t *image, const darray_t *image_mark, const darray_t *flow, const darray_t *flow_i, int inner_iter, int outer_iter, int quick, int scalexyz, char **feat_names, struct colorize **_col);
#endif
* options
*/
-static int in_itr_num = 5, out_itr_num = 1, optical_flow = 1, bright_contrast = 1, alpha_change = 1;
+static int in_itr_num = 5, out_itr_num = 1, quick = 0, optical_flow = 1, bright_contrast = 1, alpha_change = 1;
int scale = 1, scalexyz = 999;
static enum test test = NO_TEST;
{"depth", 1, 0, 'd'},
{"in-itr-num", 1, 0, 'i'},
{"out-itr-num", 1, 0, 'o'},
+ {"quick", 0, 0, 'q'},
{"zscale", 1, 0, 'z'},
{"brightness-contrast", 1, 0, 'b'},
{"optical-flow", 1, 0, 'f'},
{0, 0, 0, 0},
};
- c = getopt_long(argc, argv, "hd:i:o:z:b:f:s:t:", long_options, &option_index);
+ c = getopt_long(argc, argv, "hd:i:o:qz:b:f:s:t:", long_options, &option_index);
if (c == -1)
break;
out_itr_num = atoi(optarg);
skip_args += 2;
break;
+ case 'q':
+ quick = 1;
+ skip_args += 1;
+ break;
case 'z':
scalexyz = atoi(optarg);
skip_args += 2;
printf(" -d --depth <bits> Save images with given color bit depth (default=%d)\n", save_depth);
printf(" -i --in-itr-num <num> Alter inner iterations (weightening count) of colorization algorithm (default=%d)\n", in_itr_num);
printf(" -o --out-itr-num <num> Alter outer iterations (complete turns) of colorization algorithm (default=%d)\n", out_itr_num);
+ printf(" -q --quick Use quick render, but sufaces may be colorized incomplete\n");
printf(" -z --zscale <levels> How many grids (staring with the finest) should be scaled in z direction to generate the next coarse grid ");
if (scalexyz < 999)
printf("(default=%d)\n", scalexyz);
if (test != FLOW_NEXT && test != FLOW_PREV && test != MARKED && test != MASK && test != MASK_COLOR && test != BC_ONLY && test != BC_IMAGE) {
printf("Colorizing %d frames, please wait...\n", k);
- rc = colorize(I, mI, flow, flow_i, in_itr_num, out_itr_num, scalexyz, feat_names);
+ rc = colorize(I, mI, flow, flow_i, in_itr_num, out_itr_num, quick, scalexyz, feat_names, NULL);
if (rc < 0) {
if (k > 1)
printf("No memory! Use smaller frames or less frames between key frames or add more memory.");
#include "process.h"
/*
- * create image array and marked array
+ * create image array and marked array (if not already)
*/
int alloc_I_arrays(darray_t **I, darray_t **mI, int w, int h, int k, int *features, char **feat_names, int change_alpha, int change_bc)
{
int dims[4];
- dims[0] = w; dims[1] = h; dims[2] = k;
- *mI = darrayCreate(3, dims);
- if (!*mI) {
- printf("%s:failed to create mark array\n", __func__);
- goto exit;
+ if (!(*mI)) {
+ dims[0] = w; dims[1] = h; dims[2] = k;
+ *mI = darrayCreate(3, dims);
+ if (!*mI) {
+ printf("%s:failed to create mark array\n", __func__);
+ goto exit;
+ }
}
*features = 2; /* u and v */
feat_names[0] = "u";
feat_names[3] = "contrast";
}
}
- dims[0] = w; dims[1] = h; dims[2] = *features + 1; dims[3] = k;
- *I = darrayCreate(4, dims);
- if (!*I) {
- printf("%s:failed to create image array\n", __func__);
- goto exit;
+ if (!(*I)) {
+ dims[0] = w; dims[1] = h; dims[2] = *features + 1; dims[3] = k;
+ *I = darrayCreate(4, dims);
+ if (!*I) {
+ printf("%s:failed to create image array\n", __func__);
+ goto exit;
+ }
}
return 0;