TYCoordinateMapper.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /**@file TYCoordinateMapper.h
  2. * @brief Coordinate Conversion API
  3. * @note Considering performance, we leave the responsibility of parameters check to users.
  4. * @copyright Copyright(C)2016-2018 Percipio All Rights Reserved
  5. **/
  6. #ifndef TY_COORDINATE_MAPPER_H_
  7. #define TY_COORDINATE_MAPPER_H_
  8. #include <stdlib.h>
  9. #include "TYApi.h"
  10. typedef struct TY_PIXEL_DESC
  11. {
  12. int16_t x; // x coordinate in pixels
  13. int16_t y; // y coordinate in pixels
  14. uint16_t depth; // depth value
  15. uint16_t rsvd;
  16. }TY_PIXEL_DESC;
  17. typedef struct TY_PIXEL_COLOR_DESC
  18. {
  19. int16_t x; // x coordinate in pixels
  20. int16_t y; // y coordinate in pixels
  21. uint8_t bgr_ch1; // color info <channel 1>
  22. uint8_t bgr_ch2; // color info <channel 2>
  23. uint8_t bgr_ch3; // color info <channel 3>
  24. uint8_t rsvd;
  25. }TY_PIXEL_COLOR_DESC;
  26. // ------------------------------
  27. // base convertion
  28. // ------------------------------
  29. /// @brief Calculate 4x4 extrinsic matrix's inverse matrix.
  30. /// @param [in] orgExtrinsic Input extrinsic matrix.
  31. /// @param [out] invExtrinsic Inverse matrix.
  32. /// @retval TY_STATUS_OK Succeed.
  33. /// @retval TY_STATUS_ERROR Calculation failed.
  34. TY_CAPI TYInvertExtrinsic (const TY_CAMERA_EXTRINSIC* orgExtrinsic,
  35. TY_CAMERA_EXTRINSIC* invExtrinsic);
  36. /// @brief Map pixels on depth image to 3D points.
  37. /// @param [in] src_calib Depth image's calibration data.
  38. /// @param [in] depthW Width of depth image.
  39. /// @param [in] depthH Height of depth image.
  40. /// @param [in] depthPixels Pixels on depth image.
  41. /// @param [in] count Number of depth pixels.
  42. /// @param [out] point3d Output point3D.
  43. /// @retval TY_STATUS_OK Succeed.
  44. TY_CAPI TYMapDepthToPoint3d (const TY_CAMERA_CALIB_INFO* src_calib,
  45. uint32_t depthW, uint32_t depthH,
  46. const TY_PIXEL_DESC* depthPixels, uint32_t count,
  47. TY_VECT_3F* point3d,
  48. float f_scale_unit = 1.0f);
  49. /// @brief Map 3D points to pixels on depth image. Reverse operation of TYMapDepthToPoint3d.
  50. /// @param [in] dst_calib Target depth image's calibration data.
  51. /// @param [in] point3d Input 3D points.
  52. /// @param [in] count Number of points.
  53. /// @param [in] depthW Width of target depth image.
  54. /// @param [in] depthH Height of target depth image.
  55. /// @param [out] depth Output depth pixels.
  56. /// @retval TY_STATUS_OK Succeed.
  57. TY_CAPI TYMapPoint3dToDepth (const TY_CAMERA_CALIB_INFO* dst_calib,
  58. const TY_VECT_3F* point3d, uint32_t count,
  59. uint32_t depthW, uint32_t depthH,
  60. TY_PIXEL_DESC* depth,
  61. float f_scale_unit = 1.0f);
  62. /// @brief Map depth image to 3D points. 0 depth pixels maps to (NAN, NAN, NAN).
  63. /// @param [in] src_calib Depth image's calibration data.
  64. /// @param [in] depthW Width of depth image.
  65. /// @param [in] depthH Height of depth image.
  66. /// @param [in] depth Depth image.
  67. /// @param [out] point3d Output point3D image.
  68. /// @retval TY_STATUS_OK Succeed.
  69. TY_CAPI TYMapDepthImageToPoint3d (const TY_CAMERA_CALIB_INFO* src_calib,
  70. int32_t imageW, int32_t imageH,
  71. const uint16_t* depth,
  72. TY_VECT_3F* point3d,
  73. float f_scale_unit = 1.0f);
  74. /// @brief Map 3D points to depth image. (NAN, NAN, NAN) will be skipped.
  75. /// @param [in] dst_calib Target depth image's calibration data.
  76. /// @param [in] point3d Input 3D points.
  77. /// @param [in] count Number of points.
  78. /// @param [in] depthW Width of target depth image.
  79. /// @param [in] depthH Height of target depth image.
  80. /// @param [in,out] depth Depth image buffer.
  81. /// @retval TY_STATUS_OK Succeed.
  82. TY_CAPI TYMapPoint3dToDepthImage (const TY_CAMERA_CALIB_INFO* dst_calib,
  83. const TY_VECT_3F* point3d, uint32_t count,
  84. uint32_t depthW, uint32_t depthH, uint16_t* depth,
  85. float f_target_scale = 1.0f);
  86. /// @brief Map 3D points to another coordinate.
  87. /// @param [in] extrinsic Extrinsic matrix.
  88. /// @param [in] point3dFrom Source 3D points.
  89. /// @param [in] count Number of source 3D points.
  90. /// @param [out] point3dTo Target 3D points.
  91. /// @retval TY_STATUS_OK Succeed.
  92. TY_CAPI TYMapPoint3dToPoint3d (const TY_CAMERA_EXTRINSIC* extrinsic,
  93. const TY_VECT_3F* point3dFrom, int32_t count,
  94. TY_VECT_3F* point3dTo);
  95. // ------------------------------
  96. // inlines
  97. // ------------------------------
  98. /// @brief Map depth pixels to color coordinate pixels.
  99. /// @param [in] depth_calib Depth image's calibration data.
  100. /// @param [in] depthW Width of current depth image.
  101. /// @param [in] depthH Height of current depth image.
  102. /// @param [in] depth Depth image pixels.
  103. /// @param [in] count Number of depth image pixels.
  104. /// @param [in] color_calib Color image's calibration data.
  105. /// @param [in] mappedW Width of target depth image.
  106. /// @param [in] mappedH Height of target depth image.
  107. /// @param [out] mappedDepth Output pixels.
  108. /// @retval TY_STATUS_OK Succeed.
  109. static inline TY_STATUS TYMapDepthToColorCoordinate(
  110. const TY_CAMERA_CALIB_INFO* depth_calib,
  111. uint32_t depthW, uint32_t depthH,
  112. const TY_PIXEL_DESC* depth, uint32_t count,
  113. const TY_CAMERA_CALIB_INFO* color_calib,
  114. uint32_t mappedW, uint32_t mappedH,
  115. TY_PIXEL_DESC* mappedDepth,
  116. float f_scale_unit = 1.0f);
  117. /// @brief Map original depth image to color coordinate depth image.
  118. /// @param [in] depth_calib Depth image's calibration data.
  119. /// @param [in] depthW Width of current depth image.
  120. /// @param [in] depthH Height of current depth image.
  121. /// @param [in] depth Depth image.
  122. /// @param [in] color_calib Color image's calibration data.
  123. /// @param [in] mappedW Width of target depth image.
  124. /// @param [in] mappedH Height of target depth image.
  125. /// @param [out] mappedDepth Output pixels.
  126. /// @retval TY_STATUS_OK Succeed.
  127. static inline TY_STATUS TYMapDepthImageToColorCoordinate(
  128. const TY_CAMERA_CALIB_INFO* depth_calib,
  129. uint32_t depthW, uint32_t depthH, const uint16_t* depth,
  130. const TY_CAMERA_CALIB_INFO* color_calib,
  131. uint32_t mappedW, uint32_t mappedH, uint16_t* mappedDepth,
  132. float f_scale_unit = 1.0f);
  133. /// @brief Create depth image to color coordinate lookup table.
  134. /// @param [in] depth_calib Depth image's calibration data.
  135. /// @param [in] depthW Width of current depth image.
  136. /// @param [in] depthH Height of current depth image.
  137. /// @param [in] depth Depth image.
  138. /// @param [in] color_calib Color image's calibration data.
  139. /// @param [in] mappedW Width of target depth image.
  140. /// @param [in] mappedH Height of target depth image.
  141. /// @param [out] lut Output lookup table.
  142. /// @retval TY_STATUS_OK Succeed.
  143. static inline TY_STATUS TYCreateDepthToColorCoordinateLookupTable(
  144. const TY_CAMERA_CALIB_INFO* depth_calib,
  145. uint32_t depthW, uint32_t depthH, const uint16_t* depth,
  146. const TY_CAMERA_CALIB_INFO* color_calib,
  147. uint32_t mappedW, uint32_t mappedH,
  148. TY_PIXEL_DESC* lut,
  149. float f_scale_unit = 1.0f);
  150. /// @brief Map original RGB pixels to depth coordinate.
  151. /// @param [in] depth_calib Depth image's calibration data.
  152. /// @param [in] depthW Width of current depth image.
  153. /// @param [in] depthH Height of current depth image.
  154. /// @param [in] depth Current depth image.
  155. /// @param [in] color_calib Color image's calibration data.
  156. /// @param [in] rgbW Width of RGB image.
  157. /// @param [in] rgbH Height of RGB image.
  158. /// @param [in] src Input RGB pixels info.
  159. /// @param [in] cnt Input src RGB pixels cnt
  160. /// @param [in] min_distance The min distance(mm), which is generally set to the minimum measured distance of the current camera
  161. /// @param [in] max_distance The longest distance(mm), which is generally set to the longest measuring distance of the current camera
  162. /// @param [out] dst Output RGB pixels info.
  163. /// @retval TY_STATUS_OK Succeed.
  164. static inline TY_STATUS TYMapRGBPixelsToDepthCoordinate(
  165. const TY_CAMERA_CALIB_INFO* depth_calib,
  166. uint32_t depthW, uint32_t depthH, const uint16_t* depth,
  167. const TY_CAMERA_CALIB_INFO* color_calib,
  168. uint32_t rgbW, uint32_t rgbH,
  169. TY_PIXEL_COLOR_DESC* src, uint32_t cnt,
  170. uint32_t min_distance,
  171. uint32_t max_distance,
  172. TY_PIXEL_COLOR_DESC* dst,
  173. float f_scale_unit = 1.0f);
  174. /// @brief Map original RGB image to depth coordinate RGB image.
  175. /// @param [in] depth_calib Depth image's calibration data.
  176. /// @param [in] depthW Width of current depth image.
  177. /// @param [in] depthH Height of current depth image.
  178. /// @param [in] depth Current depth image.
  179. /// @param [in] color_calib Color image's calibration data.
  180. /// @param [in] rgbW Width of RGB image.
  181. /// @param [in] rgbH Height of RGB image.
  182. /// @param [in] inRgb Current RGB image.
  183. /// @param [out] mappedRgb Output RGB image.
  184. /// @retval TY_STATUS_OK Succeed.
  185. static inline TY_STATUS TYMapRGBImageToDepthCoordinate(
  186. const TY_CAMERA_CALIB_INFO* depth_calib,
  187. uint32_t depthW, uint32_t depthH, const uint16_t* depth,
  188. const TY_CAMERA_CALIB_INFO* color_calib,
  189. uint32_t rgbW, uint32_t rgbH, const uint8_t* inRgb,
  190. uint8_t* mappedRgb,
  191. float f_scale_unit = 1.0f);
  192. /// @brief Map original RGB48 image to depth coordinate RGB image.
  193. /// @param [in] depth_calib Depth image's calibration data.
  194. /// @param [in] depthW Width of current depth image.
  195. /// @param [in] depthH Height of current depth image.
  196. /// @param [in] depth Current depth image.
  197. /// @param [in] color_calib Color image's calibration data.
  198. /// @param [in] rgbW Width of RGB48 image.
  199. /// @param [in] rgbH Height of RGB48 image.
  200. /// @param [in] inRgb Current RGB48 image.
  201. /// @param [out] mappedRgb Output RGB48 image.
  202. /// @retval TY_STATUS_OK Succeed.
  203. static inline TY_STATUS TYMapRGB48ImageToDepthCoordinate(
  204. const TY_CAMERA_CALIB_INFO* depth_calib,
  205. uint32_t depthW, uint32_t depthH, const uint16_t* depth,
  206. const TY_CAMERA_CALIB_INFO* color_calib,
  207. uint32_t rgbW, uint32_t rgbH, const uint16_t* inRgb,
  208. uint16_t* mappedRgb,
  209. float f_scale_unit = 1.0f);
  210. /// @brief Map original MONO16 image to depth coordinate MONO16 image.
  211. /// @param [in] depth_calib Depth image's calibration data.
  212. /// @param [in] depthW Width of current depth image.
  213. /// @param [in] depthH Height of current depth image.
  214. /// @param [in] depth Current depth image.
  215. /// @param [in] color_calib Color image's calibration data.
  216. /// @param [in] rgbW Width of MONO16 image.
  217. /// @param [in] rgbH Height of MONO16 image.
  218. /// @param [in] gray Current MONO16 image.
  219. /// @param [out] mappedGray Output MONO16 image.
  220. /// @retval TY_STATUS_OK Succeed.
  221. static inline TY_STATUS TYMapMono16ImageToDepthCoordinate(
  222. const TY_CAMERA_CALIB_INFO* depth_calib,
  223. uint32_t depthW, uint32_t depthH, const uint16_t* depth,
  224. const TY_CAMERA_CALIB_INFO* color_calib,
  225. uint32_t rgbW, uint32_t rgbH, const uint16_t* gray,
  226. uint16_t* mappedGray,
  227. float f_scale_unit = 1.0f);
  228. /// @brief Map original MONO8 image to depth coordinate MONO8 image.
  229. /// @param [in] depth_calib Depth image's calibration data.
  230. /// @param [in] depthW Width of current depth image.
  231. /// @param [in] depthH Height of current depth image.
  232. /// @param [in] depth Current depth image.
  233. /// @param [in] color_calib Color image's calibration data.
  234. /// @param [in] monoW Width of MONO8 image.
  235. /// @param [in] monoH Height of MONO8 image.
  236. /// @param [in] inMono Current MONO8 image.
  237. /// @param [out] mappedMono Output MONO8 image.
  238. /// @retval TY_STATUS_OK Succeed.
  239. static inline TY_STATUS TYMapMono8ImageToDepthCoordinate(
  240. const TY_CAMERA_CALIB_INFO* depth_calib,
  241. uint32_t depthW, uint32_t depthH, const uint16_t* depth,
  242. const TY_CAMERA_CALIB_INFO* color_calib,
  243. uint32_t monoW, uint32_t monoH, const uint8_t* inMono,
  244. uint8_t* mappedMono,
  245. float f_scale_unit = 1.0f);
  246. #define TYMAP_CHECKRET(f, bufToFree) \
  247. do{ \
  248. TY_STATUS err = (f); \
  249. if(err){ \
  250. if(bufToFree) \
  251. free(bufToFree); \
  252. return err; \
  253. } \
  254. } while(0)
  255. static inline TY_STATUS TYMapDepthToColorCoordinate(
  256. const TY_CAMERA_CALIB_INFO* depth_calib,
  257. uint32_t depthW, uint32_t depthH,
  258. const TY_PIXEL_DESC* depth, uint32_t count,
  259. const TY_CAMERA_CALIB_INFO* color_calib,
  260. uint32_t mappedW, uint32_t mappedH,
  261. TY_PIXEL_DESC* mappedDepth,
  262. float f_scale_unit)
  263. {
  264. TY_VECT_3F* p3d = (TY_VECT_3F*)malloc(sizeof(TY_VECT_3F) * count);
  265. TYMAP_CHECKRET(TYMapDepthToPoint3d(depth_calib, depthW, depthH, depth, count, p3d, f_scale_unit), p3d );
  266. TY_CAMERA_EXTRINSIC extri_inv;
  267. TYMAP_CHECKRET(TYInvertExtrinsic(&color_calib->extrinsic, &extri_inv), p3d);
  268. TYMAP_CHECKRET(TYMapPoint3dToPoint3d(&extri_inv, p3d, count, p3d), p3d );
  269. TYMAP_CHECKRET(TYMapPoint3dToDepth(color_calib, p3d, count, mappedW, mappedH, mappedDepth, f_scale_unit), p3d );
  270. free(p3d);
  271. return TY_STATUS_OK;
  272. }
  273. static inline TY_STATUS TYMapDepthImageToColorCoordinate(
  274. const TY_CAMERA_CALIB_INFO* depth_calib,
  275. uint32_t depthW, uint32_t depthH, const uint16_t* depth,
  276. const TY_CAMERA_CALIB_INFO* color_calib,
  277. uint32_t mappedW, uint32_t mappedH, uint16_t* mappedDepth, float f_scale_unit)
  278. {
  279. TY_VECT_3F* p3d = (TY_VECT_3F*)malloc(sizeof(TY_VECT_3F) * depthW * depthH);
  280. TYMAP_CHECKRET(TYMapDepthImageToPoint3d(depth_calib, depthW, depthH, depth, p3d, f_scale_unit), p3d);
  281. TY_CAMERA_EXTRINSIC extri_inv;
  282. TYMAP_CHECKRET(TYInvertExtrinsic(&color_calib->extrinsic, &extri_inv), p3d);
  283. TYMAP_CHECKRET(TYMapPoint3dToPoint3d(&extri_inv, p3d, depthW * depthH, p3d), p3d);
  284. TYMAP_CHECKRET(TYMapPoint3dToDepthImage(
  285. color_calib, p3d, depthW * depthH, mappedW, mappedH, mappedDepth, f_scale_unit), p3d);
  286. free(p3d);
  287. return TY_STATUS_OK;
  288. }
  289. static inline TY_STATUS TYMapRGBPixelsToDepthCoordinate(
  290. const TY_CAMERA_CALIB_INFO* depth_calib,
  291. uint32_t depthW, uint32_t depthH, const uint16_t* depth,
  292. const TY_CAMERA_CALIB_INFO* color_calib,
  293. uint32_t rgbW, uint32_t rgbH,
  294. TY_PIXEL_COLOR_DESC* src, uint32_t cnt,
  295. uint32_t min_distance,
  296. uint32_t max_distance,
  297. TY_PIXEL_COLOR_DESC* dst,
  298. float f_scale_unit)
  299. {
  300. uint32_t m_distance_range = max_distance - min_distance;
  301. TY_CAMERA_EXTRINSIC extri = color_calib->extrinsic;
  302. TY_PIXEL_DESC* pixels_array = (TY_PIXEL_DESC*)malloc(sizeof(TY_PIXEL_DESC) * m_distance_range);
  303. TY_PIXEL_DESC* pixels_mapped_array = (TY_PIXEL_DESC*)malloc(sizeof(TY_PIXEL_DESC) * m_distance_range);
  304. TY_VECT_3F* p3d_array = (TY_VECT_3F*)malloc(sizeof(TY_VECT_3F) * m_distance_range);
  305. for (uint32_t i = 0; i < cnt; i++) {
  306. for (uint32_t m = 0; m < m_distance_range; m++) {
  307. pixels_array[m].x = src[i].x;
  308. pixels_array[m].y = src[i].y;
  309. pixels_array[m].depth = m + min_distance;
  310. }
  311. TYMapDepthToPoint3d(color_calib, rgbW, rgbH, pixels_array, m_distance_range, &p3d_array[0], f_scale_unit);
  312. TYMapPoint3dToPoint3d(&extri, &p3d_array[0], m_distance_range, &p3d_array[0]);
  313. TYMapPoint3dToDepth(depth_calib, p3d_array, m_distance_range, depthW, depthH, pixels_mapped_array, f_scale_unit);
  314. uint16_t m_min_delt = 0xffff;
  315. dst[i].x = -1;
  316. dst[i].y = -1;
  317. for (uint32_t m = 0; m < m_distance_range; m++) {
  318. int16_t pixel_x = pixels_mapped_array[m].x;
  319. int16_t pixel_y = pixels_mapped_array[m].y;
  320. uint16_t delt = abs(pixels_mapped_array[m].depth - depth[pixel_y*depthW + pixel_x]);
  321. if (delt < m_min_delt) {
  322. m_min_delt = delt;
  323. if (m_min_delt < 10) {
  324. dst[i].x = pixel_x;
  325. dst[i].y = pixel_y;
  326. dst[i].bgr_ch1 = src[i].bgr_ch1;
  327. dst[i].bgr_ch2 = src[i].bgr_ch2;
  328. dst[i].bgr_ch3 = src[i].bgr_ch3;
  329. }
  330. }
  331. }
  332. }
  333. free(pixels_array);
  334. free(pixels_mapped_array);
  335. free(p3d_array);
  336. return TY_STATUS_OK;
  337. }
  338. static inline TY_STATUS TYCreateDepthToColorCoordinateLookupTable(
  339. const TY_CAMERA_CALIB_INFO* depth_calib,
  340. uint32_t depthW, uint32_t depthH, const uint16_t* depth,
  341. const TY_CAMERA_CALIB_INFO* color_calib,
  342. uint32_t mappedW, uint32_t mappedH,
  343. TY_PIXEL_DESC* lut,
  344. float f_scale_unit)
  345. {
  346. TY_VECT_3F* p3d = (TY_VECT_3F*)malloc(sizeof(TY_VECT_3F) * depthW * depthH);
  347. TYMAP_CHECKRET(TYMapDepthImageToPoint3d(depth_calib, depthW, depthH, depth, p3d, f_scale_unit), p3d);
  348. TY_CAMERA_EXTRINSIC extri_inv;
  349. TYMAP_CHECKRET(TYInvertExtrinsic(&color_calib->extrinsic, &extri_inv), p3d);
  350. TYMAP_CHECKRET(TYMapPoint3dToPoint3d(&extri_inv, p3d, depthW * depthH, p3d), p3d);
  351. TYMAP_CHECKRET(TYMapPoint3dToDepth(color_calib, p3d, depthW * depthH, mappedW, mappedH, lut, f_scale_unit), p3d );
  352. free(p3d);
  353. return TY_STATUS_OK;
  354. }
  355. static inline TY_STATUS TYMapRGBImageToDepthCoordinate(
  356. const TY_CAMERA_CALIB_INFO* depth_calib,
  357. uint32_t depthW, uint32_t depthH, const uint16_t* depth,
  358. const TY_CAMERA_CALIB_INFO* color_calib,
  359. uint32_t rgbW, uint32_t rgbH, const uint8_t* inRgb,
  360. uint8_t* mappedRgb, float f_scale_unit)
  361. {
  362. TY_PIXEL_DESC* lut = (TY_PIXEL_DESC*)malloc(sizeof(TY_PIXEL_DESC) * depthW * depthH);
  363. TYMAP_CHECKRET(TYCreateDepthToColorCoordinateLookupTable(
  364. depth_calib, depthW, depthH, depth,
  365. color_calib, rgbW, rgbH, lut, f_scale_unit), lut);
  366. for(uint32_t depthr = 0; depthr < depthH; depthr++)
  367. for(uint32_t depthc = 0; depthc < depthW; depthc++)
  368. {
  369. TY_PIXEL_DESC* plut = &lut[depthr * depthW + depthc];
  370. uint8_t* outPtr = &mappedRgb[depthW * depthr * 3 + depthc * 3];
  371. if(plut->x < 0 || plut->x >= (int)rgbW || plut->y < 0 || plut->y >= (int)rgbH){
  372. outPtr[0] = outPtr[1] = outPtr[2] = 0;
  373. } else {
  374. const uint8_t* inPtr = &inRgb[rgbW * plut->y * 3 + plut->x * 3];
  375. outPtr[0] = inPtr[0];
  376. outPtr[1] = inPtr[1];
  377. outPtr[2] = inPtr[2];
  378. }
  379. }
  380. free(lut);
  381. return TY_STATUS_OK;
  382. }
  383. static inline TY_STATUS TYMapRGB48ImageToDepthCoordinate(
  384. const TY_CAMERA_CALIB_INFO* depth_calib,
  385. uint32_t depthW, uint32_t depthH, const uint16_t* depth,
  386. const TY_CAMERA_CALIB_INFO* color_calib,
  387. uint32_t rgbW, uint32_t rgbH, const uint16_t* inRgb,
  388. uint16_t* mappedRgb, float f_scale_unit)
  389. {
  390. TY_PIXEL_DESC* lut = (TY_PIXEL_DESC*)malloc(sizeof(TY_PIXEL_DESC) * depthW * depthH);
  391. TYMAP_CHECKRET(TYCreateDepthToColorCoordinateLookupTable(
  392. depth_calib, depthW, depthH, depth,
  393. color_calib, rgbW, rgbH, lut, f_scale_unit), lut);
  394. for(uint32_t depthr = 0; depthr < depthH; depthr++)
  395. for(uint32_t depthc = 0; depthc < depthW; depthc++)
  396. {
  397. TY_PIXEL_DESC* plut = &lut[depthr * depthW + depthc];
  398. uint16_t* outPtr = &mappedRgb[depthW * depthr * 3 + depthc * 3];
  399. if(plut->x < 0 || plut->x >= (int)rgbW || plut->y < 0 || plut->y >= (int)rgbH){
  400. outPtr[0] = outPtr[1] = outPtr[2] = 0;
  401. } else {
  402. const uint16_t* inPtr = &inRgb[rgbW * plut->y * 3 + plut->x * 3];
  403. outPtr[0] = inPtr[0];
  404. outPtr[1] = inPtr[1];
  405. outPtr[2] = inPtr[2];
  406. }
  407. }
  408. free(lut);
  409. return TY_STATUS_OK;
  410. }
  411. static inline TY_STATUS TYMapMono16ImageToDepthCoordinate(
  412. const TY_CAMERA_CALIB_INFO* depth_calib,
  413. uint32_t depthW, uint32_t depthH, const uint16_t* depth,
  414. const TY_CAMERA_CALIB_INFO* color_calib,
  415. uint32_t rgbW, uint32_t rgbH, const uint16_t* gray,
  416. uint16_t* mappedGray, float f_scale_unit)
  417. {
  418. TY_PIXEL_DESC* lut = (TY_PIXEL_DESC*)malloc(sizeof(TY_PIXEL_DESC) * depthW * depthH);
  419. TYMAP_CHECKRET(TYCreateDepthToColorCoordinateLookupTable(
  420. depth_calib, depthW, depthH, depth,
  421. color_calib, rgbW, rgbH, lut, f_scale_unit), lut);
  422. for(uint32_t depthr = 0; depthr < depthH; depthr++)
  423. for(uint32_t depthc = 0; depthc < depthW; depthc++)
  424. {
  425. TY_PIXEL_DESC* plut = &lut[depthr * depthW + depthc];
  426. uint16_t* outPtr = &mappedGray[depthW * depthr + depthc];
  427. if(plut->x < 0 || plut->x >= (int)rgbW || plut->y < 0 || plut->y >= (int)rgbH){
  428. outPtr[0] = 0;
  429. } else {
  430. const uint16_t* inPtr = &gray[rgbW * plut->y + plut->x];
  431. outPtr[0] = inPtr[0];
  432. }
  433. }
  434. free(lut);
  435. return TY_STATUS_OK;
  436. }
  437. static inline TY_STATUS TYMapMono8ImageToDepthCoordinate(
  438. const TY_CAMERA_CALIB_INFO* depth_calib,
  439. uint32_t depthW, uint32_t depthH, const uint16_t* depth,
  440. const TY_CAMERA_CALIB_INFO* color_calib,
  441. uint32_t monoW, uint32_t monoH, const uint8_t* inMono,
  442. uint8_t* mappedMono, float f_scale_unit)
  443. {
  444. TY_PIXEL_DESC* lut = (TY_PIXEL_DESC*)malloc(sizeof(TY_PIXEL_DESC) * depthW * depthH);
  445. TYMAP_CHECKRET(TYCreateDepthToColorCoordinateLookupTable(
  446. depth_calib, depthW, depthH, depth,
  447. color_calib, monoW, monoH, lut, f_scale_unit), lut);
  448. for(uint32_t depthr = 0; depthr < depthH; depthr++)
  449. for(uint32_t depthc = 0; depthc < depthW; depthc++)
  450. {
  451. TY_PIXEL_DESC* plut = &lut[depthr * depthW + depthc];
  452. uint8_t* outPtr = &mappedMono[depthW * depthr + depthc];
  453. if(plut->x < 0 || plut->x >= (int)monoW || plut->y < 0 || plut->y >= (int)monoH){
  454. outPtr[0] = 0;
  455. } else {
  456. const uint8_t* inPtr = &inMono[monoW * plut->y + plut->x];
  457. outPtr[0] = inPtr[0];
  458. }
  459. }
  460. free(lut);
  461. return TY_STATUS_OK;
  462. }
  463. #endif