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
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
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
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
00198 int centerCount;
00199 for (centerCount = 1; centerCount < k; centerCount++) {
00200
00201
00202 double bestNewPot = -1;
00203 int bestNewIndex = 0;
00204 for (int localTrial = 0; localTrial < numLocalTries; localTrial++) {
00205
00206
00207
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
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
00221 if ((bestNewPot < 0)||(newPot < bestNewPot)) {
00222 bestNewPot = newPot;
00223 bestNewIndex = index;
00224 }
00225 }
00226
00227
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
00241 class UniqueNumber {
00242 public:
00243 int current;
00244 UniqueNumber();
00245 int operator()();
00246 };
00247
00248
00249
00250
00251
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 }
00269
00270
00271 #endif