#include <time.h>
#include "../lib/darray.h"
#include "../lib/colorize.h"
-#include "ppm.h"
+#include "img.h"
#include "yuv.h"
#include "mark.h"
+#include "process.h"
+#include "settings.h"
#include "dir_seperator.h"
#ifdef WITH_OPENCV
#include "opticalflow.h"
#endif
-#define min(x,y) ((x < y) ? x : y)
-
static void print_help(const char *app);
static void print_test_help();
}
/*
- * scaling
- */
-
-/* scale down image in img_buffer by calculating average */
-static void scale_img(unsigned char *img_buffer, int width, int height, int scale)
-{
- int w, h, i, j, x, y;
- int r, g, b;
-
- if (scale == 1)
- return;
-
- w = width / scale;
- h = height / scale;
-
- for (i = 0; i < h; i++) {
- for (j = 0; j < w; j++) {
- r = g = b = 0;
- for (y = 0; y < scale; y++) {
- for (x = 0; x < scale; x++) {
- r += img_buffer[((i*scale+y) * width + j*scale+x) * 3 + 0];
- g += img_buffer[((i*scale+y) * width + j*scale+x) * 3 + 1];
- b += img_buffer[((i*scale+y) * width + j*scale+x) * 3 + 2];
- }
- }
- img_buffer[(i * w + j)*3 + 0] = r / scale / scale;
- img_buffer[(i * w + j)*3 + 1] = g / scale / scale;
- img_buffer[(i * w + j)*3 + 2] = b / scale / scale;
- }
- }
-}
-
-/* scale down mark map in mark_buffer */
-static void scale_mark(unsigned char *mark_buffer, int width, int height, int scale)
-{
- int w, h, i, j, x, y;
- unsigned char c, temp;
-
- if (scale == 1)
- return;
-
- w = width / scale;
- h = height / scale;
-
- for (i = 0; i < h; i++) {
- for (j = 0; j < w; j++) {
- c = 0;
- /* always use one index other than 0, if there is any in an areaa to be shrinked */
- for (y = 0; y < scale; y++) {
- for (x = 0; x < scale; x++) {
- temp = mark_buffer[(i*scale+y) * width + j*scale+x];
- if (temp)
- c = temp;
- }
- }
- mark_buffer[i * w + j] = c;
- }
- }
-}
-
-/*
* options
*/
-static int in_itr_num = 5, out_itr_num = 1, optical_flow = 1, bright_contrast = 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 char output_prefix[256] = "";
-static enum test {
- NO_TEST = 0,
- FLOW_NEXT,
- FLOW_PREV,
- MARKED,
- MASK,
- MASK_COLOR,
- BC_ONLY,
- BC_IMAGE,
-} test = NO_TEST;
+static enum test test = NO_TEST;
static int parse_test(const char *arg)
{
return BC_ONLY;
if (!strcmp(arg, "bc-image"))
return BC_IMAGE;
+ if (!strcmp(arg, "alpha"))
+ return ALPHA;
+ if (!strcmp(arg, "no-alpha"))
+ return NO_ALPHA;
+ if (!strcmp(arg, "removal-image"))
+ return REMOVAL_IMAGE;
return NO_TEST;
}
int option_index = 0, c;
static struct option long_options[] = {
{"help", 0, 0, 'h'},
+ {"depth", 1, 0, 'd'},
+ {"output-prefix", 1, 0, 'O'},
{"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, "hi:o:z:b:f:s:t:", long_options, &option_index);
+ c = getopt_long(argc, argv, "hd:O:i:o:qz:b:f:s:t:", long_options, &option_index);
if (c == -1)
break;
case 'h':
print_help(argv[0]);
exit(0);
+ case 'd':
+ save_depth = atoi(optarg);
+ skip_args += 2;
+ break;
+ case 'O':
+ strcpy(output_prefix, optarg);
+ skip_args += 2;
+ break;
case 'i':
in_itr_num = atoi(optarg);
skip_args += 2;
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;
bright_contrast = atoi(optarg);
skip_args += 2;
break;
+ case 'a':
+ alpha_change = atoi(optarg);
+ skip_args += 2;
+ break;
case 'f':
optical_flow = atoi(optarg);
skip_args += 2;
#include "../version.h"
);
printf("Usage: %s [options] <grey ppm image> <marked ppm image> <result ppm image> [<frames> <start>]\n", app);
- printf(" Colorize grey image using maked image and save to result image.\n");
+ printf(" Colorize grey image using marked image and save to result image.\n");
printf(" If frames and start frame is given, image names must include printf integer formatting (e.g. %%04d).\n");
printf("Usage: %s [options] <grey ppm image> marked <result ppm image>\n", app);
printf(" Colorize grey image using marked mask + palette and save to result image.\n");
printf(" Use list to view sequence segments between keyframes.\n");
printf("\nOptions:\n");
printf(" -h --help This help\n");
+ printf(" -d --depth <bits> Save images with given color bit depth (default=%d)\n", save_depth);
+ printf(" -O --output-prefix <path>/<prefix> Store result image of a sequence using this prefix\n");
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);
else
printf("(default=infinite)\n");
printf(" -b --brightness-contrast [0 | 1] Apply brightnes and contrast, if defined in palette by GUI (default=%d)\n", bright_contrast);
+ printf(" -a --alpha-change [0 | 1] Apply alpha channel change, if defined in palette by GUI (default=%d)\n", alpha_change);
#ifdef WITH_OPENCV
printf(" -f --optical-flow [0 | 1] Apply optical flow, if defined by GUI (default=%d)\n", optical_flow);
#endif
printf(" flow-next Optical flow plane to next image\n");
printf(" flow-prev Optical flow plane to previous image\n");
#endif
- printf(" marked Only apply makred colors to grey image\n");
+ printf(" marked Only apply marked colors to grey image\n");
printf(" mask Show mask of marked areas\n");
printf(" mask+color Show mask of marked areas + color\n");
printf(" bc-only Only apply brightness+contrast, leave colors of grey image as is\n");
printf(" bc-image Show brightness+contrast change on grey image as uv components\n");
+ printf(" alpha Show the image with alpha channel only\n");
+ printf(" no-alpha Show the image without alpha channel, to see pixles that are made transparent\n");
+ printf(" removal-image Show the image with \"transparency removal layer\" only, but keep u and v\n");
}
/*
int main(int argc, char *argv[])
{
- darray_t *gI = NULL, *cI = NULL, *markIm = NULL, *ntscIm = NULL, *resultIm;
- darray_t *dx = NULL, *dy = NULL, *idx = NULL, *idy = NULL;
-#ifdef WITH_OPENCV
- int *flow_map_x = NULL, *flow_map_y = NULL;
-#endif
- double *ptr, *ptr2, *ptr3;
+ darray_t *gI = NULL, *cI = NULL, *mI = NULL, *I = NULL;
+ darray_t *flow = NULL, *flow_i = NULL;
+ double *ptr_gI = NULL, *ptr_cI = NULL, *ptr_y = NULL, *ptr_u = NULL, *ptr_v = NULL, *ptr_a = NULL, *ptr_r = NULL, *ptr_b = NULL, *ptr_c = NULL, *ptr_m = NULL;
double diff, sum;
int dims[4];
- int width = 0, height = 0, w, h, k = 1, index = 0, count;
- unsigned char *img_buffer, *img_buffer_all = NULL, *mark_buffer = NULL;
- int rc, i, ii, j;
+ int w = 0, h = 0, load_w, load_h, k = 1, index = 0, z;
+ unsigned short *img = NULL;
+ unsigned char *mark_buffer = NULL;
+ int rc, x, y, i, ii;
time_t start, end;
const char* filename;
char first_filename[256];
int seq_offset = 0, seq_next = 0;
- int features, change_bc;
+ int features, change_bc, change_alpha;
+ char *feat_names[20];
int skip_args;
skip_args = handle_options(argc, argv);
time(&start);
- change_bc = 0;
- for (count = 0; count < k; count++) {
+ change_bc = change_alpha = 0;
+ for (z = 0; z < k; z++) {
if (sequence) {
- filename = sequence[count].filename;
+ filename = sequence[z].filename;
/* first_filename is set by load_sequence */
} else {
filename = argv[1];
}
#ifdef WITH_OPENCV
// load flow settings
- if (sequence || !strcmp(argv[2], "marked")) {
+ if (sequence && optical_flow)
flow_default();
- if (optical_flow)
- load_flow(first_filename);
+#endif
+ load_settings(first_filename);
+#ifdef WITH_OPENCV
+ // load flow settings
+ if (sequence && optical_flow) {
if (flow_enable == 0 && (test == FLOW_NEXT || test == FLOW_PREV)) {
fprintf(stderr, "Cannot test optical flow, because it is not enabled by GUI.\n");
exit (0);
}
#endif
// load original image and convert their RGB components to double RGB array
- rc = load_img(-1, &img_buffer, &w, &h, filename, index + count);
- if (rc) {
- fprintf(stderr, "Failed to load grey image\n");
+ img = load_img(&load_w, &load_h, filename, index + z);
+ if (!img) {
+ fprintf(stderr, "Failed to load grey image '%s'\n", filename);
return 0;
}
- scale_img(img_buffer, w, h, scale);
- if (count == 0) {
- width = w / scale;
- height = h / scale;
+ scale_img(img, load_w, load_h, scale);
+ if (z == 0) {
+ w = load_w / scale;
+ h = load_h / scale;
}
- if (w/scale != width || h/scale != height) {
+ if (load_w/scale != w || load_h/scale != h) {
fprintf(stderr, "Error: All input images must have equal dimenstions.\n");
return 0;
}
// now we know the dimensions, so we can create input arrays
if (!gI) {
- dims[0] = width; dims[1] = height; dims[2] = 3; dims[3] = k;
+ dims[0] = w; dims[1] = h; dims[2] = 3; dims[3] = k;
gI = darrayCreate(4, dims);
}
if (!gI) {
exit (0);
}
if (!cI) {
- dims[0] = width; dims[1] = height; dims[2] = 3; dims[3] = k;
+ dims[0] = w; dims[1] = h; dims[2] = 3; dims[3] = k;
cI = darrayCreate(4, dims);
}
if (!cI) {
printf("failed to create marked image array\n");
exit (0);
}
- ptr = darrayGetPr(gI) + width*height*3*count;
- img2array(img_buffer, width, height, ptr, width, height);
-#ifdef WITH_OPENCV
- if (k > 1 && flow_enable) {
- if (!img_buffer_all)
- img_buffer_all = malloc(width*height*3*k);
- if (!img_buffer_all) {
- printf("failed to create grey image array\n");
- exit (0);
- }
- memcpy(img_buffer_all + width*height*3*count, img_buffer, width*height*3);
- }
-#endif
- free(img_buffer);
+ img2array_short(img, w, h, darrayGetPr(gI) + w*h*3*z, w, h);
+ free(img);
if (sequence || !strcmp(argv[2], "marked")) {
char name[256];
unsigned char c;
// load marked mask and convert their RGB components to double YUV array
- memcpy(darrayGetPr(cI) + width*height*3*count, darrayGetPr(gI) + width*height*3*count, width*height*3 * sizeof(double));
+ memcpy(darrayGetPr(cI) + w*h*3*z, darrayGetPr(gI) + w*h*3*z, w*h*3 * sizeof(double));
/* add extra memory for unscaled data to prevent buffer overflow */
if (!mark_buffer)
- mark_buffer = (unsigned char *)malloc(width*height*k + w*h);
+ mark_buffer = (unsigned char *)malloc(w*h*k + load_w*load_h);
if (!mark_buffer) {
printf("no memory for mark buffer\n");
exit (0);
printf("failed to load palette for file: '%s'\n", filename);
exit (0);
}
- sprintf(name, filename, index + count);
+ sprintf(name, filename, index + z);
/* always load full unscaled image, then scale down */
- if (load_marked(mark_buffer + width*height*count, w, h, name) == 0) {
- scale_mark(mark_buffer + width*height*count, w, h, scale);
- ptr = darrayGetPr(cI) + width*height*3*count;
- for (i = 0; i < height; i++) {
- for (j = 0; j < width; j++) {
+ if (load_marked(mark_buffer + w*h*z, load_w, load_h, name) == 0) {
+ scale_mark(mark_buffer + w*h*z, load_w, load_h, scale);
+ ptr_cI = darrayGetPr(cI) + w*h*3*z;
+ for (y = 0; y < h; y++) {
+ for (x = 0; x < w; x++) {
/* do not apply mask on index 0 */
- c = mark_buffer[i*width+j + width*height*count];
+ c = mark_buffer[y*w+x + w*h*z];
if (c == 0)
continue;
/* check for any brightness/contrast change */
if (bright_contrast && (mark_palette[c-1].bright != 0 || mark_palette[c-1].contrast != 1))
change_bc = 1;
+ /* check for any alpha change */
+ if (alpha_change && mark_palette[c-1].alpha < 1)
+ change_alpha = 1;
/* do not apply white pixles, this meas: keep original color */
if (mark_palette[c-1].r == 255 && mark_palette[c-1].g == 255 && mark_palette[c-1].b == 255)
continue;
- ptr[i*width+j] = mark_palette[c-1].r / 255.0F;
- ptr[i*width+j + width*height] = mark_palette[c-1].g / 255.0F;
- ptr[i*width+j + width*height*2] = mark_palette[c-1].b / 255.0F;
+ ptr_cI[y*w+x] = mark_palette[c-1].r / 255.0F;
+ ptr_cI[y*w+x + w*h] = mark_palette[c-1].g / 255.0F;
+ ptr_cI[y*w+x + w*h*2] = mark_palette[c-1].b / 255.0F;
}
}
} else
- memset(mark_buffer + width*height*count, 0, width*height);
+ memset(mark_buffer + w*h*z, 0, w*h);
} else {
// load marked image and convert their RGB components to double YUV array
- rc = load_img(-1, &img_buffer, &w, &h, argv[2], index + count);
- if (!rc) {
- if (w/scale != width || h/scale != height) {
+ img = load_img(&load_w, &load_h, argv[2], index + z);
+ if (!img) {
+ scale_img(img, load_w, load_h, scale);
+ if (load_w/scale != w || load_h/scale != h) {
fprintf(stderr, "Error: All input images must have equal dimenstions.\n");
return 0;
}
- ptr = darrayGetPr(cI) + width*height*3*count;
- img2array(img_buffer, width, height, ptr, width, height);
- free(img_buffer);
+ img2array_short(img, w, h, darrayGetPr(cI) + w*h*3*z, w, h);
+ free(img);
} else {
fprintf(stderr, "Failed to load marked image, omitting...\n");
- memcpy(darrayGetPr(cI) + width*height*3*count, darrayGetPr(gI) + width*height*3*count, width*height*3 * sizeof(double));
+ memcpy(darrayGetPr(cI) + w*h*3*z, darrayGetPr(gI) + w*h*3*z, w*h*3 * sizeof(double));
}
}
}
- /* create color mask and ntsc arrays for the colorization process */
- dims[0] = width; dims[1] = height; dims[2] = k;
- markIm = darrayCreate(3, dims);
- if (!markIm) {
- printf("failed to create mark array\n");
- exit (0);
- }
- features = (change_bc) ? 4 : 2;
- dims[0] = width; dims[1] = height; dims[2] = features+1; dims[3] = k;
- ntscIm = darrayCreate(4, dims);
- if (!ntscIm) {
- printf("failed to create ntsc array\n");
- exit (0);
- }
+ rc = alloc_I_arrays(&I, &mI, w, h, k, &features, feat_names, change_alpha, change_bc);
+ if (rc)
+ exit(0);
+
+ for (z = 0; z < k; z++) {
+ set_I_ptr(I, mI, w, h, z, features, change_alpha, change_bc, &ptr_y, &ptr_u, &ptr_v, &ptr_a, &ptr_r, &ptr_b, &ptr_c, &ptr_m);
+ ptr_gI = darrayGetPr(gI) + w*h*3*z;
+ ptr_cI = darrayGetPr(cI) + w*h*3*z;
+
+ // convert original image into YUV
+ rgb2yuv(ptr_gI, ptr_gI, w, h);
+ // convert maked image into YUV
+ rgb2yuv(ptr_cI, ptr_cI, w, h);
- for (count = 0; count < k; count++) {
if (sequence || !strcmp(argv[2], "marked")) {
unsigned char c;
- ptr = darrayGetPr(markIm) + width*height*count;
// use marked mask to fill markIm
- for (i = 0; i < height; i++) {
- for (j = 0; j < width; j++) {
- if (j < width && i < height) {
- /* do not apply mask on index 0 */
- c = mark_buffer[i*width+j + width*height*count];
- } else
- c = 0;
+ for (y = 0; y < h; y++) {
+ for (x = 0; x < w; x++) {
+ /* do not apply mask on index 0 */
+ c = mark_buffer[y*w+x + w*h*z];
if (c)
- ptr[i*width+j] = 1.0F;
+ ptr_m[y*w+x] = 1.0F;
else
- ptr[i*width+j] = 0.0F;
+ ptr_m[y*w+x] = 0.0F;
}
}
} else {
// - convert into absolute (positive values)
// - sum all components to get grey image
// - apply threshold (pixle is 1F, if the absolute difference is > 0.01F)
- // original code: markIm=(sum(abs(gI-cI),3)>0.01);
- ptr = darrayGetPr(gI) + width*height*3*count;
- ptr2 = darrayGetPr(cI) + width*height*3*count;
- ptr3 = darrayGetPr(markIm) + width*height*count;
- for (i = 0, ii = width * height; i < ii; i++) {
+ // original code: markIm=(sum(abs(gI-cI),3)>0.01); (according to developers of the algorithm)
+ for (i = 0, ii = w * h; i < ii; i++) {
diff = 0;
- sum = ptr[i] - ptr2[i];
+ sum = ptr_gI[i] - ptr_cI[i];
if (sum < 0)
diff -= sum;
else
diff += sum;
- sum = ptr[i + ii] - ptr2[i + ii];
+ sum = ptr_gI[i + ii] - ptr_cI[i + ii];
if (sum < 0)
diff -= sum;
else
diff += sum;
- sum = ptr[i + ii + ii] - ptr2[i + ii + ii];
+ sum = ptr_gI[i + ii + ii] - ptr_cI[i + ii + ii];
if (sum < 0)
diff -= sum;
else
diff += sum;
if (diff > 0.01)
- ptr3[i] = 1.0F;
+ ptr_m[i] = 1.0;
else
- ptr3[i] = 0.0F;
- }
- }
-
- // convert original image into YUV
- ptr = darrayGetPr(gI) + width*height*3*count;
- rgb2yuv(ptr, ptr, width, height);
-
- // convert maked image into YUV
- ptr = darrayGetPr(cI) + width*height*3*count;
- rgb2yuv(ptr, ptr, width, height);
-
- if (test != BC_ONLY) {
- if (sequence || !strcmp(argv[2], "marked")) {
- unsigned char c;
- // generate NTSC image: use luminance from original image and chrominance from original or marked image
- ptr = darrayGetPr(gI) + width*height*3*count;
- ptr2 = darrayGetPr(cI) + width*height*3*count;
- ptr3 = darrayGetPr(ntscIm) + width*height*(features+1)*count;
- /* use original y component */
- memcpy(ptr3, ptr, width * height * sizeof(double));
- /* apply new uv components */
- for (i = 0; i < height; i++) {
- for (j = 0; j < width; j++) {
- c = mark_buffer[i*width+j + width*height*count];
- if (c == 0) {
- ptr3[width * height + width * i + j] = ptr[width * height + width * i + j];
- ptr3[width * height * 2 + width * i + j] = ptr[width * height * 2 + width * i + j];
- } else {
- ptr3[width * height + width * i + j] = ptr2[width * height + width * i + j];
- ptr3[width * height * 2 + width * i + j] = ptr2[width * height * 2 + width * i + j];
- }
- }
- }
- } else {
- // generate NTSC image: use luminance from original image and chrominance from maked image
- ptr = darrayGetPr(gI) + width*height*3*count;
- ptr2 = darrayGetPr(cI) + width*height*3*count;
- ptr3 = darrayGetPr(ntscIm) + width*height*(features+1)*count;
- memcpy(ptr3, ptr, width * height * sizeof(double));
- memcpy(ptr3 + width * height, ptr2 + width * height, width * height * sizeof(double));
- memcpy(ptr3 + width * height * 2, ptr2 + width * height * 2, width * height * sizeof(double));
+ ptr_m[i] = 0.0;
}
- } else {
- /* use grey image as result if BC_ONLY test is selected */
- ptr = darrayGetPr(gI) + width*height*3*count;
- ptr2 = darrayGetPr(ntscIm) + width*height*(features+1)*count;
- memcpy(ptr2, ptr, width * height * 3 * sizeof(double));
}
- if (change_bc) {
- unsigned char c;
- ptr2 = darrayGetPr(ntscIm) + width*height*(features+1)*count;
- /* use original y component */
- memcpy(ptr2, ptr, width * height * sizeof(double));
- /* apply brightness and contrast from makred pixles to uv components of grey image */
- for (i = 0; i < height; i++) {
- for (j = 0; j < width; j++) {
- /* use unchanged brightness and contrast on index 0 */
- c = mark_buffer[i*width+j + width*height*count];
- if (c == 0) {
- ptr2[i*width+j + width*height*3] = 0;
- ptr2[i*width+j + width*height*4] = 0.1;
- } else {
- ptr2[i*width+j + width*height*3] = mark_palette[c-1].bright / 10.0;
- ptr2[i*width+j + width*height*4] = mark_palette[c-1].contrast / 10.0;
- }
- }
- }
- }
+ prepare_arrays(w, h, change_alpha, change_bc, mark_buffer+w*h*z, ptr_gI, ptr_cI, ptr_y, ptr_u, ptr_v, ptr_a, ptr_r, ptr_b, ptr_c, ptr_m, test);
}
- /* create flow vectors */
- dims[0] = width; dims[1] = height; dims[2] = k - 1;
- dx = darrayCreate(3, dims);
- if (!dx) {
- printf("failed to create array\n");
- exit (0);
- }
- dy = darrayCreate(3, dims);
- if (!dy) {
- printf("failed to create array\n");
- exit (0);
- }
- idx = darrayCreate(3, dims);
- if (!idx) {
- printf("failed to create array\n");
- exit (0);
- }
- idy = darrayCreate(3, dims);
- if (!idy) {
- printf("failed to create array\n");
- exit (0);
- }
#ifdef WITH_OPENCV
if (k > 1 && flow_enable) {
- printf("Calculating optical flow for %d frames: steps=%d win=%d max=%d\n", k, flow_steps/scale, flow_win/scale, flow_max/scale);
- flow_map_x = (int *)malloc(sizeof(int)*width*height);
- if (!flow_map_x) {
- printf("failed to alloc array\n");
+ /* create flow vectors */
+ dims[0] = w; dims[1] = h; dims[2] = k - 1; dims[3] = 2;
+ flow = darrayCreate(4, dims);
+ if (!flow) {
+ printf("failed to create array\n");
exit (0);
}
- flow_map_y = (int *)malloc(sizeof(int)*width*height);
- if (!flow_map_y) {
- printf("failed to alloc array\n");
+ flow_i = darrayCreate(4, dims);
+ if (!flow_i) {
+ printf("failed to create array\n");
exit (0);
}
+ printf("Calculating optical flow for %d frames: window=%d\n", k, flow_window/scale);
+ } else if (k > 1)
+ printf("Note: Optical flow is not activated!\n");
+ for (z = 0; z < k-1; z++) {
+ if (flow)
+ create_flow_maps(NULL, darrayGetPr(gI) + w*h*3*(z+1), darrayGetPr(gI) + w*h*3*z, w, h, flow_window/scale, 0, NULL, NULL, darrayGetPr(flow) + w*h*z, darrayGetPr(flow) + w*h*z + w*h*(k-1), NULL);
+ if (flow_i)
+ create_flow_maps(darrayGetPr(gI) + w*h*3*z, NULL, darrayGetPr(gI) + w*h*3*(z+1), w, h, flow_window/scale, 0, darrayGetPr(flow_i) + w*h*z, darrayGetPr(flow_i) + w*h*z + w*h*(k-1), NULL, NULL, NULL);
}
- for (count = 0; count < k-1; count++) {
- if (flow_map_x && flow_map_y)
- create_flow_maps(NULL, img_buffer_all + width*height*3*(count+1), img_buffer_all + width*height*3*count, width, height, flow_steps/scale, flow_win/scale, flow_max/scale, NULL, NULL, flow_map_x, flow_map_y, NULL);
- ptr = darrayGetPr(dx) + width*height*count;
- if (flow_map_x) {
- for (j = 0; j < height; j++) {
- for (i = 0; i < width; i++)
- ptr[i + width * j] = flow_map_x[width*j + i];
- }
- }
- ptr = darrayGetPr(dy) + width*height*count;
- if (flow_map_y) {
- for (j = 0; j < height; j++) {
- for (i = 0; i < width; i++)
- ptr[i + width * j] = flow_map_y[width*j + i];
- }
- }
- if (flow_map_x && flow_map_y)
- create_flow_maps(img_buffer_all + width*height*3*count, NULL, img_buffer_all + width*height*3*(count+1), width, height, flow_steps/scale, flow_win/scale, flow_max/scale, flow_map_x, flow_map_y, NULL, NULL, NULL);
- ptr = darrayGetPr(idx) + width*height*count;
- if (flow_map_x) {
- for (j = 0; j < height; j++) {
- for (i = 0; i < width; i++)
- ptr[i + width * j] = flow_map_x[width*j + i];
- }
- }
- ptr = darrayGetPr(idy) + width*height*count;
- if (flow_map_y) {
- for (j = 0; j < height; j++) {
- for (i = 0; i < width; i++)
- ptr[i + width * j] = flow_map_y[width*j + i];
- }
- }
- }
+#else
+ if (k > 1)
+ printf("Note: Optical flow is not compiled in!\n");
#endif
- free(img_buffer_all);
- img_buffer_all = NULL;
darrayDestroy(gI);
gI = NULL;
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);
- resultIm = colorize(ntscIm, markIm, dx, dy, idx, idy, in_itr_num, out_itr_num, scalexyz);
- if (!resultIm) {
+ 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.");
else
printf("No memory! Use smaller image or add more memory.");
exit(-1);
}
- } else
- resultIm = darrayClone(ntscIm);
-
- /* if we have a change, we apply brightness+contrast from bcIm */
- if (change_bc) {
- for (count = 0; count < k; count++) {
- ptr = darrayGetPr(ntscIm) + width*height*(features+1)*count;
- ptr2 = darrayGetPr(resultIm) + width*height*(features+1)*count;
- for (i = 0; i < height; i++) {
- for (j = 0; j < width; j++) {
- /* apply contrast */
- ptr2[width * i + j] = (ptr2[width * i + j] - 0.5) * ptr[width * height * 4 + width * i + j] * 10.0 + 0.5;
- /* apply brightness */
- ptr2[width * i + j] += ptr[width * height * 3 + width * i + j] * 10.0;
- if (ptr2[width * i + j] < 0)
- ptr2[width * i + j] = 0;
- if (ptr2[width * i + j] > 1)
- ptr2[width * i + j] = 1;
-#if 0
-#warning TEST: show brightness and contrast change as uv vectors on a grey array */
-ptr2[width * i + j] = 0.5;
-ptr2[width * height + width * i + j] = ptr[width * height + width * i + j] * 10;
-ptr2[width * height * 2 + width * i + j] = ptr[width * height * 2 + width * i + j] * 10 - 1;
-#endif
- }
- }
- }
}
- darrayDestroy(ntscIm);
- ntscIm = NULL;
+ for (z = 0; z < k; z++) {
+ set_I_ptr(I, mI, w, h, z, features, change_alpha, change_bc, &ptr_y, &ptr_u, &ptr_v, &ptr_a, &ptr_r, &ptr_b, &ptr_c, &ptr_m);
+ postpare_arrays(w, h, change_alpha, change_bc, ptr_y, ptr_u, ptr_v, ptr_a, ptr_r, ptr_b, ptr_c, ptr_m, test);
#ifdef WITH_OPENCV
- if (test == FLOW_NEXT || test == FLOW_PREV) {
- /* apply flow planes to result image as u and y vector */
- for (count = 0; count < k; count++) {
- ptr = darrayGetPr(resultIm) + width*height*(features+1)*count;
+ if (test == FLOW_NEXT || test == FLOW_PREV) {
+ double *ptr_f1 = NULL, *ptr_f2 = NULL;
+ /* apply flow planes to result image as u and y vector */
if (test == FLOW_NEXT) {
- ptr2 = darrayGetPr(dx) + width*height*count;
- ptr3 = darrayGetPr(dy) + width*height*count;
+ if (flow) {
+ ptr_f1 = darrayGetPr(flow) + w*h*z;
+ ptr_f2 = darrayGetPr(flow) + w*h*z*(k-1);
+ }
} else {
- ptr2 = darrayGetPr(idx) + width*height*count;
- ptr3 = darrayGetPr(idy) + width*height*count;
+ if (flow_i) {
+ ptr_f1 = darrayGetPr(flow_i) + w*h*z;
+ ptr_f2 = darrayGetPr(flow_i) + w*h*z*(k-1);
+ }
}
- for (i = 0; i < height; i++) {
- for (j = 0; j < width; j++) {
- ptr[width * i + j] = 0.5;
- if (count < k-1) {
- ptr[width * i + j + width*height] = ptr2[width * i + j] / flow_max;
- ptr[width * i + j + width*height*2] = ptr3[width * i + j] / flow_max;
- } else {
- ptr[width * i + j + width*height] = 0;
- ptr[width * i + j + width*height*2] = 0;
+ if (ptr_f1 && ptr_f1) {
+ for (y = 0; y < h; y++) {
+ for (x = 0; x < w; x++) {
+ ptr_y[w*y+x] = 0.5;
+ if (z < k-1) {
+ ptr_u[w*y+x] = ptr_f1[w * y + x] / 50;
+ ptr_v[w*y+x] = ptr_f2[w * y + x] / 50;
+ } else {
+ ptr_u[w*y+x] = 0;
+ ptr_v[w*y+x] = 0;
+ }
}
}
}
}
- }
#endif
- if (test == MASK) {
- /* apply maked mask as image */
- for (count = 0; count < k; count++) {
- ptr = darrayGetPr(resultIm) + width*height*(features+1)*count;
- ptr2 = darrayGetPr(markIm) + width*height*count;
- for (i = 0; i < height; i++) {
- for (j = 0; j < width; j++) {
- ptr[width * i + j] = ptr2[width * i + j];
- ptr[width * i + j + width*height] = ptr2[width * i + j];
- ptr[width * i + j + width*height*2] = ptr2[width * i + j];
- }
- }
- }
- }
-
- if (test == MASK_COLOR) {
- /* apply maked mask on grey image */
- for (count = 0; count < k; count++) {
- ptr = darrayGetPr(resultIm) + width*height*(features+1)*count;
- ptr2 = darrayGetPr(markIm) + width*height*count;
- for (i = 0; i < height; i++) {
- for (j = 0; j < width; j++) {
- /* darken unmarked areas, make maked areas with uniformed brightness */
- if (ptr2[width * i + j] < 0.5F)
- ptr[width * i + j] = ptr[width * i + j] / 4;
- else
- ptr[width * i + j] = 0.5F;
- }
- }
- }
- }
-
- if (test == BC_IMAGE) {
- /* apply bc image as result image */
- for (count = 0; count < k; count++) {
- ptr = darrayGetPr(resultIm) + width*height*(features+1)*count;
- ptr2 = darrayGetPr(resultIm) + width*height*(features+1)*count;
- /* use uniformed brightness as y component */
- for (i = 0; i < height; i++) {
- for (j = 0; j < width; j++) {
- ptr[width * i + j] = 0.5F;
- }
- }
- if (change_bc) {
- ptr2 = darrayGetPr(resultIm) + width*height*(features+1)*count;
- memcpy(ptr+width*height, ptr2+width*height*3, width * height * sizeof(double));
- memcpy(ptr+width*height*2, ptr2+width*height*4, width * height * sizeof(double));
- } else {
- memset(ptr+width*height, 0, width * height * sizeof(double));
- memset(ptr+width*height*2, 0, width * height * sizeof(double));
- }
- }
- }
-
- // save result YUV array to image with RGB components
- img_buffer = (unsigned char *)malloc(width * height * 3);
- if (!img_buffer) {
- fprintf(stderr, "Failed to allocate image buffer\n");
- return 0;
- }
- for (count = 0; count < k; count++) {
- ptr = darrayGetPr(resultIm) + width*height*(features+1)*count;
- yuv2rgb(ptr, ptr, width, height);
- array2img(ptr, width, height, img_buffer, width, height);
+ // save result YUV array to image with RGB components
+ yuv2rgb(ptr_y, ptr_y, w, h);
if (sequence) {
static char name[256], *p, *q;
- p = sequence[count].filename;
+ p = sequence[z].filename;
while((q = strchr(p, DIR_SEPERATOR)))
p = q + 1;
- strcpy(name, sequence[count].filename);
- name[p - sequence[count].filename] = '\0';
- strcat(name, "colorized_");
+ if (output_prefix[0] == '\0') {
+ strcpy(name, sequence[z].filename);
+ name[p - sequence[z].filename] = '\0';
+ strcat(name, "colorized_");
+ } else {
+ strcpy(name, output_prefix);
+ }
strcat(name, p);
filename = name;
} else
filename = argv[3];
- save_img(img_buffer, width, height, filename, index + count);
+ /* don't save alpha on these tests */
+ if (test == ALPHA || test == REMOVAL_IMAGE || test == NO_ALPHA)
+ save_img_array(ptr_y, w, h, 0, filename, index + z);
+ else
+ save_img_array(ptr_y, w, h, change_alpha, filename, index + z);
}
- free(img_buffer);
time(&end);
printf("Elapsed time: %d minutes, %d seconds\n", (int)(end-start)/60, (int)(end-start)%60);
// destroy
- darrayDestroy(resultIm);
- resultIm = NULL;
- darrayDestroy(dx);
- dx = NULL;
- darrayDestroy(dy);
- dy = NULL;
- darrayDestroy(idx);
- idx = NULL;
- darrayDestroy(idy);
- idy = NULL;
-#ifdef WITH_OPENCV
- free(flow_map_x);
- flow_map_x = NULL;
- free(flow_map_y);
- flow_map_y = NULL;
-#endif
- darrayDestroy(markIm);
- markIm = NULL;
+ darrayDestroy(I);
+ I = NULL;
+ darrayDestroy(mI);
+ mI = NULL;
+ darrayDestroy(flow);
+ flow = NULL;
+ darrayDestroy(flow_i);
+ flow_i = NULL;
free(mark_buffer);
mark_buffer = NULL;