VisionUtils.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *   http://www.apache.org/licenses/LICENSE-2.0
00009 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016  
00017  
00018 #ifndef __IPA_VISIONUTILS_H__
00019 #define __IPA_VISIONUTILS_H__
00020 
00021 #ifdef __LINUX__
00022         #include "cob_vision_utils/GlobalDefines.h"
00023 #else
00024         #include "cob_perception_common/cob_vision_utils/common/include/cob_vision_utils/GlobalDefines.h"
00025 #endif
00026 
00027 #include <opencv2/core/core.hpp>
00028 #include <iostream>
00029 
00030 namespace ipa_Utils {
00031 
00032 
00036 cv::Mat vstack(const std::vector<cv::Mat> &mats);
00037 
00039 void InitUndistortMap( const cv::Mat& A, const cv::Mat& dist_coeffs,
00040                                         cv::Mat& mapxarr, cv::Mat& mapyarr );
00041 
00049 unsigned long ConvertToShowImage(const cv::Mat& source, cv::Mat& dest, int channel = 1, double min = -1, double max = -1);
00050 
00063 unsigned long MaskImage(const cv::Mat& source, cv::Mat& dest, const cv::Mat& mask, cv::Mat& destMask, float minMaskThresh = 0,
00064                                                 float maxMaskTresh = 20000, int sourceChannel = 1, double sourceMin = -1, double sourceMax = -1);
00065 
00072 unsigned long EvaluatePolynomial(double x, int degree, double* coefficients, double* y);
00073 
00081 unsigned long FilterByAmplitude(cv::Mat& xyzImage, const cv::Mat& greyImage, cv::Mat* mask, cv::Mat* maskColor, float minMaskThresh, float maxMaskThresh);
00082 
00091 unsigned long FilterTearOffEdges(cv::Mat& xyzImage, cv::Mat* mask, float piHalfFraction = 6);
00092 
00094 unsigned long FilterSpeckles( cv::Mat& img, int maxSpeckleSize, double _maxDiff, cv::Mat& _buf );
00095 
00101 cv::Vec3b GrayColorMap(double value, double min, double max);
00102 
00106 cv::Mat GetColorcoded(const cv::Mat& img_32F);
00107 
00113 cv::Mat GetColorcoded(const cv::Mat& img_32F, double min, double max);
00114 
00119 unsigned long SaveMat(cv::Mat& mat, std::string filename, int type=CV_32F);
00120 
00125 unsigned long LoadMat(cv::Mat& mat, std::string filename, int type=CV_32F);
00126 
00127 
00132 template <typename Distance>
00133 void ClusteringKMeanspp(int k, cv::Mat& dataset, int* indices, int indices_length, int* centers, int& centers_length, int numLocalTries=1)
00134 {
00135         typedef typename Distance::ElementType ElementType;
00136         typedef typename Distance::ResultType DistanceType;
00137 
00138         Distance distance = Distance();
00139 
00140         int n = indices_length;
00141 
00142         double currentPot = 0;
00143         DistanceType* closestDistSq = new DistanceType[n];
00144 
00145         // Choose one random center and set the closestDistSq values
00146         //int index = cv::flann::rand_int(n);
00147         int index = rand() % n;
00148         assert(index >=0 && index < n);
00149         centers[0] = indices[index];
00150 
00151         for (int i = 0; i < n; i++) {
00152                 closestDistSq[i] = distance(dataset.ptr(indices[i]), 
00153                         dataset.ptr(indices[index]), dataset.cols);
00154                 currentPot += closestDistSq[i];
00155         }
00156 
00157         // Choose each center
00158         int centerCount;
00159         for (centerCount = 1; centerCount < k; centerCount++) {
00160 
00161                 // Repeat several trials
00162                 double bestNewPot = -1;
00163                 int bestNewIndex = 0;
00164                 for (int localTrial = 0; localTrial < numLocalTries; localTrial++) {
00165 
00166                         // Choose our center - have to be slightly careful to return a valid answer even accounting
00167                         // for possible rounding errors
00168                         //double randVal = cv::flann::rand_double(currentPot);
00169                         double randVal = (double)rand() / RAND_MAX;
00170                         double fMin = 0;
00171                         double fMax = currentPot;
00172                         randVal = fMin + randVal * (fMax - fMin);
00173                         for (index = 0; index < n-1; index++) {
00174                                 if (randVal <= closestDistSq[index]) break;
00175                                 else randVal -= closestDistSq[index];
00176                         }
00177 
00178                         // Compute the new potential
00179                         double newPot = 0;
00180                         for (int i = 0; i < n; i++) 
00181                                 newPot += std::min( distance(dataset.ptr(indices[i]), 
00182                                         dataset.ptr(indices[index]), dataset.cols), closestDistSq[i] );
00183 
00184                         // Store the best result
00185                         if ((bestNewPot < 0)||(newPot < bestNewPot)) {
00186                                 bestNewPot = newPot;
00187                                 bestNewIndex = index;
00188                         }
00189                 }
00190 
00191                 // Add the appropriate center
00192                 centers[centerCount] = indices[bestNewIndex];
00193                 currentPot = bestNewPot;
00194                 for (int i = 0; i < n; i++) 
00195                         closestDistSq[i] = std::min( distance(dataset.ptr(indices[i]),
00196                                 dataset.ptr(indices[bestNewIndex]), dataset.cols), closestDistSq[i] );
00197         }
00198 
00199         centers_length = centerCount;
00200 
00201         delete[] closestDistSq;
00202 }
00203 
00204 // Generator that yields an increasing sequence of integers
00205 class UniqueNumber {
00206 public:
00207   int current;
00208   UniqueNumber();
00209   int operator()();
00210 };
00211 
00212 // Returns random subset of input vector. Will not create duplicates.
00213 // @param v Input vector
00214 // @param n Number of items to return
00215 // @return Vector of same type as input vector (length n)
00216 template<class T> std::vector<T> takeRandomN(const std::vector<T> &v, int n)
00217 {
00218         int current = 0;
00219         std::vector<int> indices(v.size());
00220         std::generate(indices.begin(), indices.end(), UniqueNumber());
00221         std::random_shuffle(indices.begin(), indices.end());
00222         
00223         std::vector<T> ret;
00224         for (int i = 0; i < n; i++)
00225         {
00226                 ret.push_back(v[indices[i]]);
00227         }
00228 
00229         return ret;
00230 }
00231 
00232 } // end namespace __IPA_VISIONUTILS_H__
00233 
00234 
00235 #endif


cob_vision_utils
Author(s): Jan Fischer
autogenerated on Fri Mar 15 2019 03:10:16