Пример #1
0
 void padder<T>::mirror(idx<T> &in, idx<T> &padded) {
   idx<T> tmp, tmp2;
   int i;
   // mirror border left
   for (i = std::max(0, (int) (ncol - in.dim(1) / 2)); i < ncol; ++i) {
     tmp2 = in.narrow(1, 1, ncol - i - 1);
     tmp = padded.narrow(1, 1, i);
     tmp = tmp.narrow(2, in.dim(2), ncol);
     idx_copy(tmp2, tmp);
   }
   // mirror border right
   for (i = std::max(0, (int) (ncol - in.dim(1) / 2)); i < ncol; ++i) {
     tmp2 = in.narrow(1, 1, in.dim(1) - ncol - 1 + i);
     tmp = padded.narrow(1, 1, padded.dim(1) - 1 - i);
     tmp = tmp.narrow(2, in.dim(2), ncol);
     idx_copy(tmp2, tmp);
   }
   // mirror border top using out as input
   for (i = std::max(0, (int) (nrow - in.dim(2) / 2)); i < nrow; ++i) {
     tmp2 = padded.narrow(2, 1, nrow + nrow - i - 1);
     tmp = padded.narrow(2, 1, i);
     idx_copy(tmp2, tmp);
   }
   // mirror border bottom using out as input
   for (i = std::max(0, (int) (nrow - in.dim(2) / 2)); i < nrow; ++i) {
     tmp2 = padded.narrow(2, 1, padded.dim(2) - nrow * 2 - 1 + i);
     tmp = padded.narrow(2, 1, padded.dim(2) - 1 - i);
     idx_copy(tmp2, tmp);
   }    
 }
Пример #2
0
idx<T> image_region_to_rect(idx<T> &im, const rect<int> &r, uint oheight,
                            uint owidth, rect<int> *cropped,
                            uint dh, uint dw)
{
    // TODO: check that rectangle is within image
    if (im.order() != 2 && im.order() != 3)
        eblerror("expected a 2d or 3d input but got " << im);
    idxdim d(im);
    d.setdim(dh, oheight);
    d.setdim(dw, owidth);
    idx<T> res(d);

    float hcenter = r.h0 + (float)r.height / 2; // input height center
    float wcenter = r.w0 + (float)r.width / 2; // input width center
    // // limit centers to half the width/height away from borders
    // // to handle incorrect regions
    // hcenter = MIN((float)im.dim(dh) - (float)r.height/2,
    //            std::max((float)r.height/2, hcenter));
    // wcenter = MIN((float)im.dim(dw) - (float)r.width/2,
    //            std::max((float)r.width/2, wcenter));
    float h0 = hcenter - (float)oheight / 2; // out height offset in input
    float w0 = wcenter - (float)owidth / 2; // out width offset in input
    float h1 = hcenter + (float)oheight / 2;
    float w1 = wcenter + (float)owidth / 2;
    int gh0 = (int)std::max(0, (int)MIN(im.dim(dh), h0)); // input h offset
    int gw0 = (int)std::max(0, (int)MIN(im.dim(dw), w0)); // input w offset
    int gh1 = (int)std::max(0, (int)MIN(im.dim(dh), h1));
    int gw1 = (int)std::max(0, (int)MIN(im.dim(dw), w1));
    int h = gh1 - gh0 + std::max(0, -r.h0); // out height narrow
    int w = gw1 - gw0 + std::max(0, -r.w0); // out width narrow
    int fh0 = (int)std::max(0, (int)(gh0 - h0)); // out height offset narrow
    int fw0 = (int)std::max(0, (int)(gw0 - w0)); // out width offset narrow

    // narrow original image
    int hmin = std::max(0, std::min((int)res.dim(dh) - fh0,
                                    std::min((int)im.dim(dh) - gh0, h)));
    int wmin = std::max(0, std::min((int)res.dim(dw) - fw0,
                                    std::min((int)im.dim(dw) - gw0, w)));
    // clear result image
    idx_clear(res);
    if (hmin != 0 && wmin != 0)  // only copy if overlap with image
    {
        idx<T> tmpim = im.narrow(dh, hmin, gh0);
        tmpim = tmpim.narrow(dw, wmin, gw0);
        // narrow target image
        idx<T> tmpres = res.narrow(dh, hmin, fh0);
        tmpres = tmpres.narrow(dw, wmin, fw0);
        // copy original to target
        idx_copy(tmpim, tmpres);
    }
    // set cropped rectangle to region in the output image containing input
    if (cropped)
    {
        cropped->h0 = fh0;
        cropped->w0 = fw0;
        cropped->height = hmin;
        cropped->width = wmin;
    }
    return res;
}
Пример #3
0
 void image_paste_center(idx<T> &in, idx<T> &out) {
   if (in.order() != 3 || out.order() != 3)
     eblerror("expected 3d idx but got " << in << " and " << out);
   int d0 = 1;
   int d1 = d0 + 1;
   intg ci = in.dim(d0) - out.dim(d0);
   intg cj = in.dim(d1) - out.dim(d1);
   intg xci = (int) (ci / 2);
   intg xcj = (int) (cj / 2);
   idx<T> i = in.narrow(d0, out.dim(d0), xci);
   i = i.narrow(d1, out.dim(d1), xcj);
   idx_copy(i, out);
 }
