示例#1
0
文件: dataset.cpp 项目: 2php/eblearn
 void jitter::set(const idx<t_jitter> &j) {
   s = j.get(0);
   h = (int) j.get(1);
   w = (int) j.get(2);
   r = j.get(3);
   idx_copy(j, jitts);
 }
示例#2
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;
  }
示例#3
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++;
	}
      }
    }
  }