VPI - Vision Programming Interface

4.0 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.pBase = reinterpret_cast<VPIByte *>(mat.data);
348  imgPlane.offsetBytes = 0;
349  imgPlane.pixelType = pixType;
350  return VPI_SUCCESS;
351 }
352 
353 inline VPIStatus FillImageDataTwoPlanesImpl(const cv::Mat &mat, VPIImageFormat fmt, VPIImageBufferPitchLinear *imgData)
354 {
356 
358  {
360  }
361 
362  if (mat.type() == CV_8UC1)
363  {
366  {
368  }
369  }
370  else if (mat.type() == CV_16UC1)
371  {
374  {
376  }
377  }
378  else
379  {
381  }
382 
383  assert(imgData != nullptr);
384 
385  VPIImagePlanePitchLinear *imgPlanes = imgData->planes;
386 
387  imgPlanes[0].pixelType = vpiImageFormatGetPlanePixelType(fmt, 0);
388  imgPlanes[1].pixelType = vpiImageFormatGetPlanePixelType(fmt, 1);
389 
390  imgPlanes[0].width = mat.cols;
391  imgPlanes[1].width = mat.cols / 2;
392 
394  {
395  case 4:
396  imgPlanes[0].height = mat.rows / 2;
397  break;
398 
399  case 2:
400  imgPlanes[0].height = mat.rows * 2 / 3;
401  break;
402 
403  case 1:
404  imgPlanes[0].height = mat.rows * 4 / 5;
405  break;
406 
407  default:
409  }
410 
411  imgPlanes[1].height = mat.rows - imgPlanes[0].height;
412 
413  imgPlanes[0].pitchBytes = mat.step[0];
414  imgPlanes[1].pitchBytes = mat.step[0];
415 
416  imgPlanes[0].pBase = imgPlanes[1].pBase = reinterpret_cast<VPIByte *>(mat.data);
417 
418  imgPlanes[0].offsetBytes = 0;
419  imgPlanes[1].offsetBytes = mat.step[0] * imgPlanes[0].height;
420 
421  return VPI_SUCCESS;
422 }
423 
424 inline VPIStatus FillImageDataThreePlanesImpl(const cv::Mat &mat, VPIImageFormat fmt,
425  VPIImageBufferPitchLinear *imgData)
426 {
428 
430  {
432  }
433 
434  // column count must be even
435  if (mat.cols & 1)
436  {
438  }
439 
440  switch (mat.type())
441  {
442  case CV_8UC1:
443  // must have packed rows
444  if (mat.step[0] != (size_t)mat.cols)
445  {
447  }
448 
449  for (int p = 0; p < 3; ++p)
450  {
452  {
454  }
455  }
456  break;
457 
458  case CV_16UC1:
459  // must have packed rows
460  if (mat.step[0] != (size_t)mat.cols * 2)
461  {
463  }
464 
465  for (int p = 0; p < 3; ++p)
466  {
468  {
470  }
471  }
472  break;
473 
474  default:
476  }
477 
478  assert(imgData != nullptr);
479 
480  VPIImagePlanePitchLinear *imgPlanes = imgData->planes;
481 
482  imgPlanes[0].pixelType = vpiImageFormatGetPlanePixelType(fmt, 0);
483  imgPlanes[1].pixelType = vpiImageFormatGetPlanePixelType(fmt, 1);
484  imgPlanes[2].pixelType = vpiImageFormatGetPlanePixelType(fmt, 2);
485 
486  imgPlanes[0].width = mat.cols;
487  imgPlanes[1].width = mat.cols / 2;
488  imgPlanes[2].width = mat.cols / 2;
489 
491  {
492  case 4:
493  imgPlanes[0].height = mat.rows / 2;
494  break;
495  case 2:
496  imgPlanes[0].height = mat.rows * 2 / 3;
497  break;
498  case 1:
499  imgPlanes[0].height = mat.rows * 4 / 5;
500  break;
501  default:
503  }
504 
505  imgPlanes[1].height = mat.rows - imgPlanes[0].height;
506  imgPlanes[2].height = imgPlanes[1].height;
507 
508  imgPlanes[0].pitchBytes = mat.step[0];
509  imgPlanes[1].pitchBytes = mat.step[0] / 2;
510  imgPlanes[2].pitchBytes = mat.step[0] / 2;
511 
512  imgPlanes[0].pBase = imgPlanes[1].pBase = imgPlanes[2].pBase = reinterpret_cast<VPIByte *>(mat.data);
513 
514  imgPlanes[0].offsetBytes = 0;
515  imgPlanes[1].offsetBytes = mat.step[0] * imgPlanes[0].height;
516  imgPlanes[2].offsetBytes = imgPlanes[1].offsetBytes + imgPlanes[1].height * imgPlanes[1].pitchBytes;
517 
518  return VPI_SUCCESS;
519 }
520 
521 inline VPIStatus FillImageData(const cv::Mat &mat, VPIImageFormat fmt, VPIImageData *imgData)
522 {
523  assert(imgData != NULL);
524 
525  if (mat.data == NULL)
526  {
528  }
529 
530  if (mat.dims != 2)
531  {
533  }
534 
536  {
538  }
539 
540  int numPlanes = vpiImageFormatGetPlaneCount(fmt);
541 
542  memset(imgData, 0, sizeof(*imgData));
543 
545  imgData->buffer.pitch.format = fmt;
546  imgData->buffer.pitch.numPlanes = numPlanes;
547 
548  VPIStatus status;
549  switch (numPlanes)
550  {
551  case 1:
552  status = FillImageDataOnePlaneImpl(mat, fmt, &imgData->buffer.pitch);
553  break;
554 
555  case 2:
556  status = FillImageDataTwoPlanesImpl(mat, fmt, &imgData->buffer.pitch);
557  break;
558 
559  case 3:
560  status = FillImageDataThreePlanesImpl(mat, fmt, &imgData->buffer.pitch);
561  break;
562 
563  default:
564  status = VPI_ERROR_INTERNAL;
565  break;
566  }
567 
568  return status;
569 }
570 
571 inline VPIStatus FillImageData(const std::vector<cv::Mat> &matPlanes, VPIImageFormat fmt, VPIImageData *imgData)
572 {
573  assert(imgData != NULL);
574 
575  if (matPlanes.empty())
576  {
578  }
579 
580  if (matPlanes.size() != (size_t)vpiImageFormatGetPlaneCount(fmt))
581  {
583  }
584 
585  memset(imgData, 0, sizeof(*imgData));
586 
588  imgData->buffer.pitch.format = fmt;
589  imgData->buffer.pitch.numPlanes = matPlanes.size();
590 
591  for (size_t p = 0; p < matPlanes.size(); ++p)
592  {
593  const cv::Mat &mat = matPlanes[p];
594 
595  if (mat.data == NULL)
596  {
598  }
599 
600  if (mat.dims != 2)
601  {
603  }
604 
606 
607  if (ToOpenCVType(pixType) != mat.type())
608  {
610  }
611 
612  VPIImagePlanePitchLinear &plane = imgData->buffer.pitch.planes[p];
613 
614  plane.width = mat.cols;
615  plane.height = mat.rows;
616  plane.pitchBytes = mat.step[0];
617  plane.pBase = reinterpret_cast<VPIByte *>(mat.data);
618  plane.offsetBytes = 0;
619  plane.pixelType = pixType;
620  }
621 
622  return VPI_SUCCESS;
623 }
624 
625 inline VPIStatus FillImageData(const cv::Mat &mat, VPIImageData *imgData)
626 {
627  assert(imgData != NULL);
628 
629  if (mat.data == NULL)
630  {
632  }
633 
634  if (mat.dims != 2)
635  {
637  }
638 
639  VPIImageFormat fmt = ToImageFormatFromOpenCVType(mat.type());
640  if (fmt == VPI_IMAGE_FORMAT_INVALID)
641  {
643  }
644 
645  assert(imgData != nullptr);
646 
648  VPIImageBufferPitchLinear &imgPitch = imgData->buffer.pitch;
649 
650  imgPitch.format = fmt;
651  imgPitch.numPlanes = 1;
652  imgPitch.planes[0].width = mat.cols;
653  imgPitch.planes[0].height = mat.rows;
654  imgPitch.planes[0].pitchBytes = mat.step[0];
655  imgPitch.planes[0].pBase = reinterpret_cast<VPIByte *>(mat.data);
656  imgPitch.planes[0].offsetBytes = 0;
657  imgPitch.planes[0].pixelType = vpiImageFormatGetPlanePixelType(fmt, 0);
658 
659  return VPI_SUCCESS;
660 }
661 
662 inline VPIStatus CalcHeightOpenCVMatOnePlane(const VPIImageBufferPitchLinear &imgData, int &height)
663 {
664  height = imgData.planes[0].height;
665  return VPI_SUCCESS;
666 }
667 
668 inline VPIStatus CalcHeightOpenCVMatTwoPlanes(const VPIImageBufferPitchLinear &imgData, int &height)
669 {
671 
673  {
675  }
676 
677  if (imgData.planes[0].pitchBytes != imgData.planes[1].pitchBytes)
678  {
680  }
681 
682  if (imgData.planes[1].width * 2 != imgData.planes[0].width)
683  {
685  }
686 
687  if (imgData.planes[1].pBase + imgData.planes[1].offsetBytes !=
688  imgData.planes[0].pBase + imgData.planes[0].offsetBytes +
689  imgData.planes[0].height * imgData.planes[0].pitchBytes)
690  {
692  }
693 
695  {
696  case 1:
697  case 2:
698  case 4:
699  height = imgData.planes[0].height + imgData.planes[1].height;
700  break;
701 
702  default:
704  }
705 
706  return VPI_SUCCESS;
707 }
708 
709 inline VPIStatus CalcHeightOpenCVMatThreePlanes(const VPIImageBufferPitchLinear &imgData, int &height)
710 {
712 
713  // All planes must have packed rows
714  for (int i = 0; i < 3; ++i)
715  {
716  if (imgData.planes[i].pitchBytes !=
717  imgData.planes[i].width * vpiPixelTypeGetBitsPerPixel(imgData.planes[i].pixelType) / 8)
718  {
720  }
721  }
722 
723  if (imgData.planes[0].pitchBytes != imgData.planes[1].pitchBytes * 2 ||
724  imgData.planes[1].pitchBytes != imgData.planes[2].pitchBytes)
725  {
727  }
728 
729  int samplesVert = vpiChromaSubsamplingGetSamplesVert(css);
730 
731  if ((imgData.planes[1].height * 4 + samplesVert - 1) / samplesVert != imgData.planes[0].height)
732  {
734  }
735 
736  if (imgData.planes[2].height != imgData.planes[1].height)
737  {
739  }
740 
741  for (int i = 0; i < 2; ++i)
742  {
743  if (imgData.planes[i + 1].pBase + imgData.planes[i + 1].offsetBytes !=
744  imgData.planes[i].pBase + imgData.planes[i].offsetBytes +
745  imgData.planes[i].height * imgData.planes[i].pitchBytes)
746  {
748  }
749  }
750 
752  {
753  case 1:
754  case 2:
755  case 4:
756  height = imgData.planes[0].height + imgData.planes[1].height;
757  break;
758 
759  default:
761  }
762 
763  return VPI_SUCCESS;
764 }
765 
766 inline VPIStatus ExportOpenCVMat(const VPIImageData &imgData, cv::Mat &mat)
767 {
769  {
771  }
772 
773  const VPIImageBufferPitchLinear &imgPitch = imgData.buffer.pitch;
774 
775  VPIStatus status;
776  int height;
777  switch (imgPitch.numPlanes)
778  {
779  case 1:
780  status = CalcHeightOpenCVMatOnePlane(imgPitch, height);
781  break;
782 
783  case 2:
784  status = CalcHeightOpenCVMatTwoPlanes(imgPitch, height);
785  break;
786 
787  case 3:
788  status = CalcHeightOpenCVMatThreePlanes(imgPitch, height);
789  break;
790 
791  default:
793  }
794 
795  if (status == VPI_SUCCESS)
796  {
797  VPIPixelType pixType = imgPitch.planes[0].pixelType;
798 
799  if (pixType == VPI_PIXEL_TYPE_DEFAULT)
800  {
801  pixType = vpiImageFormatGetPlanePixelType(imgPitch.format, 0);
802  if (pixType == VPI_PIXEL_TYPE_INVALID)
803  {
805  }
806  }
807 
808  int cvType = ToOpenCVType(pixType);
809  if (cvType == -1)
810  {
812  }
813 
814  mat = cv::Mat(height, imgPitch.planes[0].width, cvType,
815  imgPitch.planes[0].pBase + imgPitch.planes[0].offsetBytes, imgPitch.planes[0].pitchBytes);
816  }
817 
818  return status;
819 }
820 
821 inline VPIStatus ExportOpenCVMatPlanes(const VPIImageData &imgData, std::vector<cv::Mat> &mat)
822 {
824  {
826  }
827 
828  const VPIImageBufferPitchLinear &imgPitch = imgData.buffer.pitch;
829 
830  if (imgPitch.numPlanes > VPI_MAX_PLANE_COUNT)
831  {
833  }
834 
835  int cvTypes[VPI_MAX_PLANE_COUNT];
836  for (int i = 0; i < imgPitch.numPlanes; ++i)
837  {
838  VPIPixelType pixType = imgPitch.planes[i].pixelType;
839 
840  if (pixType == VPI_PIXEL_TYPE_DEFAULT)
841  {
842  pixType = vpiImageFormatGetPlanePixelType(imgPitch.format, i);
843  if (pixType == VPI_PIXEL_TYPE_INVALID)
844  {
846  }
847  }
848 
849  cvTypes[i] = ToOpenCVType(pixType);
850 
851  if (cvTypes[i] == -1)
852  {
854  }
855  }
856 
857  mat.resize(imgPitch.numPlanes);
858 
859  for (int i = 0; i < imgPitch.numPlanes; ++i)
860  {
861  mat[i] = cv::Mat(imgPitch.planes[i].height, imgPitch.planes[i].width, cvTypes[i],
862  imgPitch.planes[i].pBase + imgPitch.planes[i].offsetBytes, imgPitch.planes[i].pitchBytes);
863  }
864 
865  return VPI_SUCCESS;
866 }
867 
868 }}} // namespace nv::vpi::detail
869 
870 #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:324
#define VPI_IMAGE_FORMAT_2F32
Single plane with two interleaved 32-bit floating point channels.
Definition: ImageFormat.h:142
#define VPI_IMAGE_FORMAT_F32
Single plane with one 32-bit floating point channel.
Definition: ImageFormat.h:136
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:330
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:111
#define VPI_IMAGE_FORMAT_S16
Single plane with one 16-bit signed integer channel.
Definition: ImageFormat.h:120
#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:423
#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:478
#define VPI_IMAGE_FORMAT_S8
Single plane with one 8-bit signed integer channel.
Definition: ImageFormat.h:108
#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
unsigned char VPIByte
Definition of a byte type.
Definition: Types.h:288
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:245
VPIImagePlanePitchLinear planes[VPI_MAX_PLANE_COUNT]
Data of all image planes in pitch-linear layout.
Definition: Image.h:164
VPIImageBufferPitchLinear pitch
Image stored in pitch-linear layout.
Definition: Image.h:214
int32_t numPlanes
Number of planes.
Definition: Image.h:160
VPIImageFormat format
Image format.
Definition: Image.h:156
VPIPixelType pixelType
Type of each pixel within this plane.
Definition: Image.h:115
VPIImageBufferType bufferType
Type of image buffer.
Definition: Image.h:242
int64_t offsetBytes
Offset in bytes from pBase to the first column of the first plane row.
Definition: Image.h:137
int32_t height
Height of this plane in pixels.
Definition: Image.h:123
VPIByte * pBase
Pointer to the memory buffer which contains the plane data.
Definition: Image.h:145
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:150
@ VPI_IMAGE_BUFFER_HOST_PITCH_LINEAR
Host-accessible with planes in pitch-linear memory layout.
Definition: Image.h:176
Stores the image plane contents.
Definition: Image.h:154
Stores information about image characteristics and content.
Definition: Image.h:238
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