Пример #4
0
 void padder<T>::pad(idx<T> &in, idx<T> &out) {
   // allocate padded buffer
   idxdim d = in;
   d.setdim(1, d.dim(1) + nrow + nrow2);
   d.setdim(2, d.dim(2) + ncol + ncol2);
   if (out.get_idxdim() != d)
     out.resize(d);
   idx_clear(out);
   // copy in to padded buffer
   idx<T> tmp = out.narrow(1, in.dim(1), nrow);
   tmp = tmp.narrow(2, in.dim(2), ncol);
   idx_copy(in, tmp);
   if (bmirror)
     mirror(in, out);
 }
Пример #5
0
unsigned int draw_layer(idx<float> &layer, const char *s,
			unsigned int h, unsigned int w) {
#ifdef __GUI__
  draw_matrix(layer, s, h, w, 1.0, 1.0, (float)-1.0, (float)1.0);
#endif
  return layer.dim(1) + 3;
}
Пример #6
0
 idx<T> padder<T>::pad(idx<T> &in) {
   // allocate padded buffer
   idxdim d = in;
   d.setdim(1, d.dim(1) + nrow + nrow2);
   d.setdim(2, d.dim(2) + ncol + ncol2);
   idx<T> out(d);
   idx_clear(out);
   // copy in to padded buffer
   idx<T> tmp = out.narrow(1, in.dim(1), nrow);
   tmp = tmp.narrow(2, in.dim(2), ncol);
   idx_copy(in, tmp);
   if (bmirror)
     mirror(in, out);
   // return result
   return out;
 }
Пример #7
0
  // display original and new images.
  void display_images(idx<ubyte> &classes, int label, idx<float> &original,
		      idx<float> &new_images, const int channels_mode,
		      idx<ubyte> &ds_names, int current_ds) {
#ifdef __GUI__
    unsigned int h = 0, w = 0;
    static unsigned int cnt = 0;
    idx<float> layer;

    // only display every 15 images
    if (cnt++ % 15 != 0) return ;
    
    // reset window and do a batch display
    disable_window_updates();
    clear_window();
    
    // original image (RGB)
    static string s;
    s = "RGB"; s += " - "; s += (char *) classes[label].idx_ptr();
    draw_matrix(original, s.c_str(), h, w);
    w = 0; h += new_images.dim(1) + 5;
    gui << at(h, w) << black_on_white();
    gui << ds_names[current_ds].idx_ptr() << " dataset:";
    h += 15;
    
    // new images
    idx_bloop1(image, new_images, float) {
      switch (channels_mode) {
      case 1: // YpUV
	w += draw_layer(image, 2, 0, "Yp", h, w);
	w += draw_layer(image, 2, 1, "U", h, w);
	w += draw_layer(image, 2, 2, "V", h, w);
	break ;
      case 3: // Yp
	w += draw_layer(image, 2, 0, "Yp", h, w);
	break ;
      case 4: { // YpH3
	w += draw_layer(image, 2, 0, "Yp", h, w);
	w += draw_layer(image, 2, 1, "H3", h, w);
	idx_addc(layer, (float)1.0, layer);
	idx_dotc(layer, (float)210.0, layer);
	w += layer.dim(1) + 5;
	static idx<float> rgb(layer.dim(0), layer.dim(1), 3);
	h3_to_rgb(layer, rgb);
	draw_matrix(rgb, "H3 (colored)", h, w);
	break ;
      }
      case 5: // VpH2SV
	w += draw_layer(image, 2, 0, "Vp", h, w);
	w += draw_layer(image, 2, 1, "H1", h, w);
	w += draw_layer(image, 2, 2, "H2", h, w);
	w += draw_layer(image, 2, 3, "S", h, w);
	w += draw_layer(image, 2, 4, "V", h, w);
	w = 0; h += image.dim(1) + 5;
	break ;
      }
    }
    enable_window_updates();
#endif
  }
