Cinder

  • Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

include/opencv2/flann/sampling.h

Go to the documentation of this file.
00001 /***********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  * Copyright 2008-2009  Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
00005  * Copyright 2008-2009  David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions
00009  * are met:
00010  *
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  *************************************************************************/
00028 
00029 
00030 #ifndef _OPENCV_SAMPLING_H_
00031 #define _OPENCV_SAMPLING_H_
00032 
00033 
00034 #include "opencv2/flann/matrix.h"
00035 #include "opencv2/flann/random.h"
00036 
00037 
00038 namespace cvflann
00039 {
00040 
00041 template<typename T>
00042 Matrix<T> random_sample(Matrix<T>& srcMatrix, long size, bool remove = false)
00043 {
00044     UniqueRandom rand((int)srcMatrix.rows);
00045     Matrix<T> newSet(new T[size * srcMatrix.cols], size, (long)srcMatrix.cols);
00046 
00047     T *src,*dest;
00048     for (long i=0;i<size;++i) {
00049         long r = rand.next();
00050         dest = newSet[i];
00051         src = srcMatrix[r];
00052         for (size_t j=0;j<srcMatrix.cols;++j) {
00053             dest[j] = src[j];
00054         }
00055         if (remove) {
00056             dest = srcMatrix[srcMatrix.rows-i-1];
00057             src = srcMatrix[r];
00058             for (size_t j=0;j<srcMatrix.cols;++j) {
00059                 std::swap(*src,*dest);
00060                 src++;
00061                 dest++;
00062             }
00063         }
00064     }
00065 
00066     if (remove) {
00067         srcMatrix.rows -= size;
00068     }
00069 
00070     return newSet;
00071 }
00072 
00073 template<typename T>
00074 Matrix<T> random_sample(const Matrix<T>& srcMatrix, size_t size)
00075 {
00076     UniqueRandom rand((int)srcMatrix.rows);
00077     Matrix<T> newSet(new T[size * srcMatrix.cols], (long)size, (long)srcMatrix.cols);
00078 
00079     T *src,*dest;
00080     for (size_t i=0;i<size;++i) {
00081         long r = rand.next();
00082         dest = newSet[i];
00083         src = srcMatrix[r];
00084         for (size_t j=0;j<srcMatrix.cols;++j) {
00085             dest[j] = src[j];
00086         }
00087     }
00088 
00089     return newSet;
00090 }
00091 
00092 } // namespace cvflann
00093 
00094 #endif /* _OPENCV_SAMPLING_H_ */