18 #ifndef __IPA_VISIONUTILS_H__ 19 #define __IPA_VISIONUTILS_H__ 23 #include <opencv2/core/core.hpp> 32 cv::Mat
vstack(
const std::vector<cv::Mat> &mats);
41 unsigned long ConvertToShowImage(
const cv::Mat& source, cv::Mat& dest,
int channel = 1,
double min = -1,
double max = -1);
55 unsigned long MaskImage(
const cv::Mat& source, cv::Mat& dest,
const cv::Mat& mask, cv::Mat& destMask,
float minMaskThresh = 0,
56 float maxMaskTresh = 20000,
int sourceChannel = 1,
double sourceMin = -1,
double sourceMax = -1);
64 unsigned long EvaluatePolynomial(
double x,
int degree,
double* coefficients,
double* y);
73 unsigned long FilterByAmplitude(cv::Mat& xyzImage,
const cv::Mat& greyImage, cv::Mat* mask, cv::Mat* maskColor,
float minMaskThresh,
float maxMaskThresh);
83 unsigned long FilterTearOffEdges(cv::Mat& xyzImage, cv::Mat* mask,
float piHalfFraction = 6);
86 unsigned long FilterSpeckles( cv::Mat& img,
int maxSpeckleSize,
double _maxDiff, cv::Mat& _buf );
93 cv::Vec3b
GrayColorMap(
double value,
double min,
double max);
99 unsigned long SaveMat(cv::Mat& mat, std::string filename,
int type=CV_32F);
105 unsigned long LoadMat(cv::Mat& mat, std::string filename,
int type=CV_32F);
112 template <
typename Distance>
113 void ClusteringKMeanspp(
int k, cv::Mat& dataset,
int* indices,
int indices_length,
int* centers,
int& centers_length,
int numLocalTries=1)
115 typedef typename Distance::ElementType ElementType;
116 typedef typename Distance::ResultType DistanceType;
118 Distance distance = Distance();
120 int n = indices_length;
122 double currentPot = 0;
123 DistanceType* closestDistSq =
new DistanceType[n];
127 int index = rand() % n;
128 assert(index >=0 && index < n);
129 centers[0] = indices[index];
131 for (
int i = 0; i < n; i++) {
132 closestDistSq[i] = distance(dataset.ptr(indices[i]),
133 dataset.ptr(indices[index]), dataset.cols);
134 currentPot += closestDistSq[i];
139 for (centerCount = 1; centerCount < k; centerCount++) {
142 double bestNewPot = -1;
143 int bestNewIndex = 0;
144 for (
int localTrial = 0; localTrial < numLocalTries; localTrial++) {
149 double randVal = (double)rand() / RAND_MAX;
151 double fMax = currentPot;
152 randVal = fMin + randVal * (fMax - fMin);
153 for (index = 0; index < n-1; index++) {
154 if (randVal <= closestDistSq[index])
break;
155 else randVal -= closestDistSq[index];
160 for (
int i = 0; i < n; i++)
161 newPot += std::min( distance(dataset.ptr(indices[i]),
162 dataset.ptr(indices[index]), dataset.cols), closestDistSq[i] );
165 if ((bestNewPot < 0)||(newPot < bestNewPot)) {
167 bestNewIndex = index;
172 centers[centerCount] = indices[bestNewIndex];
173 currentPot = bestNewPot;
174 for (
int i = 0; i < n; i++)
175 closestDistSq[i] = std::min( distance(dataset.ptr(indices[i]),
176 dataset.ptr(indices[bestNewIndex]), dataset.cols), closestDistSq[i] );
179 centers_length = centerCount;
181 delete[] closestDistSq;
196 template<
class T> std::vector<T>
takeRandomN(
const std::vector<T> &v,
int n)
199 std::vector<int> indices(v.size());
200 std::generate(indices.begin(), indices.end(),
UniqueNumber());
201 std::random_shuffle(indices.begin(), indices.end());
204 for (
int i = 0; i < n; i++)
206 ret.push_back(v[indices[i]]);
cv::Mat vstack(const std::vector< cv::Mat > &mats)
unsigned long ConvertToShowImage(const cv::Mat &source, cv::Mat &dest, int channel=1, double min=-1, double max=-1)
unsigned long SaveMat(cv::Mat &mat, std::string filename, int type=CV_32F)
unsigned long FilterSpeckles(cv::Mat &img, int maxSpeckleSize, double _maxDiff, cv::Mat &_buf)
Description.
cv::Vec3b GrayColorMap(double value, double min, double max)
unsigned long LoadMat(cv::Mat &mat, std::string filename, int type=CV_32F)
unsigned long MaskImage(const cv::Mat &source, cv::Mat &dest, const cv::Mat &mask, cv::Mat &destMask, float minMaskThresh=0, float maxMaskTresh=20000, int sourceChannel=1, double sourceMin=-1, double sourceMax=-1)
unsigned long EvaluatePolynomial(double x, int degree, double *coefficients, double *y)
std::vector< T > takeRandomN(const std::vector< T > &v, int n)
unsigned long FilterTearOffEdges(cv::Mat &xyzImage, cv::Mat *mask, float piHalfFraction=6)
unsigned long FilterByAmplitude(cv::Mat &xyzImage, const cv::Mat &greyImage, cv::Mat *mask, cv::Mat *maskColor, float minMaskThresh, float maxMaskThresh)
void ClusteringKMeanspp(int k, cv::Mat &dataset, int *indices, int indices_length, int *centers, int ¢ers_length, int numLocalTries=1)