VisionUtils.h
Go to the documentation of this file.
00001 /****************************************************************
00002 *
00003 * Copyright (c) 2010
00004 *
00005 * Fraunhofer Institute for Manufacturing Engineering
00006 * and Automation (IPA)
00007 *
00008 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00009 *
00010 * Project name: care-o-bot
00011 * ROS stack name: cob_perception_common
00012 * ROS package name: cob_vision_utils
00013 * Description: Utility functions for Fraunhofer IPA vision library.
00014 *
00015 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00016 *
00017 * Author: Jan Fischer, email:jan.fischer@ipa.fhg.de
00018 * Supervised by: Jan Fischer, email:jan.fischer@ipa.fhg.de
00019 *
00020 * Date of creation: July 2008
00021 * ToDo:
00022 *
00023 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00024 *
00025 * Redistribution and use in source and binary forms, with or without
00026 * modification, are permitted provided that the following conditions are met:
00027 *
00028 * * Redistributions of source code must retain the above copyright
00029 * notice, this list of conditions and the following disclaimer.
00030 * * Redistributions in binary form must reproduce the above copyright
00031 * notice, this list of conditions and the following disclaimer in the
00032 * documentation and/or other materials provided with the distribution.
00033 * * Neither the name of the Fraunhofer Institute for Manufacturing
00034 * Engineering and Automation (IPA) nor the names of its
00035 * contributors may be used to endorse or promote products derived from
00036 * this software without specific prior written permission.
00037 *
00038 * This program is free software: you can redistribute it and/or modify
00039 * it under the terms of the GNU Lesser General Public License LGPL as
00040 * published by the Free Software Foundation, either version 3 of the
00041 * License, or (at your option) any later version.
00042 *
00043 * This program is distributed in the hope that it will be useful,
00044 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00045 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00046 * GNU Lesser General Public License LGPL for more details.
00047 *
00048 * You should have received a copy of the GNU Lesser General Public
00049 * License LGPL along with this program.
00050 * If not, see <http://www.gnu.org/licenses/>.
00051 *
00052 ****************************************************************/
00053  
00058 
00059 #ifndef __IPA_VISIONUTILS_H__
00060 #define __IPA_VISIONUTILS_H__
00061 
00062 #ifdef __LINUX__
00063         #include "cob_vision_utils/GlobalDefines.h"
00064 #else
00065         #include "cob_perception_common/cob_vision_utils/common/include/cob_vision_utils/GlobalDefines.h"
00066 #endif
00067 
00068 #include <opencv/cv.h>
00069 #include <iostream>
00070 
00071 namespace ipa_Utils {
00072 
00073 
00077 cv::Mat vstack(const std::vector<cv::Mat> &mats);
00078 
00080 void InitUndistortMap( const cv::Mat& A, const cv::Mat& dist_coeffs,
00081                     cv::Mat& mapxarr, cv::Mat& mapyarr );
00082 
00090 unsigned long ConvertToShowImage(const cv::Mat& source, cv::Mat& dest, int channel = 1, double min = -1, double max = -1);
00091 
00104 unsigned long MaskImage(const cv::Mat& source, cv::Mat& dest, const cv::Mat& mask, cv::Mat& destMask, float minMaskThresh = 0,
00105                                                 float maxMaskTresh = 20000, int sourceChannel = 1, double sourceMin = -1, double sourceMax = -1);
00106 
00113 unsigned long EvaluatePolynomial(double x, int degree, double* coefficients, double* y);
00114 
00122 unsigned long FilterByAmplitude(cv::Mat& xyzImage, const cv::Mat& greyImage, cv::Mat* mask, cv::Mat* maskColor, float minMaskThresh, float maxMaskThresh);
00123 
00132 unsigned long FilterTearOffEdges(cv::Mat& xyzImage, cv::Mat* mask, float piHalfFraction = 6);
00133 
00135 unsigned long FilterSpeckles( cv::Mat& img, int maxSpeckleSize, double _maxDiff, cv::Mat& _buf );
00136 
00142 cv::Vec3b GrayColorMap(double value, double min, double max);
00143 
00147 cv::Mat GetColorcoded(const cv::Mat& img_32F);
00148 
00154 cv::Mat GetColorcoded(const cv::Mat& img_32F, double min, double max);
00155 
00160 unsigned long SaveMat(cv::Mat& mat, std::string filename, int type=CV_32F);
00161 
00166 unsigned long LoadMat(cv::Mat& mat, std::string filename, int type=CV_32F);
00167 
00168 
00173 template <typename Distance>
00174 void ClusteringKMeanspp(int k, cv::Mat& dataset, int* indices, int indices_length, int* centers, int& centers_length, int numLocalTries=1)
00175 {
00176         typedef typename Distance::ElementType ElementType;
00177     typedef typename Distance::ResultType DistanceType;
00178 
00179         Distance distance = Distance();
00180 
00181     int n = indices_length;
00182 
00183     double currentPot = 0;
00184     DistanceType* closestDistSq = new DistanceType[n];
00185 
00186     // Choose one random center and set the closestDistSq values
00187     int index = cvflann::rand_int(n);
00188     assert(index >=0 && index < n);
00189     centers[0] = indices[index];
00190 
00191     for (int i = 0; i < n; i++) {
00192         closestDistSq[i] = distance(dataset.ptr(indices[i]), 
00193                         dataset.ptr(indices[index]), dataset.cols);
00194         currentPot += closestDistSq[i];
00195     }
00196 
00197     // Choose each center
00198     int centerCount;
00199     for (centerCount = 1; centerCount < k; centerCount++) {
00200 
00201         // Repeat several trials
00202         double bestNewPot = -1;
00203         int bestNewIndex = 0;
00204         for (int localTrial = 0; localTrial < numLocalTries; localTrial++) {
00205 
00206             // Choose our center - have to be slightly careful to return a valid answer even accounting
00207             // for possible rounding errors
00208             double randVal = cvflann::rand_double(currentPot);
00209             for (index = 0; index < n-1; index++) {
00210                 if (randVal <= closestDistSq[index]) break;
00211                 else randVal -= closestDistSq[index];
00212             }
00213 
00214             // Compute the new potential
00215             double newPot = 0;
00216             for (int i = 0; i < n; i++) 
00217                                 newPot += std::min( distance(dataset.ptr(indices[i]), 
00218                                         dataset.ptr(indices[index]), dataset.cols), closestDistSq[i] );
00219 
00220             // Store the best result
00221             if ((bestNewPot < 0)||(newPot < bestNewPot)) {
00222                 bestNewPot = newPot;
00223                 bestNewIndex = index;
00224             }
00225         }
00226 
00227         // Add the appropriate center
00228         centers[centerCount] = indices[bestNewIndex];
00229         currentPot = bestNewPot;
00230         for (int i = 0; i < n; i++) 
00231                         closestDistSq[i] = std::min( distance(dataset.ptr(indices[i]),
00232                                 dataset.ptr(indices[bestNewIndex]), dataset.cols), closestDistSq[i] );
00233     }
00234 
00235     centers_length = centerCount;
00236 
00237     delete[] closestDistSq;
00238 }
00239 
00240 // Generator that yields an increasing sequence of integers
00241 class UniqueNumber {
00242 public:
00243   int current;
00244   UniqueNumber();
00245   int operator()();
00246 };
00247 
00248 // Returns random subset of input vector. Will not create duplicates.
00249 // @param v Input vector
00250 // @param n Number of items to return
00251 // @return Vector of same type as input vector (length n)
00252 template<class T> std::vector<T> takeRandomN(const std::vector<T> &v, int n)
00253 {
00254     int current = 0;
00255     std::vector<int> indices(v.size());
00256     std::generate(indices.begin(), indices.end(), UniqueNumber());
00257     std::random_shuffle(indices.begin(), indices.end());
00258     
00259     std::vector<T> ret;
00260     for (int i = 0; i < n; i++)
00261     {
00262         ret.push_back(v[indices[i]]);
00263     }
00264 
00265     return ret;
00266 }
00267 
00268 } // end namespace __IPA_VISIONUTILS_H__
00269 
00270 
00271 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


cob_vision_utils
Author(s): Jan Fischer
autogenerated on Fri Mar 8 2013 15:03:41