From dc61f6191879dde42a3d9f704b1511cd8cb37a93 Mon Sep 17 00:00:00 2001 From: Charles Otto Date: Tue, 24 Feb 2015 14:03:15 -0800 Subject: [PATCH] Add multi-channel support to ut format 7 --- openbr/plugins/gallery/binary.cpp | 17 +++++++++++------ scripts/matlab/writeUT.m | 31 ++++++++++++++++++------------- scripts/matlab/writeUTFVectors.m | 3 --- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/openbr/plugins/gallery/binary.cpp b/openbr/plugins/gallery/binary.cpp index 1f7a94d..b83184c 100644 --- a/openbr/plugins/gallery/binary.cpp +++ b/openbr/plugins/gallery/binary.cpp @@ -231,15 +231,17 @@ class utGallery : public BinaryGallery } else if (ut.algorithmID == 7) { // binary data consisting of a single channel matrix, of a supported type. - // 3 element header: - // uint32 datatype (single channel opencv datatype code) + // 4 element header: + // uint16 datatype (single channel opencv datatype code) // uint32 matrix rows // uint32 matrix cols - // Followed by serialized data, in row-major order. + // uint16 matrix depth (max 512) + // Followed by serialized data, in row-major order (in r/c), with depth values + // for each layer listed in order (i.e. rgb, rgb etc.) // #### NOTE! matlab's default order is col-major, so some work should // be done on the matlab side to make sure that the initial serialization is correct. - uint32_t dataType = *reinterpret_cast(dataStart); - dataStart += sizeof(uint32_t); + uint16_t dataType = *reinterpret_cast(dataStart); + dataStart += sizeof(uint16_t); uint32_t matrixRows = *reinterpret_cast(dataStart); dataStart += sizeof(uint32_t); @@ -247,6 +249,9 @@ class utGallery : public BinaryGallery uint32_t matrixCols = *reinterpret_cast(dataStart); dataStart += sizeof(uint32_t); + uint16_t matrixDepth= *reinterpret_cast(dataStart); + dataStart += sizeof(uint16_t); + // Set metadata t.file.set("Label", ut.label); t.file.set("X", ut.x); @@ -254,7 +259,7 @@ class utGallery : public BinaryGallery t.file.set("Width", ut.width); t.file.set("Height", ut.height); - t.append(cv::Mat(matrixRows, matrixCols, CV_MAKETYPE(dataType, 1), dataStart).clone() /* We don't want a shallow copy! */); + t.append(cv::Mat(matrixRows, matrixCols, CV_MAKETYPE(dataType, matrixDepth), dataStart).clone() /* We don't want a shallow copy! */); return t; } else { diff --git a/scripts/matlab/writeUT.m b/scripts/matlab/writeUT.m index 70d0340..8399383 100644 --- a/scripts/matlab/writeUT.m +++ b/scripts/matlab/writeUT.m @@ -9,9 +9,13 @@ function writeUT(handle, matrix, imageID, roi_x, roi_y, roi_width, roi_height, l % computed values: fvSize, urlSize (url encoded as null-terminated 8-bit % string, urlSize includes null terminator), a null terminator will be % added to url by this function +% +% For performance reasons, handle should be opened in 'W', i.e. buffered +% mode. -if (size(matrix,3) ~= 1) - disp('Cannot serialize matrix, only single channel matrices are supported'); +% 512 -- max supported channels in cv::Mat +if (size(matrix,3) > 512) + disp('Cannot serialize matrix, 512 is the max depth supported'); return; end @@ -35,11 +39,12 @@ end % }; % algorithm 7 binary data format: -% uint32 datatype code (copied from opencv, base datatype codes, single +% uint16 datatype code (copied from opencv, base datatype codes, single % channel is assumed) % uint32 matrix rows % uint32 matrix cols -% row-major serialization of matrix +% uint16 channel count (max valid is 512) +% channels->rows->columns % opencv data type definitions % #define CV_8U 0 @@ -134,16 +139,16 @@ fwrite(handle, fvSize, 'uint32'); % url (just writing a single null byte) fwrite(handle, url, 'uint8'); -% binary data header -- datatype code, row count, col count -fwrite(handle, type_code,'uint32'); +% binary data header -- datatype code, row count, col count, channel count +% (max 512). Datatype and channel count are uint16, dimensions are uint32 +fwrite(handle, type_code,'uint16'); fwrite(handle, uint32(size(matrix,1)), 'uint32'); fwrite(handle, uint32(size(matrix,2)), 'uint32'); +fwrite(handle, uint16(size(matrix,3)), 'uint16'); -% write feature vector data, explicit row-major enumeration, matrix(:) is -% col-major -for i = 1:size(matrix,1) - fwrite(handle, matrix(i,:), matlab_type); -end - - +% write data, explicit row-major enumeration, matrix(:) is col-major, +% followed by depth. By permuting the dimensions, we can put the bytes in +% an appropriate order: +permuted = permute(matrix,[3,2,1]); +fwrite(handle, permuted(:), matlab_type); diff --git a/scripts/matlab/writeUTFVectors.m b/scripts/matlab/writeUTFVectors.m index 5b0d00c..5c40bc5 100644 --- a/scripts/matlab/writeUTFVectors.m +++ b/scripts/matlab/writeUTFVectors.m @@ -5,7 +5,6 @@ function writeUTFVectors(handle, fvectors) % % see also writeUT - dummy_ID = []; roi_x = uint32(0); roi_y = uint32(0); @@ -16,6 +15,4 @@ label = uint32(0); urlTotal = ''; for i = 1:size(fvectors,1) writeUT(handle, fvectors(i,:), dummy_ID, roi_x, roi_y, roi_width, roi_height, label, urlTotal); - end - -- libgit2 0.21.4