Commit dc61f6191879dde42a3d9f704b1511cd8cb37a93

Authored by Charles Otto
1 parent 80d49c1c

Add multi-channel support to ut format 7

openbr/plugins/gallery/binary.cpp
@@ -231,15 +231,17 @@ class utGallery : public BinaryGallery @@ -231,15 +231,17 @@ class utGallery : public BinaryGallery
231 } 231 }
232 else if (ut.algorithmID == 7) { 232 else if (ut.algorithmID == 7) {
233 // binary data consisting of a single channel matrix, of a supported type. 233 // binary data consisting of a single channel matrix, of a supported type.
234 - // 3 element header:  
235 - // uint32 datatype (single channel opencv datatype code) 234 + // 4 element header:
  235 + // uint16 datatype (single channel opencv datatype code)
236 // uint32 matrix rows 236 // uint32 matrix rows
237 // uint32 matrix cols 237 // uint32 matrix cols
238 - // Followed by serialized data, in row-major order. 238 + // uint16 matrix depth (max 512)
  239 + // Followed by serialized data, in row-major order (in r/c), with depth values
  240 + // for each layer listed in order (i.e. rgb, rgb etc.)
239 // #### NOTE! matlab's default order is col-major, so some work should 241 // #### NOTE! matlab's default order is col-major, so some work should
240 // be done on the matlab side to make sure that the initial serialization is correct. 242 // be done on the matlab side to make sure that the initial serialization is correct.
241 - uint32_t dataType = *reinterpret_cast<uint32_t*>(dataStart);  
242 - dataStart += sizeof(uint32_t); 243 + uint16_t dataType = *reinterpret_cast<uint32_t*>(dataStart);
  244 + dataStart += sizeof(uint16_t);
243 245
244 uint32_t matrixRows = *reinterpret_cast<uint32_t*>(dataStart); 246 uint32_t matrixRows = *reinterpret_cast<uint32_t*>(dataStart);
245 dataStart += sizeof(uint32_t); 247 dataStart += sizeof(uint32_t);
@@ -247,6 +249,9 @@ class utGallery : public BinaryGallery @@ -247,6 +249,9 @@ class utGallery : public BinaryGallery
247 uint32_t matrixCols = *reinterpret_cast<uint32_t*>(dataStart); 249 uint32_t matrixCols = *reinterpret_cast<uint32_t*>(dataStart);
248 dataStart += sizeof(uint32_t); 250 dataStart += sizeof(uint32_t);
249 251
  252 + uint16_t matrixDepth= *reinterpret_cast<uint16_t*>(dataStart);
  253 + dataStart += sizeof(uint16_t);
  254 +
250 // Set metadata 255 // Set metadata
251 t.file.set("Label", ut.label); 256 t.file.set("Label", ut.label);
252 t.file.set("X", ut.x); 257 t.file.set("X", ut.x);
@@ -254,7 +259,7 @@ class utGallery : public BinaryGallery @@ -254,7 +259,7 @@ class utGallery : public BinaryGallery
254 t.file.set("Width", ut.width); 259 t.file.set("Width", ut.width);
255 t.file.set("Height", ut.height); 260 t.file.set("Height", ut.height);
256 261
257 - t.append(cv::Mat(matrixRows, matrixCols, CV_MAKETYPE(dataType, 1), dataStart).clone() /* We don't want a shallow copy! */); 262 + t.append(cv::Mat(matrixRows, matrixCols, CV_MAKETYPE(dataType, matrixDepth), dataStart).clone() /* We don't want a shallow copy! */);
258 return t; 263 return t;
259 } 264 }
260 else { 265 else {
scripts/matlab/writeUT.m
@@ -9,9 +9,13 @@ function writeUT(handle, matrix, imageID, roi_x, roi_y, roi_width, roi_height, l @@ -9,9 +9,13 @@ function writeUT(handle, matrix, imageID, roi_x, roi_y, roi_width, roi_height, l
9 % computed values: fvSize, urlSize (url encoded as null-terminated 8-bit 9 % computed values: fvSize, urlSize (url encoded as null-terminated 8-bit
10 % string, urlSize includes null terminator), a null terminator will be 10 % string, urlSize includes null terminator), a null terminator will be
11 % added to url by this function 11 % added to url by this function
  12 +%
  13 +% For performance reasons, handle should be opened in 'W', i.e. buffered
  14 +% mode.
12 15
13 -if (size(matrix,3) ~= 1)  
14 - disp('Cannot serialize matrix, only single channel matrices are supported'); 16 +% 512 -- max supported channels in cv::Mat
  17 +if (size(matrix,3) > 512)
  18 + disp('Cannot serialize matrix, 512 is the max depth supported');
15 return; 19 return;
16 end 20 end
17 21
@@ -35,11 +39,12 @@ end @@ -35,11 +39,12 @@ end
35 % }; 39 % };
36 40
37 % algorithm 7 binary data format: 41 % algorithm 7 binary data format:
38 -% uint32 datatype code (copied from opencv, base datatype codes, single 42 +% uint16 datatype code (copied from opencv, base datatype codes, single
39 % channel is assumed) 43 % channel is assumed)
40 % uint32 matrix rows 44 % uint32 matrix rows
41 % uint32 matrix cols 45 % uint32 matrix cols
42 -% row-major serialization of matrix 46 +% uint16 channel count (max valid is 512)
  47 +% channels->rows->columns
43 48
44 % opencv data type definitions 49 % opencv data type definitions
45 % #define CV_8U 0 50 % #define CV_8U 0
@@ -134,16 +139,16 @@ fwrite(handle, fvSize, &#39;uint32&#39;); @@ -134,16 +139,16 @@ fwrite(handle, fvSize, &#39;uint32&#39;);
134 % url (just writing a single null byte) 139 % url (just writing a single null byte)
135 fwrite(handle, url, 'uint8'); 140 fwrite(handle, url, 'uint8');
136 141
137 -% binary data header -- datatype code, row count, col count  
138 -fwrite(handle, type_code,'uint32'); 142 +% binary data header -- datatype code, row count, col count, channel count
  143 +% (max 512). Datatype and channel count are uint16, dimensions are uint32
  144 +fwrite(handle, type_code,'uint16');
139 fwrite(handle, uint32(size(matrix,1)), 'uint32'); 145 fwrite(handle, uint32(size(matrix,1)), 'uint32');
140 fwrite(handle, uint32(size(matrix,2)), 'uint32'); 146 fwrite(handle, uint32(size(matrix,2)), 'uint32');
  147 +fwrite(handle, uint16(size(matrix,3)), 'uint16');
141 148
142 -% write feature vector data, explicit row-major enumeration, matrix(:) is  
143 -% col-major  
144 -for i = 1:size(matrix,1)  
145 - fwrite(handle, matrix(i,:), matlab_type);  
146 -end  
147 -  
148 - 149 +% write data, explicit row-major enumeration, matrix(:) is col-major,
  150 +% followed by depth. By permuting the dimensions, we can put the bytes in
  151 +% an appropriate order:
  152 +permuted = permute(matrix,[3,2,1]);
149 153
  154 +fwrite(handle, permuted(:), matlab_type);
scripts/matlab/writeUTFVectors.m
@@ -5,7 +5,6 @@ function writeUTFVectors(handle, fvectors) @@ -5,7 +5,6 @@ function writeUTFVectors(handle, fvectors)
5 % 5 %
6 % see also writeUT 6 % see also writeUT
7 7
8 -  
9 dummy_ID = []; 8 dummy_ID = [];
10 roi_x = uint32(0); 9 roi_x = uint32(0);
11 roi_y = uint32(0); 10 roi_y = uint32(0);
@@ -16,6 +15,4 @@ label = uint32(0); @@ -16,6 +15,4 @@ label = uint32(0);
16 urlTotal = ''; 15 urlTotal = '';
17 for i = 1:size(fvectors,1) 16 for i = 1:size(fvectors,1)
18 writeUT(handle, fvectors(i,:), dummy_ID, roi_x, roi_y, roi_width, roi_height, label, urlTotal); 17 writeUT(handle, fvectors(i,:), dummy_ID, roi_x, roi_y, roi_width, roi_height, label, urlTotal);
19 -  
20 end 18 end
21 -