Пример #8
0
  unsigned int draw_layer(idx<float> &img, int dimn, int layern, const char *s,
			  unsigned int h, unsigned int w) {
#ifdef __GUI__
    idx<float> layer = img.select(dimn, layern);
    draw_matrix(layer, s, h, w, 1.0, 1.0, (float)-1.0, (float)1.0);
#endif
    return img.dim(1) + 3;
  }
Пример #9
0
  //! Return an idx of dimensions Nx2 containing all possible N similar pairs.
  idx<int> make_pairs(idx<int> &labels) {
    // allocate maximum number of pairs
    idx<int> pairs((labels.dim(0) * (labels.dim(0) - 1)) / 2, 2);
    int n = 0, r = 0;
    // for each label, loop over all following labels to find pairs
    for (int i = 0; i < labels.dim(0) - 1; ++i) {
      for (int j = i + 1; j < labels.dim(0); ++j) {
	if (labels.get(i) == labels.get(j)) {
	  r = drand(0.0, 1.0); // randomize distribution as 1st or 2nd element
	  pairs.set(i, n, (r > 0.5) ? 0 : 1);
	  pairs.set(j, n, (r > 0.5) ? 1 : 0);
	  n++;
	}
      }
    }
    pairs.resize(n, pairs.dim(1));
    return pairs;
  }
