/LoadFreenectData.m

https://gitlab.com/anima_tcs/kinectcapture · Objective C · 205 lines · 191 code · 14 blank · 0 comment · 10 complexity · 77aa73faab35536751fd30a592a07164 MD5 · raw file

  1. function FData = LoadFreenectData(FileName)
  2. %LOADFREENECTDATA - Loads Freenect Image Data from Raw File
  3. % FData = LoadFreenectImages(FileName)
  4. % FileName - Raw Freenect Data Filename
  5. % FData - File Data
  6. % .ErrorMessage - Error Message
  7. % .Error - Error Number
  8. % .IRImage - IR Data
  9. % .RGBImage - RGB Data
  10. % .DepthImage - Depth Data
  11. % .RGBResolution - RGB Image Resolution
  12. % .IRResolution - IR Image Resolution
  13. % .RGBFormat - RGB Image Format
  14. % .IRFormat - IR Image Format
  15. % .DepthFormat - Depth Image Format
  16. % .Accelerometer - Accelerometer Reading
  17. %
  18. % This software is furnished "as is", without technical support,
  19. % and with no warranty, express or implied, as to its usefulness for
  20. % any purpose.
  21. % Author: Sk. Mohammadul Haque
  22. % Copyright (c) 2014 Sk. Mohammadul Haque
  23. % Licensed under the Apache License, Version 2.0 (the "License");
  24. % you may not use this file except in compliance with the License.
  25. % You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  26. % Unless required by applicable law or agreed to in writing, software distributed
  27. % under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  28. % CONDITIONS OF ANY KIND, either express or implied. See the License for the
  29. % specific language governing permissions and limitations under the License.
  30. %
  31. % For more details and updates, visit http://mohammadulhaque.alotspace.com
  32. %
  33. FData = struct();
  34. FData.ErrorMessage = '';
  35. FData.Error = 0;
  36. FData.IRImage = [];
  37. FData.RGBImage = [];
  38. FData.DepthImage = [];
  39. FData.RGBFormat = '';
  40. FData.IRFormat = '';
  41. FData.RGBBPP = 0;
  42. FData.IRBPP = 0;
  43. FData.DepthFormat = [];
  44. FData.RGBResolution = [];
  45. FData.IRResolution = [];
  46. FData.Accelerometer = [];
  47. fp = fopen(FileName, 'rb');
  48. if(fp~=-1)
  49. fheader = fgets(fp, 4);
  50. if(~strcmp(fheader, 'FNK0')&&~strcmp(fheader, 'FNK1'))
  51. FData.ErrorMessage = 'File Format Mismatch';
  52. FData.Error = 1;
  53. fclose(fp);
  54. return;
  55. end
  56. %%/ FREENECT_RESOLUTION_MEDIUM is 640x488 for the IR camera.
  57. %typedef enum {
  58. % FREENECT_RESOLUTION_LOW = 0, /**< QVGA - 320x240 */
  59. % FREENECT_RESOLUTION_MEDIUM = 1, /**< VGA - 640x480 */
  60. % FREENECT_RESOLUTION_HIGH = 2, /**< SXGA - 1280x1024 */
  61. %} freenect_resolution;
  62. curr_rgb_res = uint8(fgets(fp, 1));
  63. switch(curr_rgb_res)
  64. case 255
  65. FData.RGBResolution = [];
  66. case 1
  67. FData.RGBResolution = [480 640];
  68. case 2
  69. FData.RGBResolution = [1024 1280];
  70. otherwise
  71. FData.ErrorMessage = 'RGB Resolution Mismatch';
  72. FData.Error = 2;
  73. fclose(fp);
  74. return;
  75. end
  76. curr_ir_res = uint8(fgets(fp, 1));
  77. switch(curr_ir_res)
  78. case 255
  79. FData.IRResolution = [];
  80. case 1
  81. FData.IRResolution = [480 640];
  82. case 2
  83. FData.IRResolution = [1024 1280];
  84. otherwise
  85. FData.ErrorMessage = 'IR Resolution Mismatch';
  86. FData.Error = 3;
  87. fclose(fp);
  88. return;
  89. end
  90. %typedef enum {
  91. % FREENECT_VIDEO_RGB = 0, /**< Decompressed RGB mode (demosaicing done by libfreenect) */
  92. % FREENECT_VIDEO_BAYER = 1, /**< Bayer compressed mode (raw information from camera) */
  93. % FREENECT_VIDEO_IR_8BIT = 2, /**< 8-bit IR mode */
  94. % FREENECT_VIDEO_IR_10BIT = 3, /**< 10-bit IR mode */
  95. % FREENECT_VIDEO_IR_10BIT_PACKED = 4, /**< 10-bit packed IR mode */
  96. % FREENECT_VIDEO_YUV_RGB = 5, /**< YUV RGB mode */
  97. % FREENECT_VIDEO_YUV_RAW = 6, /**< YUV Raw mode */
  98. %} freenect_video_format;
  99. curr_rgb_fmt = uint8(fgets(fp, 1));
  100. switch(curr_rgb_fmt)
  101. case 255
  102. FData.RGBFormat = '';
  103. FData.RGBBPP = 0;
  104. case 0
  105. FData.RGBFormat = 'FREENECT_VIDEO_RGB ';
  106. FData.RGBBPP = 24;
  107. case 5
  108. FData.RGBFormat = 'FREENECT_VIDEO_YUV_RGB';
  109. FData.RGBBPP = 24;
  110. otherwise
  111. FData.ErrorMessage = 'RGB Image Format Mismatch';
  112. FData.Error = 4;
  113. fclose(fp);
  114. return;
  115. end
  116. curr_ir_fmt = uint8(fgets(fp, 1));
  117. switch(curr_ir_fmt)
  118. case 255
  119. FData.IRFormat = '';
  120. FData.IRBPP = 0;
  121. case 2
  122. FData.IRFormat = 'FREENECT_VIDEO_IR_8BIT';
  123. FData.IRBPP = 8;
  124. case 3
  125. FData.IRFormat = 'FREENECT_VIDEO_IR_10BIT';
  126. FData.IRBPP = 16;
  127. otherwise
  128. FData.ErrorMessage = 'IR Image Format Mismatch';
  129. FData.Error = 5;
  130. fclose(fp);
  131. return;
  132. end
  133. %typedef enum {
  134. % FREENECT_DEPTH_11BIT = 0, /**< 11 bit depth information in one uint16_t/pixel */
  135. % FREENECT_DEPTH_10BIT = 1, /**< 10 bit depth information in one uint16_t/pixel */
  136. % FREENECT_DEPTH_11BIT_PACKED = 2, /**< 11 bit packed depth information */
  137. % FREENECT_DEPTH_10BIT_PACKED = 3, /**< 10 bit packed depth information */
  138. % FREENECT_DEPTH_REGISTERED = 4, /**< processed depth data in mm, aligned to 640x480 RGB */
  139. % FREENECT_DEPTH_MM = 5, /**< depth to each pixel in mm, but left unaligned to RGB image */
  140. %} freenect_depth_format;
  141. curr_depth_fmt = uint8(fgets(fp, 1));
  142. switch(curr_depth_fmt)
  143. case 255
  144. FData.DepthFormat = '';
  145. case 0
  146. FData.DepthFormat = 'FREENECT_DEPTH_11BIT';
  147. case 1
  148. FData.DepthFormat = 'FREENECT_DEPTH_10BIT';
  149. otherwise
  150. FData.ErrorMessage = 'Depth Format Mismatch';
  151. FData.Error = 6;
  152. fclose(fp);
  153. return;
  154. end
  155. content = uint8(fgets(fp, 1));
  156. bincontent = de2bi(content, 4);
  157. % | x | RGB (3) | IR (2) | Depth (1) | - in reverse ordered
  158. try
  159. if(bincontent(1,3)==1)
  160. FData.RGBImage = uint8(reshape(fread(fp,3*prod(FData.RGBResolution), 'uint8'), [3*FData.RGBResolution(1,2) FData.RGBResolution(1,1)]).');
  161. FData.RGBImage = reshape([FData.RGBImage(:,1:3:end), FData.RGBImage(:,2:3:end), FData.RGBImage(:,3:3:end)], [FData.RGBResolution(1,1) FData.RGBResolution(1,2) 3]);
  162. end
  163. if(bincontent(1,2)==1)
  164. if(FData.IRBPP==8)
  165. FData.IRImage = uint8(reshape(fread(fp, prod(FData.IRResolution), 'uint8'), [FData.IRResolution(1,2) FData.IRResolution(1,1)]).');
  166. else
  167. FData.IRImage = uint16(reshape(fread(fp, prod(FData.IRResolution), 'uint16'), [FData.IRResolution(1,2) FData.IRResolution(1,1)]).');
  168. end
  169. end
  170. if(bincontent(1,1)==1)
  171. if(strcmp(fheader, 'FNK0'))
  172. FData.DepthImage = (reshape(fread(fp, 640*480, 'ubit24'), [640 480]).');
  173. else
  174. FData.DepthImage = (reshape(fread(fp, 640*480, 'ubit16'), [640 480]).');
  175. end;
  176. FData.DepthImage(FData.DepthImage==2047) = 0;
  177. end
  178. if(~feof(fp))
  179. FData.Accelerometer = fread(fp, 3, 'double');
  180. end
  181. fclose(fp);
  182. catch me
  183. FData.ErrorMessage = me.message;
  184. FData.Error = 7;
  185. fclose(fp);
  186. end
  187. else
  188. FData.ErrorMessage = 'Can not open File';
  189. FData.Error = 8;
  190. end
  191. end