Commit b739adca69c59159f86a462c455e4d2148c2c8c9

Authored by M Taborsky
1 parent 8a07805c

added matrix to metadata

Showing 1 changed file with 45 additions and 31 deletions
sdk/plugins/pixel.cpp
@@ -34,8 +34,6 @@ class PerPixelClassifier : public MetaTransform @@ -34,8 +34,6 @@ class PerPixelClassifier : public MetaTransform
34 BR_PROPERTY(int, pixels, 10000) 34 BR_PROPERTY(int, pixels, 10000)
35 BR_PROPERTY(bool, orient, false) 35 BR_PROPERTY(bool, orient, false)
36 36
37 - //int matrices = 0;  
38 -  
39 /* 37 /*
40 Bins: 38 Bins:
41 |4|3|2| 39 |4|3|2|
@@ -54,7 +52,7 @@ class PerPixelClassifier : public MetaTransform @@ -54,7 +52,7 @@ class PerPixelClassifier : public MetaTransform
54 52
55 void rotate(Template &src, Template &dst) const 53 void rotate(Template &src, Template &dst) const
56 { 54 {
57 - // if temp.m.cols()%9 != 0, some error about how neighbors needs to be used. 55 + if (temp.m.cols()%9 != 0) qFatal("Rotation invariance can only be used after Neighbors");
58 int images = (src.m().cols)/9; 56 int images = (src.m().cols)/9;
59 dst = src; 57 dst = src;
60 for (int i = 0; i < images; i++){ 58 for (int i = 0; i < images; i++){
@@ -103,29 +101,28 @@ class PerPixelClassifier : public MetaTransform @@ -103,29 +101,28 @@ class PerPixelClassifier : public MetaTransform
103 const int length = trainingSet.length(); 101 const int length = trainingSet.length();
104 int pixelsPerImage = pixels/length; 102 int pixelsPerImage = pixels/length;
105 103
106 - for (int i=0; i < length; i++){ // Consider using foreach loops when the induction variable is only used to index into the list 104 + for (int i=0; i < length; i++){
107 Template src = trainingSet.at(i); 105 Template src = trainingSet.at(i);
108 - //matrices = src.length();  
109 106
110 const int mats = src.length(); 107 const int mats = src.length();
111 const int rows = src.m().rows; 108 const int rows = src.m().rows;
112 const int cols = src.m().cols; 109 const int cols = src.m().cols;
113 110
114 RNG &rng = theRNG(); 111 RNG &rng = theRNG();
115 - TemplateList srcPixelTemplates = TemplateList(); // Equivalent to "TemplateList srcPixelTemplates;" 112 + TemplateList srcPixelTemplates;
116 113
117 for (int m=0; m < pixelsPerImage; m++){ 114 for (int m=0; m < pixelsPerImage; m++){
118 int index = rng.uniform(0, rows*cols); 115 int index = rng.uniform(0, rows*cols);
119 - Template temp = Template(src.file, cv::Mat(1, (mats-1), CV_32F));  
120 - float *ptemp = (float*)temp.m().ptr(); // I'd encourage you to always use Mat::at<>() for indexing into matrices 116 + Template temp = Template(src.file, cv::Mat(1, mats, CV_32F));
  117 + float *ptemp = (float*)temp.m().ptr();
121 for (int n=0; n < mats; n++){ 118 for (int n=0; n < mats; n++){
122 uchar *psrc = src[n].ptr(); 119 uchar *psrc = src[n].ptr();
123 - if (n == mats-1){  
124 - temp.file.setLabel(psrc[index]);  
125 - } else {  
126 - ptemp[n] = psrc[index];  
127 - } 120 + ptemp[n] = psrc[index];
128 } 121 }
  122 + cv::Mat labelMat = src.file.get("labels").value<cv::Mat>();
  123 + uchar* plabel = labelMat.ptr();
  124 + temp.file.setLabel(plabel[index]);
  125 +
129 if (orient){ 126 if (orient){
130 Template rotated; 127 Template rotated;
131 rotate(temp, rotated); 128 rotate(temp, rotated);
@@ -136,31 +133,25 @@ class PerPixelClassifier : public MetaTransform @@ -136,31 +133,25 @@ class PerPixelClassifier : public MetaTransform
136 } 133 }
137 pixelTemplates.append(srcPixelTemplates); 134 pixelTemplates.append(srcPixelTemplates);
138 } 135 }
139 - //qDebug("Count: %i", count);  
140 - //matrices--;  
141 transform->train(pixelTemplates); 136 transform->train(pixelTemplates);
142 } 137 }
143 138
144 - // Factor out the logic for creating a template at a single pixel, rotating it, and float-casting it and call it from both train() and project()?  
145 void project(const Template &src, Template &dst) const 139 void project(const Template &src, Template &dst) const
146 { 140 {
147 const int mats = src.length(); 141 const int mats = src.length();
148 const int rows = src.m().rows; 142 const int rows = src.m().rows;
149 const int cols = src.m().cols; 143 const int cols = src.m().cols;
150 144
151 - //if (matrices == 0) matrices = src.length();  
152 -  
153 dst = src; // Do we really want to copy all the src matrices into dst? 145 dst = src; // Do we really want to copy all the src matrices into dst?
154 - dst.merge(Template(src.file, cv::Mat(src.m().rows, src.m().cols, CV_32F))); // Sorry the syntax for appending a single matrix is broken, consider using dst += cv::Mat() instead for the time being 146 + dst.merge(Template(src.file, cv::Mat(src.m().rows, src.m().cols, CV_32F)));
155 float *pdst = (float*) dst.m().ptr(); 147 float *pdst = (float*) dst.m().ptr();
156 148
157 for (int r = 0; r < rows; r++){ 149 for (int r = 0; r < rows; r++){
158 for (int c = 0; c < cols; c++){ 150 for (int c = 0; c < cols; c++){
159 - Template temp = Template(src.file, cv::Mat(1, (mats-1), CV_32F));  
160 - Template dstTemp = Template(src.file, cv::Mat(1, (mats-1), CV_32F));  
161 - 151 + Template temp = Template(src.file, cv::Mat(1, mats, CV_32F));
  152 + Template dstTemp = Template(src.file, cv::Mat(1, mats, CV_32F));
162 153
163 - for (int n=0; n < mats-1; n++){ // Consider using CvtFloat transform instead of doing this by hand 154 + for (int n=0; n < mats; n++){
164 const uchar *psrc = src[n].ptr(); 155 const uchar *psrc = src[n].ptr();
165 float *ptemp = (float*)temp[0].ptr(); 156 float *ptemp = (float*)temp[0].ptr();
166 int index = r*cols + c; 157 int index = r*cols + c;
@@ -168,14 +159,12 @@ class PerPixelClassifier : public MetaTransform @@ -168,14 +159,12 @@ class PerPixelClassifier : public MetaTransform
168 } 159 }
169 160
170 if (orient){ 161 if (orient){
171 - Template rotated = Template(src.file, cv::Mat(1, (mats-1), CV_32F)); 162 + Template rotated = Template(src.file, cv::Mat(1, mats, CV_32F));
172 rotate(temp, rotated); 163 rotate(temp, rotated);
173 temp = rotated; 164 temp = rotated;
174 - //transform->project(rotated,dstTemp);  
175 } 165 }
176 - transform->project(temp,dstTemp);  
177 166
178 - //transform->project(temp, dstTemp); 167 + transform->project(temp,dstTemp);
179 pdst[r*cols+c] = dstTemp.file.label(); 168 pdst[r*cols+c] = dstTemp.file.label();
180 } 169 }
181 } 170 }
@@ -200,7 +189,7 @@ class Neighbors: public UntrainableMetaTransform @@ -200,7 +189,7 @@ class Neighbors: public UntrainableMetaTransform
200 int mats = src.length(); 189 int mats = src.length();
201 dst.file = src.file; 190 dst.file = src.file;
202 191
203 - for (int n = 0; n < mats-1; n++){ //each matrix, except the last one, will be turned into 9 matrices 192 + for (int n = 0; n < mats; n++){ //each matrix, except the last one, will be turned into 9 matrices
204 const uchar *psrc = src[n].ptr(); 193 const uchar *psrc = src[n].ptr();
205 for (int i = -1; i < 2; i++){ 194 for (int i = -1; i < 2; i++){
206 for (int j = -1; j < 2; j++){ // these nine matrices are shifted versions of the original 195 for (int j = -1; j < 2; j++){ // these nine matrices are shifted versions of the original
@@ -240,16 +229,17 @@ class ToBinaryVector: public UntrainableMetaTransform @@ -240,16 +229,17 @@ class ToBinaryVector: public UntrainableMetaTransform
240 BR_PROPERTY(br::Transform*, transform, NULL) 229 BR_PROPERTY(br::Transform*, transform, NULL)
241 BR_PROPERTY(int, length, -1) 230 BR_PROPERTY(int, length, -1)
242 231
  232 + //needs to be updated..
243 void project(const Template &src, Template &dst) const 233 void project(const Template &src, Template &dst) const
244 { 234 {
245 235
246 dst = src; 236 dst = src;
247 int mats = src.length(); 237 int mats = src.length();
248 - for (int i = 0; i < mats-1; i++){ 238 + for (int i = 0; i < mats; i++){
249 // Does this actually modify the data? 239 // Does this actually modify the data?
250 dst[i]*(1.0/255.0); //scaling the input matrices to make the svm happier 240 dst[i]*(1.0/255.0); //scaling the input matrices to make the svm happier
251 } 241 }
252 - for (int i = 0; i < length*(mats-1); i++){ 242 + for (int i = 0; i < length*(mats); i++){
253 dst.prepend(Template(src.file, Mat::zeros(src.m().rows, src.m().cols, CV_8U))); 243 dst.prepend(Template(src.file, Mat::zeros(src.m().rows, src.m().cols, CV_8U)));
254 } 244 }
255 245
@@ -262,7 +252,7 @@ class ToBinaryVector: public UntrainableMetaTransform @@ -262,7 +252,7 @@ class ToBinaryVector: public UntrainableMetaTransform
262 int rows = transformed.m().rows; 252 int rows = transformed.m().rows;
263 int cols = transformed.m().cols; 253 int cols = transformed.m().cols;
264 254
265 - for (int i = 0; i < mats-1; i++){ 255 + for (int i = 0; i < mats; i++){
266 uchar *ptransformed = transformed[i].ptr(); 256 uchar *ptransformed = transformed[i].ptr();
267 for (int r = 0; r < rows; r++){ 257 for (int r = 0; r < rows; r++){
268 for (int c = 0; c < cols; c++){ 258 for (int c = 0; c < cols; c++){
@@ -276,4 +266,28 @@ class ToBinaryVector: public UntrainableMetaTransform @@ -276,4 +266,28 @@ class ToBinaryVector: public UntrainableMetaTransform
276 266
277 BR_REGISTER(Transform, ToBinaryVector) 267 BR_REGISTER(Transform, ToBinaryVector)
278 268
  269 +/*!
  270 + * \ingroup transforms
  271 + * \brief If "labels" is specified, makes the last matrix into metadata
  272 + * \author E. Taborsky \cite mmtaborsky
  273 + */
  274 +
  275 +class ToMetadata: public UntrainableMetaTransform
  276 +{
  277 + Q_OBJECT
  278 +
  279 + void project(const Template &src, Template &dst) const
  280 + {
  281 + dst = src;
  282 + if (dst.file.contains("labels")){
  283 + QVariant last = qVariantFromValue(dst.m());
  284 + dst.file.set("labels", last);
  285 + dst.pop_back();
  286 + }
  287 + }
  288 +
  289 +};
  290 +
  291 +BR_REGISTER(Transform, ToMetadata)
  292 +
279 #include "pixel.moc" 293 #include "pixel.moc"