Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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 }
00093
00094 #endif