Example #1
0
static void RandInts(
    vec_int& ints,    // out: scrambled integers in range 0 ... NSIZE(ints)-1
    int      seed)    // in:  random seed
{
    const int n = NSIZE(ints);
    CV_Assert(n > 0);
    if (n > RAND_MAX)
        Err("vector size %d is too big (max allowed is %d)", n, RAND_MAX);
    CV_Assert(seed != 0);
    if (seed == 1)       // 1 has a special meaning which we don't want
        seed = int(1e6); // arb

    int i;
    for (i = 0; i < n; i++)
        ints[i] = i;

    srand(seed);

    // We use our own random shuffle here because different compilers
    // give different results which messes up regression testing.
    // (I think only Visual C 6.0 is incompatible with everyone else?)
    //
    // Following code is equivalent to
    //    random_shuffle(ints.begin(), ints.end(),
    //       pointer_to_unary_function<int,int>(RandInt));

    vec_int::iterator it = ints.begin();
    for (i = 2; ++it != ints.end(); i++)
        iter_swap(it, ints.begin() + rand() % n);
}
Example #2
0
static inline void InitIndices(
    vec_int&    row_indices,    // out
    vec_double& row_fracs,      // out
    vec_int&    col_indices,    // out
    vec_double& col_fracs,      // out
    vec_double& pixelweights,   // out
    const int   patchwidth)     // in: in pixels
{
    CV_Assert(patchwidth % 2 == 1); // patchwidth must be odd in this implementation

    const int npix = SQ(patchwidth); // number of pixels in image patch

    row_indices.resize(npix);
    row_fracs.resize(npix);
    col_indices.resize(npix);
    col_fracs.resize(npix);
    pixelweights.resize(npix);

    const int halfpatchwidth = (patchwidth-1) / 2;

    const double grid_rows_per_img_row = GRIDHEIGHT / (patchwidth-1.);
    const double row_offset = GRIDHEIGHT / 2. - .5; // see header comment

    const double grid_cols_per_img_col = GRIDWIDTH / (patchwidth-1.);
    const double col_offset = GRIDWIDTH / 2. - .5;

    // downweight at border of patch is exp(-1 / (2 * WINDOW_SIGMA))
    const double weight = -1 / (WINDOW_SIGMA * GRIDHEIGHT * GRIDWIDTH );

    int ipix = 0;

    for (double patchrow = -halfpatchwidth; patchrow <= halfpatchwidth; patchrow++)
    {
        const double signed_row = patchrow * grid_rows_per_img_row;
        const double row        = signed_row + row_offset;
        const int irow          = int(floor(row));
        const double row_frac   = row - irow;

        CV_DbgAssert(row >= -.5 && row <= GRIDHEIGHT - .5); // same applies to col below

        for (double patchcol = -halfpatchwidth; patchcol <= halfpatchwidth; patchcol++)
        {
            row_indices[ipix] = irow;
            row_fracs[ipix]   = row_frac;

            const double signed_col = patchcol * grid_cols_per_img_col;
            const double col        = signed_col + col_offset;
            const int icol          = int(floor(col));

            col_indices[ipix] = icol;
            col_fracs[ipix]   = col - icol;

            pixelweights[ipix] = // TODO this weights col and row offsets equally
                exp(weight * (SQ(signed_row) + SQ(signed_col)));

            ipix++;
        }
    }
}
Example #3
0
void monomial_mul(const vec_int &A, const vec_int &B, vec_int &C)
{
    auto a = A.begin();
    auto b = B.begin();
    auto c = C.begin();
    for (; a != A.end(); ++a, ++b, ++c) {
        *c = *a + *b;
    }
}
Example #4
0
    vector<vector<int> > fourSum(vector<int> &num, int target) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
      if (num.size() < 4)
	return result;
      //O(n ^ 2)
      v.clear();
      result.clear();
      for (size_t i = 0; i < num.size(); ++i)
      {
	for (size_t j = i + 1; j < num.size(); ++j)
	{
	  TwoSum t;
	  t.value = num[i] + num[j];
	  t.pos1 = i;
	  t.pos2 = j;
	  v.push_back(t);
	}
      }
      //O(n ^ 2 log (n ^ 2))
      sort(v.begin(),  v.end(), my_compare);
      //O(n)
      cut_the_tail(target);
      for (size_t i = 0; i < v.size(); ++i)
      {
	//O(log (n ^ 2))
	size_t begin = my_binary_search_begin(target - v[i].value, i + 1, v.size() - 1);   //reduce the running time
	size_t end = my_binary_search_end(target - v[i].value, i + 1, v.size() - 1);     //reduce the running time
	//O(n ^ 2)
	for (size_t j = begin; j <= end; ++j)
	{
	  
	  if (v[i].pos1 == v[j].pos1 || v[i].pos2 == v[j].pos1
	      || v[i].pos1 == v[j].pos2 || v[i].pos2 == v[j].pos2)    //v[m] and v[n] share a same element
	    continue;
	  else
	  {
	    vector<int> temp;
	    temp.push_back(num[v[i].pos1]);
	    temp.push_back(num[v[i].pos2]);
	    temp.push_back(num[v[j].pos1]);
	    temp.push_back(num[v[j].pos2]);
	    sort(temp.begin(), temp.end());
	    if (find(result.begin(), result.end(), temp) == result.end())
	      result.push_back(temp);
	  }
	}
      }
      return result;
    }
