Renesas / opencv-lib

Dependents:   RZ_A2M_Mbed_samples

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sampling.h Source File

sampling.h

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_FLANN_SAMPLING_H_
00031 #define OPENCV_FLANN_SAMPLING_H_
00032 
00033 #include "matrix.h"
00034 #include "random.h"
00035 
00036 namespace cvflann
00037 {
00038 
00039 template<typename T>
00040 Matrix<T> random_sample(Matrix<T>& srcMatrix, long size, bool remove = false)
00041 {
00042     Matrix<T> newSet(new T[size * srcMatrix.cols], size,srcMatrix.cols);
00043 
00044     T* src,* dest;
00045     for (long i=0; i<size; ++i) {
00046         long r = rand_int((int)(srcMatrix.rows-i));
00047         dest = newSet[i];
00048         src = srcMatrix[r];
00049         std::copy(src, src+srcMatrix.cols, dest);
00050         if (remove) {
00051             src = srcMatrix[srcMatrix.rows-i-1];
00052             dest = srcMatrix[r];
00053             std::copy(src, src+srcMatrix.cols, dest);
00054         }
00055     }
00056     if (remove) {
00057         srcMatrix.rows -= size;
00058     }
00059     return newSet;
00060 }
00061 
00062 template<typename T>
00063 Matrix<T> random_sample(const Matrix<T>& srcMatrix, size_t size)
00064 {
00065     UniqueRandom rand((int)srcMatrix.rows);
00066     Matrix<T> newSet(new T[size * srcMatrix.cols], size,srcMatrix.cols);
00067 
00068     T* src,* dest;
00069     for (size_t i=0; i<size; ++i) {
00070         long r = rand.next();
00071         dest = newSet[i];
00072         src = srcMatrix[r];
00073         std::copy(src, src+srcMatrix.cols, dest);
00074     }
00075     return newSet;
00076 }
00077 
00078 } // namespace
00079 
00080 
00081 #endif /* OPENCV_FLANN_SAMPLING_H_ */