Пример #10
0
    void serialize(Archive & ar, idx<ubyte>& mat, const unsigned int version) {
      intg d1, d2, d3;
      if (Archive::is_saving::value) { // saving to stream
	if (!mat.contiguousp())
	  eblerror("expected contiguous idx for serialization");
	if (mat.order() != 3)
	  eblerror("no support for idx order != 3 for now, got: " << mat);

	d1 = mat.dim(0); 
	d2 = mat.dim(1); 
	d3 = mat.dim(2); 
	
	ar & d1;
	ar & d2;
	ar & d3;
	idx_aloop1(m, mat, ubyte) {
	  ar & *m;
	}
      } else { // loading from stream
Пример #11
0
 cost_module<T1,T2,Tstate1,Tstate2>::cost_module(idx<T1> &targets_)
   : targets(targets_), in2(targets.select(0, 0)), energies(targets_.dim(0)) {
 }
Пример #12
0
int display(list<string>::iterator &ifname,
	    bool signd, bool load, list<string> *mats) {
  //cout << "displaying " << ifname->c_str() << endl;
  // conf mode
  if (conf)
    return display_net<T>(ifname, signd, load, mats);
  // mat mode
  if (is_matrix(ifname->c_str())) {
    idxdim d = get_matrix_dims(ifname->c_str());
    if (interleaved)
      d.shift_dim(0, 2);
    if (save_individually || print
        || !(d.order() == 2  || (d.order() == 3 &&
                                 (d.dim(2) == 1 || d.dim(2) == 3)))) {
      // this is probably not an image, just display info and print matrix
      string type;
      get_matrix_type(ifname->c_str(), type);
      idx<T> m = load_matrix<T>(ifname->c_str());
      cout << "Matrix " << ifname->c_str() << " is of type " << type
	   << " with dimensions " << d << " (min " << idx_min(m)
	   << ", max " << idx_max(m) << ", mean " << idx_mean(m)
	   << "):" << endl;
      m.print();
      if (has_multiple_matrices(ifname->c_str())) {
	midx<T> ms = load_matrices<T>(ifname->c_str(), true);
	// saving sub-matrices
	if (save_individually) {
	  cout << "Saving each sub-matrix of " << *ifname << " individually..."
	       << endl;
	  save_matrices_individually(ms, *ifname, true);
	}
	// printing sub-matrices
	cout << "This file contains " << m << " matrices: ";
	if (ms.order() == 1) {
	  for (intg i = 0; i < ms.dim(0); ++i) {
	    idx<T> tmp = ms.mget(i);
	    cout << tmp.info() << " ";
	  }
	} else if (ms.order() == 2) {
	  for (intg i = 0; i < ms.dim(0); ++i) {
	    for (intg j = 0; j < ms.dim(1); ++j) {
	      idx<T> tmp = ms.mget(i, j);
	      cout << tmp.info() << " ";
	    }
	    cout << endl;
	  }
	}
	cout << endl;
      } else
	cout << "This is a single-matrix file." << endl;
      return 0;
    }
  }
  // image mode
  int loaded = 0;
  static idx<T> mat;
  uint h = 0, w = 0, rowh = 0, maxh = 0;
  list<string>::iterator fname = ifname;
#ifdef __GUI__
  disable_window_updates();
  clear_window();
  if (show_filename) {
    gui << at(h, w) << black_on_white() << ifname->c_str();
    h += 16;
  }
#endif
  maxh = h;
  for (uint i = 0; i < nh; ++i) {
    rowh = maxh;
    for (uint j = 0; j < nw; ++j) {
      if (fname == mats->end())
	fname = mats->begin();
      try {
	//      if (load)
	mat = load_image<T>(*fname);
	if (print)
	  cout << *fname << ": " << mat << endl << mat.str() << endl;
	// show only some channels
	if (chans >= 0)
	  mat = mat.select(2, chans);
	loaded++;
	maxh = (std::max)(maxh, (uint) (rowh + mat.dim(0)));
	T min = 0, max = 0;
#ifdef __GUI__
	if (autorange || signd) {
	  if (autorange) {
	    min = idx_min(mat);
	    max = idx_max(mat);
	  } else if (signd) {
	    T matmin = idx_min(mat);
	    if ((double)matmin < 0) {
	      min = -1;
	      max = -1;
	    }
	  }
	  draw_matrix(mat, rowh, w, zoom, zoom, min, max);
	} else
	  draw_matrix(mat, rowh, w, zoom, zoom, (T) range[0], (T) range[1]);
#endif
	w += mat.dim(1) + 1;
      } catch(string &err) {
	ERROR_MSG(err.c_str());
      }
      fname++;
      if (fname == ifname)
	break ;
    }
    if (fname == ifname)
      break ;
    maxh++;
    w = 0;
  }
#ifdef __GUI__
  // info
  if (show_info) {
    set_text_colors(0, 0, 0, 255, 255, 255, 255, 200);
    gui << mat;
    gui << at(15, 0) << *fname;
    gui << at(29, 0) << "min: " << idx_min(mat) << " max: " << idx_max(mat);
  }
  // help
  if (show_help) {
    h = 0;
    w = 0;
    uint hstep = 14;
    set_text_colors(0, 0, 255, 255, 255, 255, 255, 200);
    gui << at(h, w) << "Controls:"; h += hstep;
    set_text_colors(0, 0, 0, 255, 255, 255, 255, 200);
    gui << at(h, w) << "Right/Space: next image"; h += hstep;
    gui << at(h, w) << "Left/Backspace: previous image"; h += hstep;
    gui << at(h, w) << "i: image info"; h += hstep;
    gui << at(h, w) << "a: auto-range (use min and max as range)"; h += hstep;
    gui << at(h, w) << "x/z: show more/less images on width axis"; h += hstep;
    gui << at(h, w) << "y/t: show more/less images on height axis"; h += hstep;
    gui << at(h, w) << "0,1,2: show channel 0, 1 or 2 only"; h += hstep;
    gui << at(h, w) << "9: show alls channels"; h += hstep;
    gui << at(h, w) << "h: help";
  }
  // update title
  string title;
  title << "matshow: " << ebl::basename(ifname->c_str());
  set_window_title(title.c_str());
  enable_window_updates();
#endif
  return loaded;
}
Пример #13
0
  // convert the input image <src> into the <channels_mode> format.
  void convert_image(idx<float> &src, idx<float> &dst, const int channels_mode,
		     unsigned int fkernel_size) {
    idxdim d = idxdim(src), d2 = idxdim(src);
    d2.setdim(2, dst.dim(2));
    idx<float> in, out, src2(d), tmp(d2), tmp2;
    if (fkernel_size > 0) {
      src2 = src.narrow(0, src.dim(0) - fkernel_size + 1,
			floor(fkernel_size / 2));
      src2 = src2.narrow(1, src.dim(1) - fkernel_size + 1,
			 floor(fkernel_size / 2));
      tmp2 = tmp.narrow(0, tmp.dim(0) - fkernel_size + 1,
			floor(fkernel_size / 2));
      tmp2 = tmp2.narrow(1, tmp.dim(1) - fkernel_size + 1,
			 floor(fkernel_size / 2));
    }
    idx_clear(dst);
    // narrow dst to fit src if smaller
    if (src2.dim(0) < dst.dim(0))
      dst = dst.narrow(0, src2.dim(0), floor((dst.dim(0) - src2.dim(0)) / 2));
    if (src2.dim(1) < dst.dim(1))
      dst = dst.narrow(1, src2.dim(1), floor((dst.dim(1) - src2.dim(1)) / 2));
    // converting
    switch (channels_mode) {
    case 0: // RGB
      idx_copy(src2, dst);
      image_global_normalization(dst);
      break ;
    case 1: // YUV
      rgb_to_yuv(src, tmp);
      in = tmp.select(2, 0);
      d = idxdim(in);
      out = idx<float>(d);      
      image_mexican_filter(in, out, 6, fkernel_size);
      idx_copy(out, in);
      idx_copy(tmp2, dst);
      image_global_normalization(dst);      
      break ;
    case 3: { // Y only 
      rgb_to_y(src, tmp);
      in = tmp.select(2, 0);
      d = idxdim(in);
      out = idx<float>(d);      
      image_global_normalization(in);
      image_local_normalization(in, out, fkernel_size);      
      idx_copy(out, in);
      idx_copy(tmp2, dst);
    }
      break ;
    case 4: // YH3
      rgb_to_yh3(src, tmp);
      in = tmp.select(2, 0);
      d = idxdim(in);
      out = idx<float>(d);      
      image_mexican_filter(in, out, 6, fkernel_size);
      image_global_normalization(out);
      idx_copy(out, in);
      idx_copy(tmp2, dst);
      break ;
    case 5: // VpH2SV
      rgb_to_vph2sv(src, tmp, 6, fkernel_size);
      idx_copy(tmp2, dst);
      break ;
    default:
      cerr << "unknown channel mode: " << channels_mode << endl;
      eblerror("unknown channel mode");
    }
  }
Пример #14
0
  //! Recursively goes through dir, looking for files matching extension ext.
  void process_dir(const char *dir, const char *ext, const char* leftp, 
		   const char *rightp, unsigned int width,
		   unsigned int fwidth, idx<float> &images,
		   idx<int> &labels, int label, bool silent, bool display,
		   bool *binocular, 
		   const int channels_mode, const int channels_size,
		   idx<ubyte> &classes, unsigned int &counter,
		   idx<unsigned int> &counters_used, idx<int> &ds_assignment,
		   unsigned int fkernel_size, int deformations,
		   idx<int> &deformid, int &ndefid,
		   idx<ubyte> &ds_names) {
    regex eExt(ext);
    string el(".*");
    idx<float> limg(1, 1, 1);
    idx<float> rimg(1, 1, 1);
    idx<float> tmp, left_images;
    idx<int> current_labels;
    idx<int> current_deformid;
    idx<float> tmp2;
    int current_ds;
    unsigned int current_used;
    if (leftp) {
      el += leftp;
      el += ".*";
    }
    regex eLeft(el);
    cmatch what;
    path p(dir);
    if (!exists(p))
      return ;
    directory_iterator end_itr; // default construction yields past-the-end
    for (directory_iterator itr(p); itr != end_itr; ++itr) {
      if (is_directory(itr->status())) {
	process_dir(itr->path().string().c_str(), ext, leftp, rightp, width,
		    fwidth, images, labels, label, silent, display, binocular,
		    channels_mode, channels_size, classes, counter,
		    counters_used, ds_assignment, fkernel_size, deformations,
		    deformid, ndefid, ds_names);
      } else if (regex_match(itr->leaf().c_str(), what, eExt)) {
	if (regex_match(itr->leaf().c_str(), what, eLeft)) {
	  current_ds = ds_assignment.get(counter);
	  if (current_ds != -1) {
	    current_used = counters_used.get(current_ds);
	    // found left image
	    // increase example number
	    labels.set(label, current_ds, counters_used.get(current_ds));
	    // check for right image
	    if (rightp != NULL) {
	      regex reg(leftp);
	      string rp(rightp);
	      string s = regex_replace(itr->leaf(), reg, rp);
	      string sfull = itr->path().branch_path().string();
	      sfull += "/";
	      sfull += s;
	      path r(sfull);
	      if (exists(r)) {
		// found right image
		*binocular = true;
		if (image_read_rgbx(r.string().c_str(), rimg)) {
		  // resize stereo dimension to twice the channels_size
		  if (images.dim(3) == channels_size)
		    images.resize(images.dim(0), images.dim(1), 
				  images.dim(2), images.dim(3) * 2);
		  // take the right most square of the image
		  if (rimg.dim(0) <= rimg.dim(1))
		    rimg = rimg.narrow(1, rimg.dim(0),
				       rimg.dim(1) - rimg.dim(0));
		  else
		    rimg = rimg.narrow(0, rimg.dim(1),
				       rimg.dim(0) - rimg.dim(1));
		  // resize image to target width
		  rimg = image_resize(rimg, width, width, 1);
		  tmp = images[current_ds];
		  tmp = tmp[counters_used.get(current_ds)];
		  tmp = tmp.narrow(2, channels_size, channels_size);
		  // finally copy right images to idx
		  convert_image(rimg, tmp, channels_mode, fkernel_size);
		}
		if (!silent)
		  cout << "Processing (right): " << sfull << endl;
	      }
	    }
	    if (!silent) {
	      cout << counter << "/" << ds_assignment.dim(0) << ": ";
	      cout << itr->path().string().c_str() << endl;
	    }
	    // process left image
	    if (image_read_rgbx(itr->path().string().c_str(), limg)) {
	      // take the left most square of the image
	      int h = limg.dim(0), w = limg.dim(1), newh, neww;
	      if (h > w) {
		newh = width;
		neww = (newh / (float) h) * w;
	      } else {
		neww = width;
		newh = (neww / (float) w) * h;
	      }
	      // resize image to target width
	      limg = image_resize(limg, neww, newh, 1);
	      left_images = images[current_ds];
	      left_images = left_images.narrow(3, channels_size, 0);
	      left_images = left_images.narrow(0, 1 + (deformations<=0?0:
						       deformations),
					       current_used);
	      current_labels = labels[current_ds];
	      current_labels = current_labels.narrow(0, current_labels.dim(0) -
						     current_used,current_used);
	      current_deformid = deformid[current_ds];
	      current_deformid = current_deformid.
		narrow(0, current_deformid.dim(0) - current_used, current_used);
	      tmp = left_images[0];
	      // convert and copy image into images buffer
	      convert_image(limg, tmp, channels_mode, fkernel_size);
	      // if adding to dataset 0, add deformations if deformations > 0
	      if ((current_ds == 0) && (deformations > 0)) {
		add_deformations(left_images, current_labels, label,
				 deformations, current_deformid, ndefid);
		counters_used.set(counters_used.get(current_ds) + deformations,
				  current_ds);
	      } else // no deformations
		left_images = left_images.narrow(0, 1, 0);
	      // display
	      if (display) display_images(classes, label, limg, left_images,
					  channels_mode, ds_names, current_ds);
	    // increment counter for dataset current_ds
	    counters_used.set(counters_used.get(current_ds) + 1, current_ds);
	    }
	  }
	  counter++;
	}
      }
    }
  }