Example #5
0
static void NbrUsedPointsVec(   // nbr of used points in each shape
    vec_int&         nused_vec, // out: vec [nshapes] of int
    const vec_Shape& shapes)    // in
{
    nused_vec.resize(NSIZE(shapes));
    for (int ishape = 0; ishape < NSIZE(shapes); ishape++)
        nused_vec[ishape] = NbrUsedPoints(shapes[ishape]);
}
Example #6
0
StasmMat FindMatInFile (const char sFile[],  // in
                   char *psImageDirs[], // out: directories in shape file if any, optional
                   const char sRegex[], // in: can be null
                   unsigned Mask0,      // in
                   unsigned Mask1)      // in
{
static char       sFile1[SLEN]; // the mat file name
static char       sImageDirs[SLEN];
static vec_Mat    MatV;         // the matrices read in from the file
static vec_string Tags;         // the tags (i.e. string before each mat in the file)
static vec_int    TagInts;      // hex number at start of each of above tags

// iMat is static so we start where we finished last time, which means that
// searches are fast if this function is invoked for matrices in order
// It also means with multiple matches to sRegex, succesive matching shapes
// are returned each time this function is called.

static unsigned iMat;

if (strcmp(sFile, sFile1))  // first time (for this file)?
    {
    // initialize the static variables

    strcpy(sFile1, sFile);
    iMat = 0;
    // read all shapes, we will filter the one we want later
    ReadShapeFile(MatV, Tags, sImageDirs, NULL, 0, 0, sFile);
    TagInts.resize(MatV.size());
    for (size_t i = 0; i < MatV.size(); i++)
        {
        char const *s = Tags[i].c_str();
        unsigned n;
        if (1 != sscanf(s, "%x", &n))
            Err("can't convert tag %s in %s to a hex number", s, sFile);
        TagInts[i] = n;
        }
    }
// regcomp flags: egrep style expression (supports |),
// ignore case, use simple failure reporting in regexec

// regex_t CompiledRegex;
// if (0 != regcomp(&CompiledRegex, sRegex, REG_EXTENDED|REG_ICASE|REG_NOSUB))
    // Err("invalid regular expression %s", sRegex);

size_t i;
for (i = 0; i < MatV.size(); i++)
    {
    if (fMatchAttr(TagInts[iMat], Mask0, Mask1))
	    // && // filter first on attributes
        // fRegexMatch(CompiledRegex, sGetBasenameFromTag(Tags[iMat])))
        {
        break;                  // found
        }
    iMat++;
    if (iMat >= MatV.size())
        iMat = 0;               // wrap
    }
// regfree(&CompiledRegex);

if (psImageDirs)
    *psImageDirs = sImageDirs;

StasmMat m;                          // returned matrix
if (i < MatV.size())            // found?
    m = MatV[iMat];

return m;
}