VPI - Vision Programming Interface

3.1 Release

OpenCVUtils.hpp
1 /*
2  * Copyright 2020 NVIDIA Corporation. All rights reserved.
3  *
4  * NOTICE TO LICENSEE:
5  *
6  * This source code and/or documentation ("Licensed Deliverables") are
7  * subject to NVIDIA intellectual property rights under U.S. and
8  * international Copyright laws.
9  *
10  * These Licensed Deliverables contained herein is PROPRIETARY and
11  * CONFIDENTIAL to NVIDIA and is being provided under the terms and
12  * conditions of a form of NVIDIA software license agreement by and
13  * between NVIDIA and Licensee ("License Agreement") or electronically
14  * accepted by Licensee. Notwithstanding any terms or conditions to
15  * the contrary in the License Agreement, reproduction or disclosure
16  * of the Licensed Deliverables to any third party without the express
17  * written consent of NVIDIA is prohibited.
18  *
19  * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE
20  * LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE
21  * SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE. IT IS
22  * PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND.
23  * NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED
24  * DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY,
25  * NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE.
26  * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE
27  * LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY
28  * SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY
29  * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
30  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
31  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
32  * OF THESE LICENSED DELIVERABLES.
33  *
34  * U.S. Government End Users. These Licensed Deliverables are a
35  * "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT
36  * 1995), consisting of "commercial computer software" and "commercial
37  * computer software documentation" as such terms are used in 48
38  * C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government
39  * only as a commercial end item. Consistent with 48 C.F.R.12.212 and
40  * 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all
41  * U.S. Government End Users acquire the Licensed Deliverables with
42  * only those rights set forth herein.
43  *
44  * Any use of the Licensed Deliverables in individual and commercial
45  * software must include, in the user documentation and internal
46  * comments to the code, the above Disclaimer and U.S. Government End
47  * Users Notice.
48  */
49 
50 #ifndef NV_VPI_DETAIL_OPENCV_UTILS_HPP
51 #define NV_VPI_DETAIL_OPENCV_UTILS_HPP
52 
53 #include "../Image.h"
54 #include "../ImageFormat.h"
55 #include "../Status.h"
56 
57 #include <opencv2/core/core.hpp>
58 
59 #include <stdint.h>
60 
61 #include <cassert>
62 #include <cstring>
63 #include <vector>
64 
65 namespace nv { namespace vpi { namespace detail {
66 
67 inline VPIImageFormat ToImageFormatFromOpenCVType(int cvType)
68 {
70 
71  switch (CV_MAT_DEPTH(cvType))
72  {
73  case CV_8U:
74  switch (CV_MAT_CN(cvType))
75  {
76  case 1:
77  imgFormat = VPI_IMAGE_FORMAT_U8;
78  break;
79 
80  case 2:
81  imgFormat = VPI_MAKE_NONCOLOR_IMAGE_FORMAT_ABBREV(PL, UNSIGNED, XY00, 1, X8_Y8);
82  break;
83 
84  case 3:
85  imgFormat = VPI_IMAGE_FORMAT_BGR8;
86  break;
87 
88  case 4:
89  imgFormat = VPI_IMAGE_FORMAT_BGRA8;
90  break;
91  }
92  break;
93 
94  case CV_8S:
95  switch (CV_MAT_CN(cvType))
96  {
97  case 1:
98  imgFormat = VPI_IMAGE_FORMAT_S8;
99  break;
100 
101  case 2:
102  imgFormat = VPI_MAKE_NONCOLOR_IMAGE_FORMAT_ABBREV(PL, SIGNED, XY00, 1, X8_Y8);
103  break;
104 
105  case 3:
106  imgFormat = VPI_MAKE_NONCOLOR_IMAGE_FORMAT_ABBREV(PL, SIGNED, XYZ0, 1, X8_Y8_Z8);
107  break;
108 
109  case 4:
110  imgFormat = VPI_MAKE_NONCOLOR_IMAGE_FORMAT_ABBREV(PL, SIGNED, XYZW, 1, X8_Y8_Z8_W8);
111  break;
112  }
113  break;
114 
115  case CV_16U:
116  switch (CV_MAT_CN(cvType))
117  {
118  case 1:
119  imgFormat = VPI_IMAGE_FORMAT_U16;
120  break;
121 
122  case 2:
123  imgFormat = VPI_MAKE_NONCOLOR_IMAGE_FORMAT_ABBREV(PL, UNSIGNED, XY00, 1, X16_Y16);
124  break;
125 
126  case 3:
127  // BGR
128  imgFormat = VPI_MAKE_COLOR_IMAGE_FORMAT_ABBREV(RGB, UNDEFINED, PL, UNSIGNED, ZYX1, 1, X16_Y16_Z16);
129  break;
130 
131  case 4:
132  // ABGR
133  imgFormat = VPI_MAKE_COLOR_IMAGE_FORMAT_ABBREV(RGB, UNDEFINED, PL, UNSIGNED, WZYX, 1, X16_Y16_Z16_W16);
134  break;
135  }
136  break;
137 
138  case CV_16S:
139  switch (CV_MAT_CN(cvType))
140  {
141  case 1:
142  imgFormat = VPI_IMAGE_FORMAT_S16;
143  break;
144 
145  case 2:
146  imgFormat = VPI_MAKE_NONCOLOR_IMAGE_FORMAT_ABBREV(PL, SIGNED, XY00, 1, X16_Y16);
147  break;
148 
149  case 3:
150  imgFormat = VPI_MAKE_NONCOLOR_IMAGE_FORMAT_ABBREV(PL, SIGNED, XYZ0, 1, X16_Y16_Z16);
151  break;
152 
153  case 4:
154  imgFormat = VPI_MAKE_NONCOLOR_IMAGE_FORMAT_ABBREV(PL, SIGNED, XYZW, 1, X16_Y16_Z16_W16);
155  break;
156  }
157  break;
158 
159  case CV_32S:
160  switch (CV_MAT_CN(cvType))
161  {
162  case 1:
163  imgFormat = VPI_MAKE_NONCOLOR_IMAGE_FORMAT_ABBREV(PL, SIGNED, X000, 1, X32);
164  break;
165 
166  case 2:
167  imgFormat = VPI_MAKE_NONCOLOR_IMAGE_FORMAT_ABBREV(PL, SIGNED, XY00, 1, X32_Y32);
168  break;
169 
170  case 3:
171  imgFormat = VPI_MAKE_NONCOLOR_IMAGE_FORMAT_ABBREV(PL, SIGNED, XYZ0, 1, X32_Y32_Z32);
172  break;
173 
174  case 4:
175  imgFormat = VPI_MAKE_NONCOLOR_IMAGE_FORMAT_ABBREV(PL, SIGNED, XYZW, 1, X32_Y32_Z32_W32);
176  break;
177  }
178  break;
179 
180  case CV_32F:
181  switch (CV_MAT_CN(cvType))
182  {
183  case 1:
184  imgFormat = VPI_IMAGE_FORMAT_F32;
185  break;
186 
187  case 2:
188  imgFormat = VPI_IMAGE_FORMAT_2F32;
189  break;
190 
191  case 3:
192  imgFormat = VPI_MAKE_COLOR_IMAGE_FORMAT_ABBREV(RGB, UNDEFINED, PL, FLOAT, ZYX1, 1, X32_Y32_Z32);
193  break;
194 
195  case 4:
196  imgFormat = VPI_MAKE_COLOR_IMAGE_FORMAT_ABBREV(RGB, UNDEFINED, PL, FLOAT, WZYX, 1, X32_Y32_Z32_W32);
197  break;
198  }
199  break;
200 
201  case CV_64F:
202  switch (CV_MAT_CN(cvType))
203  {
204  case 1:
205  imgFormat = VPI_MAKE_NONCOLOR_IMAGE_FORMAT_ABBREV(PL, FLOAT, X000, 1, X64);
206  break;
207 
208  case 2:
209  imgFormat = VPI_MAKE_NONCOLOR_IMAGE_FORMAT_ABBREV(PL, FLOAT, XY00, 1, X64_Y64);
210  break;
211 
212  case 3:
213  imgFormat = VPI_MAKE_COLOR_IMAGE_FORMAT_ABBREV(RGB, UNDEFINED, PL, FLOAT, ZYX1, 1, X64_Y64_Z64);
214  break;
215 
216  case 4:
217  imgFormat = VPI_MAKE_COLOR_IMAGE_FORMAT_ABBREV(RGB, UNDEFINED, PL, FLOAT, WZYX, 1, X64_Y64_Z64_W64);
218  break;
219  }
220  break;
221  }
222 
223  return imgFormat;
224 }
225 
226 inline int ToOpenCVType(VPIPixelType pixType)
227 {
229  {
230  return -1;
231  }
232 
233  int depth = -1;
234  VPIDataType dataType = vpiPixelTypeGetDataType(pixType);
235  int elemSize = 0;
236 
237  if (dataType == VPI_DATA_TYPE_UNSIGNED)
238  {
239  switch (vpiPixelTypeGetPacking(pixType))
240  {
241  case VPI_PACKING_X8:
242  case VPI_PACKING_X8_Y8:
247  depth = CV_8U;
248  elemSize = 8;
249  break;
250 
251  case VPI_PACKING_X16:
252  case VPI_PACKING_X16_Y16:
255  depth = CV_16U;
256  elemSize = 16;
257  break;
258 
259  default:
260  return -1;
261  }
262  }
263  else if (dataType == VPI_DATA_TYPE_SIGNED)
264  {
265  switch (vpiPixelTypeGetPacking(pixType))
266  {
267  case VPI_PACKING_X8:
268  case VPI_PACKING_X8_Y8:
273  depth = CV_8S;
274  elemSize = 8;
275  break;
276 
277  case VPI_PACKING_X16:
278  case VPI_PACKING_X16_Y16:
281  depth = CV_16S;
282  elemSize = 16;
283  break;
284 
285  case VPI_PACKING_X32:
286  case VPI_PACKING_X32_Y32:
289  depth = CV_32S;
290  elemSize = 32;
291  break;
292 
293  default:
294  return -1;
295  }
296  }
297  else if (dataType == VPI_DATA_TYPE_FLOAT)
298  {
299  switch (vpiPixelTypeGetPacking(pixType))
300  {
301  case VPI_PACKING_X32:
302  case VPI_PACKING_X32_Y32:
305  depth = CV_32F;
306  elemSize = 32;
307  break;
308 
309  case VPI_PACKING_X64:
310  case VPI_PACKING_X64_Y64:
313  depth = CV_64F;
314  elemSize = 64;
315  break;
316 
317  default:
318  return -1;
319  }
320  }
321  else
322  {
323  return -1;
324  }
325 
326  assert(elemSize != 0);
327 
328  return CV_MAKE_TYPE(depth, vpiPixelTypeGetBitsPerPixel(pixType) / elemSize);
329 }
330 
331 inline VPIStatus FillImageDataOnePlaneImpl(const cv::Mat &mat, VPIImageFormat fmt, VPIImageBufferPitchLinear *imgData)
332 {
334 
335  if (ToOpenCVType(pixType) != mat.type())
336  {
338  }
339 
340  assert(imgData != nullptr);
341 
342  VPIImagePlanePitchLinear &imgPlane = imgData->planes[0];
343 
344  imgPlane.width = mat.cols;
345  imgPlane.height = mat.rows;
346  imgPlane.pitchBytes = mat.step[0];
347  imgPlane.data = mat.data;
348  imgPlane.pixelType = pixType;
349  return VPI_SUCCESS;
350 }
351 
352 inline VPIStatus FillImageDataTwoPlanesImpl(const cv::Mat &mat, VPIImageFormat fmt, VPIImageBufferPitchLinear *imgData)
353 {
355 
357  {
359  }
360 
361  if (mat.type() == CV_8UC1)
362  {
365  {
367  }
368  }
369  else if (mat.type() == CV_16UC1)
370  {
373  {
375  }
376  }
377  else
378  {
380  }
381 
382  assert(imgData != nullptr);
383 
384  VPIImagePlanePitchLinear *imgPlanes = imgData->planes;
385 
386  imgPlanes[0].pixelType = vpiImageFormatGetPlanePixelType(fmt, 0);
387  imgPlanes[1].pixelType = vpiImageFormatGetPlanePixelType(fmt, 1);
388 
389  imgPlanes[0].width = mat.cols;
390  imgPlanes[1].width = mat.cols / 2;
391 
393  {
394  case 4:
395  imgPlanes[0].height = mat.rows / 2;
396  break;
397 
398  case 2:
399  imgPlanes[0].height = mat.rows * 2 / 3;
400  break;
401 
402  case 1:
403  imgPlanes[0].height = mat.rows * 4 / 5;
404  break;
405 
406  default:
408  }
409 
410  imgPlanes[1].height = mat.rows - imgPlanes[0].height;
411 
412  imgPlanes[0].pitchBytes = mat.step[0];
413  imgPlanes[1].pitchBytes = mat.step[0];
414 
415  imgPlanes[0].data = mat.data;
416  imgPlanes[1].data = const_cast<uint8_t *>(mat.ptr(imgPlanes[0].height, 0));
417 
418  return VPI_SUCCESS;
419 }
420 
421 inline VPIStatus FillImageDataThreePlanesImpl(const cv::Mat &mat, VPIImageFormat fmt,
422  VPIImageBufferPitchLinear *imgData)
423 {
425 
427  {
429  }
430 
431  // column count must be even
432  if (mat.cols & 1)
433  {
435  }
436 
437  switch (mat.type())
438  {
439  case CV_8UC1:
440  // must have packed rows
441  if (mat.step[0] != (size_t)mat.cols)
442  {
444  }
445 
446  for (int p = 0; p < 3; ++p)
447  {
449  {
451  }
452  }
453  break;
454 
455  case CV_16UC1:
456  // must have packed rows
457  if (mat.step[0] != (size_t)mat.cols * 2)
458  {
460  }
461 
462  for (int p = 0; p < 3; ++p)
463  {
465  {
467  }
468  }
469  break;
470 
471  default:
473  }
474 
475  assert(imgData != nullptr);
476 
477  VPIImagePlanePitchLinear *imgPlanes = imgData->planes;
478 
479  imgPlanes[0].pixelType = vpiImageFormatGetPlanePixelType(fmt, 0);
480  imgPlanes[1].pixelType = vpiImageFormatGetPlanePixelType(fmt, 1);
481  imgPlanes[2].pixelType = vpiImageFormatGetPlanePixelType(fmt, 2);
482 
483  imgPlanes[0].width = mat.cols;
484  imgPlanes[1].width = mat.cols / 2;
485  imgPlanes[2].width = mat.cols / 2;
486 
488  {
489  case 4:
490  imgPlanes[0].height = mat.rows / 2;
491  break;
492  case 2:
493  imgPlanes[0].height = mat.rows * 2 / 3;
494  break;
495  case 1:
496  imgPlanes[0].height = mat.rows * 4 / 5;
497  break;
498  default:
500  }
501 
502  imgPlanes[1].height = mat.rows - imgPlanes[0].height;
503  imgPlanes[2].height = imgPlanes[1].height;
504 
505  imgPlanes[0].pitchBytes = mat.step[0];
506  imgPlanes[1].pitchBytes = mat.step[0] / 2;
507  imgPlanes[2].pitchBytes = mat.step[0] / 2;
508 
509  imgPlanes[0].data = mat.data;
510  imgPlanes[1].data = mat.data + mat.step[0] * imgPlanes[0].height;
511  imgPlanes[2].data = reinterpret_cast<uint8_t *>(imgPlanes[1].data) + imgPlanes[1].height * imgPlanes[1].pitchBytes;
512 
513  return VPI_SUCCESS;
514 }
515 
516 inline VPIStatus FillImageData(const cv::Mat &mat, VPIImageFormat fmt, VPIImageData *imgData)
517 {
518  assert(imgData != NULL);
519 
520  if (mat.data == NULL)
521  {
523  }
524 
525  if (mat.dims != 2)
526  {
528  }
529 
531  {
533  }
534 
535  int numPlanes = vpiImageFormatGetPlaneCount(fmt);
536 
537  memset(imgData, 0, sizeof(*imgData));
538 
540  imgData->buffer.pitch.format = fmt;
541  imgData->buffer.pitch.numPlanes = numPlanes;
542 
543  VPIStatus status;
544  switch (numPlanes)
545  {
546  case 1:
547  status = FillImageDataOnePlaneImpl(mat, fmt, &imgData->buffer.pitch);
548  break;
549 
550  case 2:
551  status = FillImageDataTwoPlanesImpl(mat, fmt, &imgData->buffer.pitch);
552  break;
553 
554  case 3:
555  status = FillImageDataThreePlanesImpl(mat, fmt, &imgData->buffer.pitch);
556  break;
557 
558  default:
559  status = VPI_ERROR_INTERNAL;
560  break;
561  }
562 
563  return status;
564 }
565 
566 inline VPIStatus FillImageData(const std::vector<cv::Mat> &matPlanes, VPIImageFormat fmt, VPIImageData *imgData)
567 {
568  assert(imgData != NULL);
569 
570  if (matPlanes.empty())
571  {
573  }
574 
575  if (matPlanes.size() != (size_t)vpiImageFormatGetPlaneCount(fmt))
576  {
578  }
579 
580  memset(imgData, 0, sizeof(*imgData));
581 
583  imgData->buffer.pitch.format = fmt;
584  imgData->buffer.pitch.numPlanes = matPlanes.size();
585 
586  for (size_t p = 0; p < matPlanes.size(); ++p)
587  {
588  const cv::Mat &mat = matPlanes[p];
589 
590  if (mat.data == NULL)
591  {
593  }
594 
595  if (mat.dims != 2)
596  {
598  }
599 
601 
602  if (ToOpenCVType(pixType) != mat.type())
603  {
605  }
606 
607  VPIImagePlanePitchLinear &plane = imgData->buffer.pitch.planes[p];
608 
609  plane.width = mat.cols;
610  plane.height = mat.rows;
611  plane.pitchBytes = mat.step[0];
612  plane.data = mat.data;
613  plane.pixelType = pixType;
614  }
615 
616  return VPI_SUCCESS;
617 }
618 
619 inline VPIStatus FillImageData(const cv::Mat &mat, VPIImageData *imgData)
620 {
621  assert(imgData != NULL);
622 
623  if (mat.data == NULL)
624  {
626  }
627 
628  if (mat.dims != 2)
629  {
631  }
632 
633  VPIImageFormat fmt = ToImageFormatFromOpenCVType(mat.type());
634  if (fmt == VPI_IMAGE_FORMAT_INVALID)
635  {
637  }
638 
639  assert(imgData != nullptr);
640 
642  VPIImageBufferPitchLinear &imgPitch = imgData->buffer.pitch;
643 
644  imgPitch.format = fmt;
645  imgPitch.numPlanes = 1;
646  imgPitch.planes[0].width = mat.cols;
647  imgPitch.planes[0].height = mat.rows;
648  imgPitch.planes[0].pitchBytes = mat.step[0];
649  imgPitch.planes[0].data = mat.data;
650  imgPitch.planes[0].pixelType = vpiImageFormatGetPlanePixelType(fmt, 0);
651 
652  return VPI_SUCCESS;
653 }
654 
655 inline VPIStatus CalcHeightOpenCVMatOnePlane(const VPIImageBufferPitchLinear &imgData, int &height)
656 {
657  height = imgData.planes[0].height;
658  return VPI_SUCCESS;
659 }
660 
661 inline VPIStatus CalcHeightOpenCVMatTwoPlanes(const VPIImageBufferPitchLinear &imgData, int &height)
662 {
664 
666  {
668  }
669 
670  if (imgData.planes[0].pitchBytes != imgData.planes[1].pitchBytes)
671  {
673  }
674 
675  if (imgData.planes[1].width * 2 != imgData.planes[0].width)
676  {
678  }
679 
680  if (imgData.planes[1].data !=
681  reinterpret_cast<uint8_t *>(imgData.planes[0].data) + imgData.planes[0].height * imgData.planes[0].pitchBytes)
682  {
684  }
685 
687  {
688  case 1:
689  case 2:
690  case 4:
691  height = imgData.planes[0].height + imgData.planes[1].height;
692  break;
693 
694  default:
696  }
697 
698  return VPI_SUCCESS;
699 }
700 
701 inline VPIStatus CalcHeightOpenCVMatThreePlanes(const VPIImageBufferPitchLinear &imgData, int &height)
702 {
704 
705  // All planes must have packed rows
706  for (int i = 0; i < 3; ++i)
707  {
708  if (imgData.planes[i].pitchBytes !=
709  imgData.planes[i].width * vpiPixelTypeGetBitsPerPixel(imgData.planes[i].pixelType) / 8)
710  {
712  }
713  }
714 
715  if (imgData.planes[0].pitchBytes != imgData.planes[1].pitchBytes * 2 ||
716  imgData.planes[1].pitchBytes != imgData.planes[2].pitchBytes)
717  {
719  }
720 
721  int samplesVert = vpiChromaSubsamplingGetSamplesVert(css);
722 
723  if ((imgData.planes[1].height * 4 + samplesVert - 1) / samplesVert != imgData.planes[0].height)
724  {
726  }
727 
728  if (imgData.planes[2].height != imgData.planes[1].height)
729  {
731  }
732 
733  for (int i = 0; i < 2; ++i)
734  {
735  if (imgData.planes[i + 1].data != reinterpret_cast<uint8_t *>(imgData.planes[i].data) +
736  imgData.planes[i].height * imgData.planes[i].pitchBytes)
737  {
739  }
740  }
741 
743  {
744  case 1:
745  case 2:
746  case 4:
747  height = imgData.planes[0].height + imgData.planes[1].height;
748  break;
749 
750  default:
752  }
753 
754  return VPI_SUCCESS;
755 }
756 
757 inline VPIStatus ExportOpenCVMat(const VPIImageData &imgData, cv::Mat &mat)
758 {
760  {
762  }
763 
764  const VPIImageBufferPitchLinear &imgPitch = imgData.buffer.pitch;
765 
766  VPIStatus status;
767  int height;
768  switch (imgPitch.numPlanes)
769  {
770  case 1:
771  status = CalcHeightOpenCVMatOnePlane(imgPitch, height);
772  break;
773 
774  case 2:
775  status = CalcHeightOpenCVMatTwoPlanes(imgPitch, height);
776  break;
777 
778  case 3:
779  status = CalcHeightOpenCVMatThreePlanes(imgPitch, height);
780  break;
781 
782  default:
784  }
785 
786  if (status == VPI_SUCCESS)
787  {
788  VPIPixelType pixType = imgPitch.planes[0].pixelType;
789 
790  if (pixType == VPI_PIXEL_TYPE_DEFAULT)
791  {
792  pixType = vpiImageFormatGetPlanePixelType(imgPitch.format, 0);
793  if (pixType == VPI_PIXEL_TYPE_INVALID)
794  {
796  }
797  }
798 
799  int cvType = ToOpenCVType(pixType);
800  if (cvType == -1)
801  {
803  }
804 
805  mat = cv::Mat(height, imgPitch.planes[0].width, cvType, imgPitch.planes[0].data, imgPitch.planes[0].pitchBytes);
806  }
807 
808  return status;
809 }
810 
811 inline VPIStatus ExportOpenCVMatPlanes(const VPIImageData &imgData, std::vector<cv::Mat> &mat)
812 {
814  {
816  }
817 
818  const VPIImageBufferPitchLinear &imgPitch = imgData.buffer.pitch;
819 
820  if (imgPitch.numPlanes > VPI_MAX_PLANE_COUNT)
821  {
823  }
824 
825  int cvTypes[VPI_MAX_PLANE_COUNT];
826  for (int i = 0; i < imgPitch.numPlanes; ++i)
827  {
828  VPIPixelType pixType = imgPitch.planes[i].pixelType;
829 
830  if (pixType == VPI_PIXEL_TYPE_DEFAULT)
831  {
832  pixType = vpiImageFormatGetPlanePixelType(imgPitch.format, i);
833  if (pixType == VPI_PIXEL_TYPE_INVALID)
834  {
836  }
837  }
838 
839  cvTypes[i] = ToOpenCVType(pixType);
840 
841  if (cvTypes[i] == -1)
842  {
844  }
845  }
846 
847  mat.resize(imgPitch.numPlanes);
848 
849  for (int i = 0; i < imgPitch.numPlanes; ++i)
850  {
851  mat[i] = cv::Mat(imgPitch.planes[i].height, imgPitch.planes[i].width, cvTypes[i], imgPitch.planes[i].data,
852  imgPitch.planes[i].pitchBytes);
853  }
854 
855  return VPI_SUCCESS;
856 }
857 
858 }}} // namespace nv::vpi::detail
859 
860 #endif // NV_VPI_DETAIL_OPENCV_UTILS_HPP
VPIPixelType vpiImageFormatGetPlanePixelType(VPIImageFormat fmt, int plane)
Get the pixel type of image format's plane.
#define VPI_IMAGE_FORMAT_BGR8
Single plane with interleaved BGR 8-bit channel.
Definition: ImageFormat.h:290
#define VPI_IMAGE_FORMAT_2F32
Single plane with two interleaved 32-bit floating point channels.
Definition: ImageFormat.h:136
#define VPI_IMAGE_FORMAT_F32
Single plane with one 32-bit floating point channel.
Definition: ImageFormat.h:130
VPIChromaSubsampling vpiImageFormatGetChromaSubsampling(VPIImageFormat fmt)
Get the image format's chroma subsampling type.
#define VPI_IMAGE_FORMAT_BGRA8
Single plane with interleaved BGRA 8-bit channel.
Definition: ImageFormat.h:296
VPIMemLayout vpiImageFormatGetMemLayout(VPIImageFormat fmt)
Get the image format's memory layout.
VPIPacking vpiImageFormatGetPlanePacking(VPIImageFormat fmt, int plane)
Get the packing for a given plane of an image format.
#define VPI_IMAGE_FORMAT_INVALID
Denotes an invalid image format.
Definition: ImageFormat.h:97
#define VPI_IMAGE_FORMAT_U16
Single plane with one 16-bit unsigned integer channel.
Definition: ImageFormat.h:109
#define VPI_IMAGE_FORMAT_S16
Single plane with one 16-bit signed integer channel.
Definition: ImageFormat.h:118
#define VPI_MAKE_COLOR_IMAGE_FORMAT_ABBREV(colorModel, colorSpec, memLayout, dataType, swizzle, numPlanes, packing0, packing1,...)
Creates a user-defined color image format constant using abbreviated parameters.
Definition: ImageFormat.h:389
#define VPI_MAKE_NONCOLOR_IMAGE_FORMAT_ABBREV(memLayout, dataType, swizzle, numPlanes, packing0, packing1,...)
Creates a user-defined non-color image format constant using abbreviated parameters.
Definition: ImageFormat.h:444
#define VPI_IMAGE_FORMAT_S8
Single plane with one 8-bit signed integer channel.
Definition: ImageFormat.h:106
#define VPI_IMAGE_FORMAT_U8
Single plane with one 8-bit unsigned integer channel.
Definition: ImageFormat.h:100
int vpiImageFormatGetPlaneCount(VPIImageFormat fmt)
Get the number of planes of an image format.
VPIMemLayout vpiPixelTypeGetMemLayout(VPIPixelType type)
Get the memory layout of a pixel type.
#define VPI_PIXEL_TYPE_INVALID
Signal format conversion errors.
Definition: PixelType.h:85
VPIPacking vpiPixelTypeGetPacking(VPIPixelType type)
Get the packing of a pixel type.
VPIDataType vpiPixelTypeGetDataType(VPIPixelType type)
Get the data type of a pixel type.
int vpiPixelTypeGetBitsPerPixel(VPIPixelType type)
Get the number of bits per pixel of a pixel type.
#define VPI_PIXEL_TYPE_DEFAULT
Used to signal that the pixel type must be inferred from image format.
Definition: PixelType.h:83
VPIChromaSubsampling
Defines how chroma-subsampling is done.
Definition: ColorSpec.h:400
int vpiChromaSubsamplingGetSamplesVert(VPIChromaSubsampling css)
Get the number of chroma samples for each group of 4 vertical luma samples.
int vpiChromaSubsamplingGetSamplesHoriz(VPIChromaSubsampling css)
Get the number of chroma samples for each group of 4 horizontal luma samples.
VPIDataType
Defines the channel data type.
Definition: DataLayout.h:265
@ VPI_PACKING_X64
One 64-bit channel.
Definition: DataLayout.h:231
@ VPI_PACKING_X8_Y8_Z8_W8
Four 8-bit channels in one 32-bit word.
Definition: DataLayout.h:219
@ VPI_PACKING_X8_Y8_Z8
Three 8-bit channels in three 8-bit words.
Definition: DataLayout.h:199
@ VPI_PACKING_X64_Y64
Two 64-bit channels in two 64-bit words.
Definition: DataLayout.h:245
@ VPI_PACKING_X32_Y32_Z32_W32
Four 32-bit channels in three 32-bit words.
Definition: DataLayout.h:247
@ VPI_PACKING_X8
One 8-bit channel.
Definition: DataLayout.h:144
@ VPI_PACKING_X32_Y32_Z32
Three 32-bit channels in three 32-bit words.
Definition: DataLayout.h:240
@ VPI_PACKING_X32
One 32-bit channel.
Definition: DataLayout.h:202
@ VPI_PACKING_X8_Y8
Two 8-bit channels in two 8-bit words.
Definition: DataLayout.h:164
@ VPI_PACKING_X64_Y64_Z64
Three 64-bit channels in three 64-bit words.
Definition: DataLayout.h:252
@ VPI_PACKING_X64_Y64_Z64_W64
Four 64-bit channels in four 64-bit words.
Definition: DataLayout.h:257
@ VPI_PACKING_X16_Y16_Z16
Three 16-bit channels in three 16-bit words.
Definition: DataLayout.h:228
@ VPI_PACKING_X16
One 16-bit channel.
Definition: DataLayout.h:151
@ VPI_PACKING_X16_Y16
Two 16-bit channels in two 16-bit words.
Definition: DataLayout.h:207
@ VPI_PACKING_X16_Y16_Z16_W16
Four 16-bit channels in one 64-bit word.
Definition: DataLayout.h:235
@ VPI_PACKING_X8_Y8__X8_Z8
2 pixels of 2 8-bit channels each, totalling 4 8-bit words.
Definition: DataLayout.h:191
@ VPI_PACKING_X32_Y32
Two 32-bit channels in two 32-bit words.
Definition: DataLayout.h:233
@ VPI_PACKING_Y8_X8__Z8_X8
2 pixels of 2 swapped 8-bit channels each, totalling 4 8-bit words.
Definition: DataLayout.h:193
@ VPI_MEM_LAYOUT_PITCH_LINEAR
Pixels are laid out in row-major order.
Definition: DataLayout.h:289
@ VPI_DATA_TYPE_FLOAT
Channel are floating point values.
Definition: DataLayout.h:272
@ VPI_DATA_TYPE_UNSIGNED
Channels are unsigned integer values.
Definition: DataLayout.h:270
@ VPI_DATA_TYPE_SIGNED
Channels are signed integer values.
Definition: DataLayout.h:271
uint64_t VPIPixelType
Pre-defined pixel types.
Definition: PixelType.h:78
uint64_t VPIImageFormat
Pre-defined image formats.
Definition: ImageFormat.h:94
VPIImageBuffer buffer
Stores the image contents.
Definition: Image.h:241
VPIImagePlanePitchLinear planes[VPI_MAX_PLANE_COUNT]
Data of all image planes in pitch-linear layout.
Definition: Image.h:160
VPIImageBufferPitchLinear pitch
Image stored in pitch-linear layout.
Definition: Image.h:210
void * data
Pointer to the first row of this plane.
Definition: Image.h:141
int32_t numPlanes
Number of planes.
Definition: Image.h:156
VPIImageFormat format
Image format.
Definition: Image.h:152
VPIPixelType pixelType
Type of each pixel within this plane.
Definition: Image.h:115
VPIImageBufferType bufferType
Type of image buffer.
Definition: Image.h:238
int32_t height
Height of this plane in pixels.
Definition: Image.h:123
int32_t width
Width of this plane in pixels.
Definition: Image.h:119
int32_t pitchBytes
Difference in bytes of beginning of one row and the beginning of the previous.
Definition: Image.h:134
#define VPI_MAX_PLANE_COUNT
Maximum number of data planes an image can have.
Definition: Image.h:146
@ VPI_IMAGE_BUFFER_HOST_PITCH_LINEAR
Host-accessible with planes in pitch-linear memory layout.
Definition: Image.h:172
Stores the image plane contents.
Definition: Image.h:150
Stores information about image characteristics and content.
Definition: Image.h:234
Represents one image plane in pitch-linear layout.
Definition: Image.h:112
VPIStatus
Status codes.
Definition: Status.h:81
@ VPI_SUCCESS
Operation completed successfully.
Definition: Status.h:82
@ VPI_ERROR_INTERNAL
Internal, non specific error.
Definition: Status.h:94
@ VPI_ERROR_INVALID_ARGUMENT
Invalid argument, either wrong range or value not accepted.
Definition: Status.h:84
@ VPI_ERROR_INVALID_IMAGE_FORMAT
Image type not accepted.
Definition: Status.h:85