void PathIndex::apply_translations(const vector<Translation>& translations) { // Convert from normal to partitioning translations // For each original node ID, we keep a vector of pairs of from mapping and // to mapping. We only keep pairs where the from mapping isn't empty. map<id_t, vector<pair<Mapping, Mapping>>> collated; for (auto& t : translations) { if (t.from().mapping_size() < 1 || t.to().mapping_size() != 1) { // Ensure the translations are the format we expect. They always have // at least one from mapping (but maybe an insert too) and exactly 1 // to mapping. cerr << "error:[vg::PathIndex] Bad translation: " << pb2json(t) << endl; throw runtime_error("Translation not in VG::edit() format"); } if (mapping_from_length(t.from().mapping(0)) == 0) { // This is a novel node and can't be on our path continue; } if (t.from().mapping(0).position().is_reverse()) { // Wait for the forward-orientation version continue; } // Stick the from and to mappings in the list for the from node collated[t.from().mapping(0).position().node_id()].push_back(make_pair(t.from().mapping(0), t.to().mapping(0))); } for (auto& kv : collated) { // For every original node and its replacement nodes // Sort the replacement mappings std::sort(kv.second.begin(), kv.second.end(), [](const pair<Mapping, Mapping>& a, const pair<Mapping, Mapping>& b) { // Return true if the a pair belongs before the b pair along the path through the original node return a.first.position().offset() <= b.first.position().offset(); }); // Make a new translation to cover the original node Translation covering; for (auto mapping_pair : kv.second) { // Split across these parts of new nodes *(covering.mutable_to()->add_mapping()) = mapping_pair.second; } // Just assume we take up the whole original node auto* from_mapping = covering.mutable_from()->add_mapping(); from_mapping->mutable_position()->set_node_id(kv.first); // Give it a full length perfect match auto* from_edit = from_mapping->add_edit(); from_edit->set_from_length(path_from_length(covering.to())); from_edit->set_to_length(from_edit->from_length()); // Apply this (single node) translation. // TODO: batch up a bit? apply_translation(covering); } }
// main function int main(int argc, char **argv) { // process input arguments if (argc != 5) { fprintf(stderr, "usage:\n\t%s dx dy in out\n", *argv); // 0 1 2 3 4 return 1; } int dx = atoi(argv[1]); int dy = atoi(argv[2]); char *filename_in = argv[3]; char *filename_out = argv[4]; // read input image int w, h; float *in = iio_read_image_float(filename_in, &w, &h); // allocate space for output image float *out = malloc(w*h*sizeof(float)); // run the algorithm apply_translation(out, dx, dy, in, w, h); // save output image iio_save_image_float(filename_out, out, w, h); // cleanup and exit free(in); free(out); return 0; }
// register two images // // w: width // h: height // left: left image // right: right image // out: right image after registration // void cregistration(float *out, float *left, float *right, int w, int h, int pd) { int d[2]; find_displacement(d, left, right, w, h, pd, 10); apply_translation(out, d[0], d[1], right, w, h, pd); }
// main function int main(int argc, char **argv) { // process input arguments if (argc != 6) { fprintf(stderr, "usage:\n\t%s image der1 der2 der3 der4\n", *argv); // 0 1 2 3 4 5 return 1; } char *filename_image = argv[1]; char *filename_der1 = argv[2]; char *filename_der2 = argv[3]; char *filename_der3 = argv[4]; char *filename_der4 = argv[5]; // read input image int w, h; float *im = iio_read_image_float(filename_image, &w, &h); // allocate space for the output image float *out1 = malloc(w*h*sizeof(float)); float *out2 = malloc(w*h*sizeof(float)); float *out3 = malloc(w*h*sizeof(float)); float *out4 = malloc(w*h*sizeof(float)); float *dec1 = malloc(w*h*sizeof(float)); float *dec2 = malloc(w*h*sizeof(float)); float *dec3 = malloc(w*h*sizeof(float)); float *dec4 = malloc(w*h*sizeof(float)); // creer les images translatees apply_translation(dec1, 1, 0, im, w, h); apply_translation(dec2, 0, 1, im, w, h); apply_translation(dec3, -1, 1, im, w, h); apply_translation(dec4, -1, -1, im, w, h); // calculer les derivees for(int j = 0 ; j < h ; j++) for(int i = 0 ; i < w ; i++) { out1[j*w+i] = (dec1[j*w+i] - im[j*w+i])*(dec1[j*w+i] - im[j*w+i]); out2[j*w+i] = (dec2[j*w+i] - im[j*w+i])*(dec2[j*w+i] - im[j*w+i]); out3[j*w+i] = (dec3[j*w+i] - im[j*w+i])*(dec3[j*w+i] - im[j*w+i])/2; out4[j*w+i] = (dec4[j*w+i] - im[j*w+i])*(dec4[j*w+i] - im[j*w+i])/2; } // save the output image iio_save_image_float(filename_der1, out1, w, h); iio_save_image_float(filename_der2, out2, w, h); iio_save_image_float(filename_der3, out3, w, h); iio_save_image_float(filename_der4, out4, w, h); // cleanup and exit free(im); free(out1); free(out2); free(out3); free(out4); free(dec1); free(dec2); free(dec3); free(dec4); return 0; }