Commit cd5c1964df71918aced797e9deb7cffb3a10daf1
1 parent
3f09f0ba
Drop pixel.cpp, it is currently unmaintained (and broken by recent changes)
Showing
1 changed file
with
0 additions
and
298 deletions
openbr/plugins/pixel.cpp deleted
| 1 | -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | ||
| 2 | - * Copyright 2012 The MITRE Corporation * | ||
| 3 | - * * | ||
| 4 | - * Licensed under the Apache License, Version 2.0 (the "License"); * | ||
| 5 | - * you may not use this file except in compliance with the License. * | ||
| 6 | - * You may obtain a copy of the License at * | ||
| 7 | - * * | ||
| 8 | - * http://www.apache.org/licenses/LICENSE-2.0 * | ||
| 9 | - * * | ||
| 10 | - * Unless required by applicable law or agreed to in writing, software * | ||
| 11 | - * distributed under the License is distributed on an "AS IS" BASIS, * | ||
| 12 | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * | ||
| 13 | - * See the License for the specific language governing permissions and * | ||
| 14 | - * limitations under the License. * | ||
| 15 | - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | ||
| 16 | - | ||
| 17 | -#include "openbr_internal.h" | ||
| 18 | - | ||
| 19 | -using namespace cv; | ||
| 20 | - | ||
| 21 | -namespace br | ||
| 22 | -{ | ||
| 23 | - | ||
| 24 | -/*! | ||
| 25 | - * \ingroup transforms | ||
| 26 | - * \brief Treat each pixel as a classification task | ||
| 27 | - * \author E. Taborsky \cite mmtaborsky | ||
| 28 | - */ | ||
| 29 | -class PerPixelClassifierTransform : public MetaTransform | ||
| 30 | -{ | ||
| 31 | - // problematic -cao | ||
| 32 | - Q_OBJECT | ||
| 33 | - Q_PROPERTY(br::Transform* transform READ get_transform WRITE set_transform RESET reset_transform) | ||
| 34 | - Q_PROPERTY(int pixels READ get_pixels WRITE set_pixels RESET reset_pixels STORED false) | ||
| 35 | - Q_PROPERTY(int orient READ get_orient WRITE set_orient RESET reset_orient STORED false) | ||
| 36 | - BR_PROPERTY(br::Transform*, transform, NULL) | ||
| 37 | - BR_PROPERTY(int, pixels, 10000) | ||
| 38 | - BR_PROPERTY(bool, orient, false) | ||
| 39 | - | ||
| 40 | - /* | ||
| 41 | - Bins: | ||
| 42 | - |4|3|2| | ||
| 43 | - |5| |1| | ||
| 44 | - |6|7|8| | ||
| 45 | - */ | ||
| 46 | - | ||
| 47 | - QList<float> shift(int n, QList<float> &src) const | ||
| 48 | - { | ||
| 49 | - for (int i = 0; i < n; i++){ // Equivalent to src.append(src.takeFirst()) ? | ||
| 50 | - src.append(src.at(i)); | ||
| 51 | - src.removeFirst(); | ||
| 52 | - } | ||
| 53 | - return src; | ||
| 54 | - } | ||
| 55 | - | ||
| 56 | - void rotate(Template &src, Template &dst) const | ||
| 57 | - { | ||
| 58 | - int images = (src.m().cols)/9; | ||
| 59 | - dst = src; | ||
| 60 | - for (int i = 0; i < images; i++){ | ||
| 61 | - double a = src.m().at<float>(7+(i*9)); //top | ||
| 62 | - double b = src.m().at<float>(1+(i*9)); //bottom | ||
| 63 | - double c = src.m().at<float>(5+(i*9)); //right | ||
| 64 | - double d = src.m().at<float>(3+(i*9)); //left | ||
| 65 | - double orientation = atan2((a-b),(c-d)); | ||
| 66 | - int bin; | ||
| 67 | - if (orientation > 0){ | ||
| 68 | - bin = ((orientation/CV_PI)*4.0 +.5); | ||
| 69 | - } else { | ||
| 70 | - bin = 8.0 + ((orientation/CV_PI)*4.0 + .5); | ||
| 71 | - } | ||
| 72 | - | ||
| 73 | - // put things in an order that makes sense to rotate | ||
| 74 | - // blugh | ||
| 75 | - QList<float> orderedList; | ||
| 76 | - QList<float> rotatedList; | ||
| 77 | - orderedList.insert(0, src.m().at<float>(3+(i*9))); | ||
| 78 | - orderedList.insert(1, src.m().at<float>(6+(i*9))); | ||
| 79 | - orderedList.insert(2, src.m().at<float>(7+(i*9))); | ||
| 80 | - orderedList.insert(3, src.m().at<float>(8+(i*9))); | ||
| 81 | - orderedList.insert(4, src.m().at<float>(5+(i*9))); | ||
| 82 | - orderedList.insert(5, src.m().at<float>(2+(i*9))); | ||
| 83 | - orderedList.insert(6, src.m().at<float>(1+(i*9))); | ||
| 84 | - orderedList.insert(7, src.m().at<float>(0+(i*9))); | ||
| 85 | - | ||
| 86 | - rotatedList = shift(bin, orderedList); | ||
| 87 | - | ||
| 88 | - dst.m().at<float>(0+(i*9)) = rotatedList.at(7); | ||
| 89 | - dst.m().at<float>(1+(i*9)) = rotatedList.at(6); | ||
| 90 | - dst.m().at<float>(2+(i*9)) = rotatedList.at(5); | ||
| 91 | - dst.m().at<float>(3+(i*9)) = rotatedList.at(0); | ||
| 92 | - dst.m().at<float>(4+(i*9)) = src.m().at<float>(4+(i*9)); // middle pixel not in orderedList | ||
| 93 | - dst.m().at<float>(5+(i*9)) = rotatedList.at(4); | ||
| 94 | - dst.m().at<float>(6+(i*9)) = rotatedList.at(1); | ||
| 95 | - dst.m().at<float>(7+(i*9)) = rotatedList.at(2); | ||
| 96 | - dst.m().at<float>(8+(i*9)) = rotatedList.at(3); | ||
| 97 | - } | ||
| 98 | - } | ||
| 99 | - | ||
| 100 | - void train(const TemplateList &trainingSet) | ||
| 101 | - { | ||
| 102 | - TemplateList pixelTemplates = TemplateList(); | ||
| 103 | - const int length = trainingSet.length(); | ||
| 104 | - int pixelsPerImage = pixels/length; | ||
| 105 | - | ||
| 106 | - for (int i=0; i < length; i++){ | ||
| 107 | - Template src = trainingSet.at(i); | ||
| 108 | - | ||
| 109 | - const int mats = src.length(); | ||
| 110 | - const int rows = src.m().rows; | ||
| 111 | - const int cols = src.m().cols; | ||
| 112 | - | ||
| 113 | - RNG &rng = theRNG(); | ||
| 114 | - TemplateList srcPixelTemplates; | ||
| 115 | - | ||
| 116 | - for (int m=0; m < pixelsPerImage; m++){ | ||
| 117 | - int index = rng.uniform(0, rows*cols); | ||
| 118 | - Template temp = Template(src.file, cv::Mat(1, mats, CV_32F)); | ||
| 119 | - float *ptemp = (float*)temp.m().ptr(); | ||
| 120 | - for (int n=0; n < mats; n++){ | ||
| 121 | - uchar *psrc = src[n].ptr(); | ||
| 122 | - ptemp[n] = psrc[index]; | ||
| 123 | - } | ||
| 124 | - cv::Mat labelMat = src.file.value("labels").value<cv::Mat>(); | ||
| 125 | - uchar* plabel = labelMat.ptr(); | ||
| 126 | - temp.file.set("Label", plabel[index]); | ||
| 127 | - | ||
| 128 | - if (orient){ | ||
| 129 | - Template rotated; | ||
| 130 | - rotate(temp, rotated); | ||
| 131 | - srcPixelTemplates.append(rotated); | ||
| 132 | - } else { | ||
| 133 | - srcPixelTemplates.append(temp); | ||
| 134 | - } | ||
| 135 | - } | ||
| 136 | - pixelTemplates.append(srcPixelTemplates); | ||
| 137 | - } | ||
| 138 | - transform->train(pixelTemplates); | ||
| 139 | - } | ||
| 140 | - | ||
| 141 | - void project(const Template &src, Template &dst) const | ||
| 142 | - { | ||
| 143 | - const int mats = src.length(); | ||
| 144 | - const int rows = src.m().rows; | ||
| 145 | - const int cols = src.m().cols; | ||
| 146 | - | ||
| 147 | - dst = src; // Do we really want to copy all the src matrices into dst? | ||
| 148 | - dst.merge(Template(src.file, cv::Mat(src.m().rows, src.m().cols, CV_32F))); | ||
| 149 | - float *pdst = (float*) dst.m().ptr(); | ||
| 150 | - | ||
| 151 | - for (int r = 0; r < rows; r++){ | ||
| 152 | - for (int c = 0; c < cols; c++){ | ||
| 153 | - Template temp = Template(src.file, cv::Mat(1, mats, CV_32F)); | ||
| 154 | - Template dstTemp = Template(src.file, cv::Mat(1, mats, CV_32F)); | ||
| 155 | - | ||
| 156 | - for (int n=0; n < mats; n++){ | ||
| 157 | - const uchar *psrc = src[n].ptr(); | ||
| 158 | - float *ptemp = (float*)temp[0].ptr(); | ||
| 159 | - int index = r*cols + c; | ||
| 160 | - ptemp[n] = psrc[index]; | ||
| 161 | - } | ||
| 162 | - | ||
| 163 | - if (orient){ | ||
| 164 | - Template rotated = Template(src.file, cv::Mat(1, mats, CV_32F)); | ||
| 165 | - rotate(temp, rotated); | ||
| 166 | - temp = rotated; | ||
| 167 | - } | ||
| 168 | - | ||
| 169 | - transform->project(temp,dstTemp); | ||
| 170 | - pdst[r*cols + c] = dstTemp.file.get<float>("Label"); | ||
| 171 | - } | ||
| 172 | - } | ||
| 173 | - } | ||
| 174 | -}; | ||
| 175 | - | ||
| 176 | -BR_REGISTER(Transform, PerPixelClassifierTransform) | ||
| 177 | - | ||
| 178 | -/*! | ||
| 179 | - * \ingroup transforms | ||
| 180 | - * \brief Construct feature vectors of neighboring pixels | ||
| 181 | - * \author E. Taborsky \cite mmtaborsky | ||
| 182 | - */ | ||
| 183 | -class NeighborsTransform : public UntrainableMetaTransform | ||
| 184 | -{ | ||
| 185 | - Q_OBJECT | ||
| 186 | - | ||
| 187 | - void project(const Template &src, Template &dst) const | ||
| 188 | - { | ||
| 189 | - int rows = src.m().rows; | ||
| 190 | - int cols = src.m().cols; | ||
| 191 | - int mats = src.length(); | ||
| 192 | - dst.file = src.file; | ||
| 193 | - | ||
| 194 | - for (int n = 0; n < mats; n++){ //each matrix, except the last one, will be turned into 9 matrices | ||
| 195 | - const uchar *psrc = src[n].ptr(); | ||
| 196 | - for (int i = -1; i < 2; i++){ | ||
| 197 | - for (int j = -1; j < 2; j++){ // these nine matrices are shifted versions of the original | ||
| 198 | - cv::Mat mat = cv::Mat(rows, cols, CV_8UC1); | ||
| 199 | - uchar *pmat = (uchar*)mat.ptr(); | ||
| 200 | - for (int r = 0; r < rows; r++){ | ||
| 201 | - for (int c = 0; c < cols; c++){ | ||
| 202 | - int index = r*cols+c; | ||
| 203 | - int newIndex = index + i*cols + j; | ||
| 204 | - if ((newIndex < 0) || (newIndex >= rows*cols)){ | ||
| 205 | - pmat[index] = psrc[index]; | ||
| 206 | - } else { | ||
| 207 | - pmat[index] = psrc[newIndex]; | ||
| 208 | - } | ||
| 209 | - } | ||
| 210 | - } | ||
| 211 | - dst.push_back(mat); //add mat to dst | ||
| 212 | - } | ||
| 213 | - } | ||
| 214 | - } | ||
| 215 | - dst.push_back(src.m()); // add the last matrix | ||
| 216 | - } | ||
| 217 | -}; | ||
| 218 | - | ||
| 219 | -BR_REGISTER(Transform, NeighborsTransform) | ||
| 220 | - | ||
| 221 | -/*! | ||
| 222 | - * \ingroup transforms | ||
| 223 | - * \brief To binary vector | ||
| 224 | - * \author E. Taborsky \cite mmtaborsky | ||
| 225 | - */ | ||
| 226 | -class ToBinaryVectorTransform : public UntrainableMetaTransform | ||
| 227 | -{ | ||
| 228 | - Q_OBJECT | ||
| 229 | - Q_PROPERTY(br::Transform* transform READ get_transform WRITE set_transform RESET reset_transform STORED false) | ||
| 230 | - Q_PROPERTY(int length READ get_length WRITE set_length RESET reset_length STORED false) | ||
| 231 | - BR_PROPERTY(br::Transform*, transform, NULL) | ||
| 232 | - BR_PROPERTY(int, length, -1) | ||
| 233 | - | ||
| 234 | - //needs to be updated.. | ||
| 235 | - void project(const Template &src, Template &dst) const | ||
| 236 | - { | ||
| 237 | - | ||
| 238 | - dst = src; | ||
| 239 | - int mats = src.length(); | ||
| 240 | - for (int i = 0; i < mats; i++){ | ||
| 241 | - // Does this actually modify the data? | ||
| 242 | - dst[i]*(1.0/255.0); //scaling the input matrices to make the svm happier | ||
| 243 | - } | ||
| 244 | - for (int i = 0; i < length*(mats); i++){ | ||
| 245 | - dst.prepend(Template(src.file, Mat::zeros(src.m().rows, src.m().cols, CV_8U))); | ||
| 246 | - } | ||
| 247 | - | ||
| 248 | - // original pixel values at the end | ||
| 249 | - | ||
| 250 | - Template transformed; | ||
| 251 | - transformed.file = src.file; | ||
| 252 | - transform->project(src, transformed); | ||
| 253 | - | ||
| 254 | - int rows = transformed.m().rows; | ||
| 255 | - int cols = transformed.m().cols; | ||
| 256 | - | ||
| 257 | - for (int i = 0; i < mats; i++){ | ||
| 258 | - uchar *ptransformed = transformed[i].ptr(); | ||
| 259 | - for (int r = 0; r < rows; r++){ | ||
| 260 | - for (int c = 0; c < cols; c++){ | ||
| 261 | - uchar index = ptransformed[r*cols+c]; | ||
| 262 | - dst[index+(length*i)].at<uchar>(r,c) = 1; | ||
| 263 | - } | ||
| 264 | - } | ||
| 265 | - } | ||
| 266 | - } | ||
| 267 | -}; | ||
| 268 | - | ||
| 269 | -BR_REGISTER(Transform, ToBinaryVectorTransform) | ||
| 270 | - | ||
| 271 | -/*! | ||
| 272 | - * \ingroup transforms | ||
| 273 | - * \brief If "labels" is specified, makes the last matrix into metadata | ||
| 274 | - * \author E. Taborsky \cite mmtaborsky | ||
| 275 | - */ | ||
| 276 | - | ||
| 277 | -// What does this do? -cao | ||
| 278 | -class ToMetadataTransform : public UntrainableMetaTransform | ||
| 279 | -{ | ||
| 280 | - Q_OBJECT | ||
| 281 | - | ||
| 282 | - void project(const Template &src, Template &dst) const | ||
| 283 | - { | ||
| 284 | - dst = src; | ||
| 285 | - if (dst.file.contains("labels")){ | ||
| 286 | - QVariant last = qVariantFromValue(dst.m()); | ||
| 287 | - dst.file.set("labels", last); | ||
| 288 | - dst.pop_back(); | ||
| 289 | - } | ||
| 290 | - } | ||
| 291 | - | ||
| 292 | -}; | ||
| 293 | - | ||
| 294 | -BR_REGISTER(Transform, ToMetadataTransform) | ||
| 295 | - | ||
| 296 | -} // namespace br | ||
| 297 | - | ||
| 298 | -#include "pixel.moc" |