/*! Write a binary grayscale pgm. */ int tc_write_image(tc_image *img, const char *filename) { int r, b, c; FILE * file; if ((file = fopen(filename, "wb")) == NULL) { tc_write_log("tc_write_image: Can't open file for writing.\r\n"); return ERR; } if (!strstr(filename, ".pgm") && !strstr(filename, ".PGM") && !strstr(filename, ".PPM") && !strstr(filename, ".ppm")) { tc_write_log("tc_write_image: Don't recognize image type."); fclose(file); return ERR; } switch(img->chans) { case 1: fprintf(file, "P5\n"); fprintf(file, "%d %d\n", img->cols, img->rows); fprintf(file, "255\n"); break; case 3: fprintf(file, "P6\n"); fprintf(file, "%d %d\n", img->cols, img->rows); fprintf(file, "255\n"); break; default: tc_write_log("tc_write_image: writing special pgm format"); /* our convention for channels > 3 */ fprintf(file, "H%i\n",img->chans); fprintf(file, "%d %d\n", img->cols, img->rows); fprintf(file, "255\n"); break; } for (r=0; r < img->rows; r++) { for (c=0; c < img->cols; c++) { for (b=0; b < img->chans; b++) { putc(pixel_to_uchar(tc_get(img, r, c, b)), file); } } } fclose(file); return OK; }
/*! Allocate a new image and fill it with a cropped portion of * the data in copy of src data. */ int tc_crop_image(tc_image **dst, tc_image *src, const int top, const int left, const int height, const int width) { int b,c,r; pixel_t x; if (src == NULL || dst == NULL) { tc_write_log("crop_image: NULL image.\r\n"); return ERR; } if (src->rows < 1 || src->cols < 1 || src->chans < 1) { tc_write_log("clone_image: Bad dimensions.\r\n"); return ERR; } if ((top < 0) || (top >= src->rows) || (left < 0) || (left >= src->cols) || (height <= 0) || ((top+height) > src->rows) || (width <= 0) || ((left+width) > src->cols)) { tc_write_log("crop_image: Bad subwindow dimensions.\r\n"); return ERR; } if (tc_alloc_image(dst, height, width, src->chans) == ERR) { tc_write_log("crop_image: Out of memory.\r\n"); return ERR; } for (b=0; b<(*dst)->chans; b++) { for (r=0; r<(*dst)->rows; r++) { for (c=0; c<(*dst)->cols; c++) { x = tc_get(src, top+r, left+c, b); tc_set((*dst), r, c, b, x); } } } return OK; }
cell_t *parse_word(seg_t w, cell_t *module, unsigned int n, cell_t *entry) { cell_t *c; cell_t *data = NULL; csize_t in = 0, out = 1; if(w.s[0] == '?' && w.n == 1) { c = param(T_ANY, entry); } else if(in = 1, out = 1, match_param_word("ap", w, &in, &out)) { c = func(OP_ap, ++in, ++out); } else if(in = 1, out = 1, match_param_word("comp", w, &in, &out)) { in += 2; c = func(OP_compose, in, ++out); } else if(in = 1, out = 1, match_param_word("external", w, &in, &out)) { c = func(OP_external, ++in, out); } else { cell_t *e = lookup_word(w); if(!e) e = module_lookup_compiled(w, &module); if(e) { in = e->entry.in; out = e->entry.out; if(FLAG(*e, entry, PRIMITIVE)) { if(e->op == OP_placeholder) { c = func(OP_placeholder, n + 1, 1); int x = trace_alloc(entry, n + 2); in = n; out = 1; data = var_create(T_LIST, tc_get(entry, x), 0, 0); } else { c = func(e->op, e->entry.in, e->entry.out); } } else { c = func(OP_exec, e->entry.in + 1, e->entry.out); data = e; } } else { return NULL; } } if(in) c->expr.arg[0] = (cell_t *)(intptr_t)(in - 1); TRAVERSE(c, out) { *p = dep(c); }
int tc_random_dataset(tc_dataset **d, char **image_filenames, char **label_filenames, tc_colormap *label_colormap, int nimages, int ndata, int sampling_method, long int seed) { srand(seed); tc_datum *datum; int i, j, current_label=1; if (d == NULL || image_filenames == NULL) { return ERR; } /* read images */ tc_dataset *dataset; dataset = (tc_dataset *) malloc(sizeof(tc_dataset)); if (dataset == NULL) { tc_write_log("Out of memory in random_dataset\n"); *d = NULL; return ERR; } tc_init_dataset(dataset); dataset->nimages = nimages; /* read images */ for (i=0; i<nimages; i++) { if (label_filenames == NULL) { tc_free_dataset(dataset); *d = NULL; return ERR; } tc_read_image(&(dataset->images[i]), image_filenames[i]); tc_read_image(&(dataset->labels[i]), label_filenames[i]); /* Change the values of the image if we're relabeling pixels * as classes (this reallocates labels[i] to have one channel) */ if (label_colormap != NULL) { if (tc_label_image(&(dataset->labels[i]), label_colormap, &(dataset->classes[i])) == ERR) { tc_free_dataset(dataset); *d = NULL; return ERR; } fprintf(stderr,"image %d contains:\n",i); for (j=0; j<label_colormap->nclasses; j++) { if (dataset->classes[i][j]>0) { fprintf(stderr, "%d instances of class %d\n", dataset->classes[i][j],j); } } } if (dataset->labels[i]->chans > 1) { tc_write_log("Use '--colorlabels' with multi-channel labels\n"); tc_free_dataset(dataset); *d = NULL; return ERR; } } /* set up datasets */ dataset->ndata = ndata; dataset->data = (tc_datum *) malloc(sizeof(tc_datum) * ndata); srand((uint64_t) NULL); for (i=0; i<ndata; i++) { datum = &(dataset->data[i]); int r, c, image, label = 0; while (!label) { /* look for a classified pixel */ image = rand() % nimages; if (sampling_method == TC_BALANCED_SAMPLING && dataset->classes[image][current_label]==0) { /* image does not contain this class, skip if balancing */ continue; } r = rand() % dataset->images[image]->rows; c = rand() % dataset->images[image]->cols; label = tc_get(dataset->labels[image], r, c, 0); if (label >= MAX_N_CLASSES || (sampling_method == TC_BALANCED_SAMPLING && label != current_label) ) { //fprintf(stderr, "Label image value %i > %d, ignoring.\n", // label, MAX_N_CLASSES); label = 0; } } datum->image = image; datum->r = r; datum->c = c; datum->label = label; dataset->represented[label]++; if (dataset->represented[label]==1) { fprintf(stderr, "new class %i at (%i,%i).\n", label, r, c); } if ((datum->label+1) > (dataset->nclasses)) { dataset->nclasses = (datum->label + 1); } if (i==ndata-1) { datum->next = NULL; } else { datum->next = &(dataset->data[i+1]); } if (sampling_method == TC_BALANCED_SAMPLING) { /* update current class \in [1,nclasses] */ current_label = ((current_label+1) % (label_colormap->nclasses)); if (current_label==0) current_label++; /* skip unlabeled */ } } fprintf(stderr, "%i classes in dataset.\n", dataset->nclasses); for (i=1; i<dataset->nclasses; i++) { fprintf(stderr, "class %d (%d total samples)\n",i,dataset->represented[i]); } *d = dataset; return OK; }