Commit 3021f01b466282570872830eaeedbc392c119b0c
1 parent
9b42f0bf
started writing a new algorithm
Showing
4 changed files
with
94 additions
and
90 deletions
sdk/plugins/algorithms.cpp
| @@ -50,6 +50,7 @@ class AlgorithmsInitializer : public Initializer | @@ -50,6 +50,7 @@ class AlgorithmsInitializer : public Initializer | ||
| 50 | Globals->abbreviations.insert("SmallSIFT", "Open+LimitSize(512)+KeyPointDetector(SIFT)+KeyPointDescriptor(SIFT):KeyPointMatcher(BruteForce)"); | 50 | Globals->abbreviations.insert("SmallSIFT", "Open+LimitSize(512)+KeyPointDetector(SIFT)+KeyPointDescriptor(SIFT):KeyPointMatcher(BruteForce)"); |
| 51 | Globals->abbreviations.insert("SmallSURF", "Open+LimitSize(512)+KeyPointDetector(SURF)+KeyPointDescriptor(SURF):KeyPointMatcher(BruteForce)"); | 51 | Globals->abbreviations.insert("SmallSURF", "Open+LimitSize(512)+KeyPointDetector(SURF)+KeyPointDescriptor(SURF):KeyPointMatcher(BruteForce)"); |
| 52 | Globals->abbreviations.insert("ColorHist", "Open+LimitSize(512)!EnsureChannels(3)+SplitChannels+Hist(256,0,8)+Cat+Normalize(L1):L2"); | 52 | Globals->abbreviations.insert("ColorHist", "Open+LimitSize(512)!EnsureChannels(3)+SplitChannels+Hist(256,0,8)+Cat+Normalize(L1):L2"); |
| 53 | + Globals->abbreviations.insert("IHH", "Open+SplitChannels/(Cvt(Gray)+Gradient+Bin(0,6.283,8,true))+Integral+Merge+IntegralSampler"); | ||
| 53 | 54 | ||
| 54 | // Hash | 55 | // Hash |
| 55 | Globals->abbreviations.insert("FileName", "Name+Identity:Identical"); | 56 | Globals->abbreviations.insert("FileName", "Name+Identity:Identical"); |
sdk/plugins/hist.cpp
| @@ -164,91 +164,6 @@ class IntegralHistTransform : public UntrainableTransform | @@ -164,91 +164,6 @@ class IntegralHistTransform : public UntrainableTransform | ||
| 164 | 164 | ||
| 165 | BR_REGISTER(Transform, IntegralHistTransform) | 165 | BR_REGISTER(Transform, IntegralHistTransform) |
| 166 | 166 | ||
| 167 | -/*! | ||
| 168 | - * \ingroup transforms | ||
| 169 | - * \brief Detects regions of low variance | ||
| 170 | - * \author Josh Klontz \cite jklontz | ||
| 171 | - */ | ||
| 172 | -class VarianceChangeDetectorTransform : public UntrainableTransform | ||
| 173 | -{ | ||
| 174 | - Q_OBJECT | ||
| 175 | - Q_PROPERTY(int bins READ get_bins WRITE set_bins RESET reset_bins STORED false) | ||
| 176 | - Q_PROPERTY(int radius READ get_radius WRITE set_radius RESET reset_radius STORED false) | ||
| 177 | - BR_PROPERTY(int, bins, 256) | ||
| 178 | - BR_PROPERTY(int, radius, 16) | ||
| 179 | - | ||
| 180 | - float stddev(const Mat &integral, int i, int j, int scale, int *buffer) const | ||
| 181 | - { | ||
| 182 | - const float count = scale*scale*radius*radius; | ||
| 183 | - | ||
| 184 | - float mean = 0; | ||
| 185 | - for (int k=0; k<bins; k++) { | ||
| 186 | - buffer[k] = integral.at<qint32>(i+scale,(j+scale)*bins+k) | ||
| 187 | - - integral.at<qint32>(i+scale, j *bins+k) | ||
| 188 | - - integral.at<qint32>(i ,(j+scale)*bins+k) | ||
| 189 | - + integral.at<qint32>(i , j *bins+k); | ||
| 190 | - mean += k*buffer[k]; | ||
| 191 | - } | ||
| 192 | - mean /= count; | ||
| 193 | - | ||
| 194 | - float variance = 0; | ||
| 195 | - for (int k=0; k<bins; k++) | ||
| 196 | - variance += buffer[k] * (k-mean) * (k-mean); | ||
| 197 | - | ||
| 198 | - return sqrt(variance/count); | ||
| 199 | - } | ||
| 200 | - | ||
| 201 | - void project(const Template &src, Template &dst) const | ||
| 202 | - { | ||
| 203 | - const Mat &m = src.m(); | ||
| 204 | - if (m.type() != CV_32SC1) qFatal("VarianceChangeDetector requires CV_32SC1 images from IntegralHist"); | ||
| 205 | - | ||
| 206 | - int *buffer = new int[bins]; | ||
| 207 | - | ||
| 208 | - float bestRatio = -std::numeric_limits<float>::max(); | ||
| 209 | - QRectF bestRect; | ||
| 210 | - | ||
| 211 | - const int rows = m.rows; | ||
| 212 | - const int cols = m.cols/bins; | ||
| 213 | - const int maxSize = min(m.rows, m.cols/bins); | ||
| 214 | - int scale = 2; | ||
| 215 | - while (scale < maxSize) { | ||
| 216 | - const int step = std::max(1, scale/6); | ||
| 217 | - for (int i=0; i+scale < rows; i+=step) { | ||
| 218 | - for (int j=0; j+scale < cols; j+=step) { | ||
| 219 | - float internalStdDev = stddev(m, i, j, scale, buffer); | ||
| 220 | - float externalStdDev = std::numeric_limits<float>::max(); | ||
| 221 | - externalStdDev = std::min(externalStdDev, ((i-2*scale >= 0) && (j-2*scale >= 0)) ? stddev(m, i-2*scale, j-2*scale, scale, buffer) : 0); | ||
| 222 | - externalStdDev = std::min(externalStdDev, (i-2*scale >= 0) ? stddev(m, i-2*scale, j , scale, buffer) : 0); | ||
| 223 | - externalStdDev = std::min(externalStdDev, ((i-2*scale >= 0) && (j+3*scale < cols)) ? stddev(m, i-2*scale, j+2*scale, scale, buffer) : 0); | ||
| 224 | - externalStdDev = std::min(externalStdDev, (j+3*scale < cols) ? stddev(m, i , j+2*scale, scale, buffer) : 0); | ||
| 225 | - externalStdDev = std::min(externalStdDev, ((i+3*scale < rows) && (j+3*scale < cols)) ? stddev(m, i+2*scale, j+2*scale, scale, buffer) : 0); | ||
| 226 | - externalStdDev = std::min(externalStdDev, (i+3*scale < rows) ? stddev(m, i+2*scale, j , scale, buffer) : 0); | ||
| 227 | - externalStdDev = std::min(externalStdDev, ((i+3*scale < rows) && (j-2*scale >= 0)) ? stddev(m, i+2*scale, j-2*scale, scale, buffer) : 0); | ||
| 228 | - externalStdDev = std::min(externalStdDev, (j-2*scale >= 0) ? stddev(m, i , j-2*scale, scale, buffer) : 0); | ||
| 229 | - | ||
| 230 | - float ratio; | ||
| 231 | - if (externalStdDev == 0) ratio = 0; | ||
| 232 | - else if (internalStdDev == 0) ratio = std::numeric_limits<float>::max() * (float(scale)/float(maxSize)); | ||
| 233 | - else ratio = scale*scale * pow(externalStdDev,2) / pow(internalStdDev, 2); | ||
| 234 | - | ||
| 235 | - if (ratio > bestRatio) { | ||
| 236 | - bestRatio = ratio; | ||
| 237 | - bestRect = QRect(j*radius, i*radius, scale*radius, scale*radius); | ||
| 238 | - } | ||
| 239 | - } | ||
| 240 | - } | ||
| 241 | - scale = std::max(scale+1, int(scale*1.25)); | ||
| 242 | - } | ||
| 243 | - | ||
| 244 | - delete[] buffer; | ||
| 245 | - dst.file.appendRect(bestRect); | ||
| 246 | - dst.file.setLabel(bestRatio); | ||
| 247 | - } | ||
| 248 | -}; | ||
| 249 | - | ||
| 250 | -BR_REGISTER(Transform, VarianceChangeDetectorTransform) | ||
| 251 | - | ||
| 252 | } // namespace br | 167 | } // namespace br |
| 253 | 168 | ||
| 254 | #include "hist.moc" | 169 | #include "hist.moc" |
sdk/plugins/integral.cpp
| 1 | #include <opencv2/imgproc/imgproc.hpp> | 1 | #include <opencv2/imgproc/imgproc.hpp> |
| 2 | +#include <Eigen/Core> | ||
| 2 | #include <openbr_plugin.h> | 3 | #include <openbr_plugin.h> |
| 3 | 4 | ||
| 5 | +using namespace cv; | ||
| 6 | + | ||
| 4 | namespace br | 7 | namespace br |
| 5 | { | 8 | { |
| 6 | 9 | ||
| @@ -15,7 +18,7 @@ class IntegralTransform : public UntrainableTransform | @@ -15,7 +18,7 @@ class IntegralTransform : public UntrainableTransform | ||
| 15 | 18 | ||
| 16 | void project(const Template &src, Template &dst) const | 19 | void project(const Template &src, Template &dst) const |
| 17 | { | 20 | { |
| 18 | - cv::integral(src, dst); | 21 | + integral(src, dst); |
| 19 | } | 22 | } |
| 20 | }; | 23 | }; |
| 21 | 24 | ||
| @@ -23,6 +26,71 @@ BR_REGISTER(Transform, IntegralTransform) | @@ -23,6 +26,71 @@ BR_REGISTER(Transform, IntegralTransform) | ||
| 23 | 26 | ||
| 24 | /*! | 27 | /*! |
| 25 | * \ingroup transforms | 28 | * \ingroup transforms |
| 29 | + * \brief Sliding window object recognition from a multi-channel intergral image. | ||
| 30 | + * \author Josh Klontz \cite jklontz | ||
| 31 | + */ | ||
| 32 | +class IntegralSampler : public UntrainableTransform | ||
| 33 | +{ | ||
| 34 | + Q_OBJECT | ||
| 35 | + Q_PROPERTY(int scales READ get_scales WRITE set_scales RESET reset_scales STORED false) | ||
| 36 | + Q_PROPERTY(float scaleFactor READ get_scaleFactor WRITE set_scaleFactor RESET reset_scaleFactor STORED false) | ||
| 37 | + Q_PROPERTY(float stepFactor READ get_stepFactor WRITE set_stepFactor RESET reset_stepFactor STORED false) | ||
| 38 | + Q_PROPERTY(int minSize READ get_minSize WRITE set_minSize RESET reset_minSize STORED false) | ||
| 39 | + BR_PROPERTY(int, scales, 1) | ||
| 40 | + BR_PROPERTY(float, scaleFactor, 1.25) | ||
| 41 | + BR_PROPERTY(float, stepFactor, 0.25) | ||
| 42 | + BR_PROPERTY(int, minSize, 8) | ||
| 43 | + | ||
| 44 | + void project(const Template &src, Template &dst) const | ||
| 45 | + { | ||
| 46 | + typedef Eigen::Map< const Eigen::Matrix<qint32,Eigen::Dynamic,1> > InputDescriptor; | ||
| 47 | + typedef Eigen::Map< Eigen::Matrix<qint32,Eigen::Dynamic,1> > OutputDescriptor; | ||
| 48 | + const Mat &m = src.m(); | ||
| 49 | + if (m.depth() != CV_32S) qFatal("Expected CV_32S matrix depth."); | ||
| 50 | + const int channels = m.channels(); | ||
| 51 | + const int rowStep = channels * m.cols; | ||
| 52 | + | ||
| 53 | + int descriptors = 0; | ||
| 54 | + int currentSize = min(m.rows, m.cols)-1; | ||
| 55 | + for (int scale=0; scale<scales; scale++) { | ||
| 56 | + descriptors += ceil((m.rows-currentSize)*stepFactor/currentSize) * | ||
| 57 | + ceil((m.cols-currentSize)*stepFactor/currentSize); | ||
| 58 | + currentSize /= scaleFactor; | ||
| 59 | + if (currentSize < minSize) | ||
| 60 | + break; | ||
| 61 | + } | ||
| 62 | + Mat n(descriptors, channels, CV_32SC1); | ||
| 63 | + | ||
| 64 | + const qint32 *dataIn = (qint32*)m.data; | ||
| 65 | + qint32 *dataOut = (qint32*)n.data; | ||
| 66 | + currentSize = min(m.rows, m.cols)-1; | ||
| 67 | + int index = 0; | ||
| 68 | + for (int scale=0; scale<scales; scale++) { | ||
| 69 | + const int currentStep = currentSize * stepFactor; | ||
| 70 | + for (int i=currentSize; i<m.rows; i+=currentStep) { | ||
| 71 | + for (int j=currentSize; j<m.cols; j+=currentStep) { | ||
| 72 | + InputDescriptor a(dataIn+((i-currentSize)*rowStep+(j-currentSize)*channels), channels, 1); | ||
| 73 | + InputDescriptor b(dataIn+((i-currentSize)*rowStep+ j *channels), channels, 1); | ||
| 74 | + InputDescriptor c(dataIn+( i *rowStep+(j-currentSize)*channels), channels, 1); | ||
| 75 | + InputDescriptor d(dataIn+( i *rowStep+ j *channels), channels, 1); | ||
| 76 | + OutputDescriptor y(dataOut+(index*channels), channels, 1); | ||
| 77 | + y = d-b-c+a; | ||
| 78 | + index++; | ||
| 79 | + } | ||
| 80 | + } | ||
| 81 | + currentSize /= scaleFactor; | ||
| 82 | + if (currentSize < minSize) | ||
| 83 | + break; | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + dst.m() = n; | ||
| 87 | + } | ||
| 88 | +}; | ||
| 89 | + | ||
| 90 | +BR_REGISTER(Transform, IntegralSampler) | ||
| 91 | + | ||
| 92 | +/*! | ||
| 93 | + * \ingroup transforms | ||
| 26 | * \brief Computes magnitude and/or angle of image. | 94 | * \brief Computes magnitude and/or angle of image. |
| 27 | * \author Josh Klontz \cite jklontz | 95 | * \author Josh Klontz \cite jklontz |
| 28 | */ | 96 | */ |
| @@ -41,10 +109,10 @@ private: | @@ -41,10 +109,10 @@ private: | ||
| 41 | void project(const Template &src, Template &dst) const | 109 | void project(const Template &src, Template &dst) const |
| 42 | { | 110 | { |
| 43 | if (src.m().type() != CV_8UC1) qFatal("Requires CV_8UC1 input."); | 111 | if (src.m().type() != CV_8UC1) qFatal("Requires CV_8UC1 input."); |
| 44 | - cv::Mat dx, dy, magnitude, angle; | ||
| 45 | - cv::Sobel(src, dx, CV_32F, 1, 0); | ||
| 46 | - cv::Sobel(src, dy, CV_32F, 0, 1); | ||
| 47 | - cv::cartToPolar(dx, dy, magnitude, angle, true); | 112 | + Mat dx, dy, magnitude, angle; |
| 113 | + Sobel(src, dx, CV_32F, 1, 0); | ||
| 114 | + Sobel(src, dy, CV_32F, 0, 1); | ||
| 115 | + cartToPolar(dx, dy, magnitude, angle, true); | ||
| 48 | if ((channel == Magnitude) || (channel == MagnitudeAndAngle)) | 116 | if ((channel == Magnitude) || (channel == MagnitudeAndAngle)) |
| 49 | dst.append(magnitude); | 117 | dst.append(magnitude); |
| 50 | if ((channel == Angle) || (channel == MagnitudeAndAngle)) | 118 | if ((channel == Angle) || (channel == MagnitudeAndAngle)) |
sdk/plugins/regions.cpp
| @@ -111,6 +111,26 @@ BR_REGISTER(Transform, CatTransform) | @@ -111,6 +111,26 @@ BR_REGISTER(Transform, CatTransform) | ||
| 111 | 111 | ||
| 112 | /*! | 112 | /*! |
| 113 | * \ingroup transforms | 113 | * \ingroup transforms |
| 114 | + * \brief Wraps OpenCV merge | ||
| 115 | + * \author Josh Klontz \cite jklontz | ||
| 116 | + */ | ||
| 117 | +class MergeTransform : public UntrainableMetaTransform | ||
| 118 | +{ | ||
| 119 | + Q_OBJECT | ||
| 120 | + | ||
| 121 | + void project(const Template &src, Template &dst) const | ||
| 122 | + { | ||
| 123 | + std::vector<Mat> mv; | ||
| 124 | + foreach (const Mat &m, src) | ||
| 125 | + mv.push_back(m); | ||
| 126 | + merge(mv, dst); | ||
| 127 | + } | ||
| 128 | +}; | ||
| 129 | + | ||
| 130 | +BR_REGISTER(Transform, MergeTransform) | ||
| 131 | + | ||
| 132 | +/*! | ||
| 133 | + * \ingroup transforms | ||
| 114 | * \brief Duplicates the template data. | 134 | * \brief Duplicates the template data. |
| 115 | * \author Josh Klontz \cite jklontz | 135 | * \author Josh Klontz \cite jklontz |
| 116 | */ | 136 | */ |