VPI - Vision Programming Interface

3.2 Release

OpenCVInterop.hpp
Go to the documentation of this file.
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 
56 #ifndef NV_VPI_OPENCV_INTEROP_HPP
57 #define NV_VPI_OPENCV_INTEROP_HPP
58 
59 #include "detail/OpenCVUtils.hpp"
60 
61 #include "Image.h"
62 
63 #include <opencv2/core/core.hpp>
64 
65 #include <vector>
66 
117 inline VPIStatus vpiImageCreateWrapperOpenCVMat(const cv::Mat &mat, VPIImageFormat fmt, uint64_t flags, VPIImage *img)
118 {
119  VPIImageData imgData = {};
120 
121  VPIStatus status = ::nv::vpi::detail::FillImageData(mat, fmt, &imgData);
122  if (status != VPI_SUCCESS)
123  {
124  return status;
125  }
126 
127  return vpiImageCreateWrapper(&imgData, nullptr, flags, img);
128 }
129 
166 inline VPIStatus vpiImageCreateWrapperOpenCVMat(const cv::Mat &mat, uint64_t flags, VPIImage *img)
167 {
168  VPIImageData imgData = {};
169  VPIStatus status = ::nv::vpi::detail::FillImageData(mat, &imgData);
170  if (status != VPI_SUCCESS)
171  {
172  return status;
173  }
174 
175  return vpiImageCreateWrapper(&imgData, nullptr, flags, img);
176 }
177 
207 inline VPIStatus vpiImageSetWrappedOpenCVMat(VPIImage img, const cv::Mat &mat)
208 {
209  VPIImageFormat fmt;
210  VPIStatus status = vpiImageGetFormat(img, &fmt);
211  if (status != VPI_SUCCESS)
212  {
213  return status;
214  }
215 
216  VPIImageData imgData = {};
217  status = ::nv::vpi::detail::FillImageData(mat, fmt, &imgData);
218  if (status != VPI_SUCCESS)
219  {
220  return status;
221  }
222 
223  return vpiImageSetWrapper(img, &imgData);
224 }
225 
263 inline VPIStatus vpiImageCreateWrapperOpenCVMatPlanes(const std::vector<cv::Mat> &matPlanes, VPIImageFormat fmt,
264  uint64_t flags, VPIImage *img)
265 {
266  VPIImageData imgData = {};
267  VPIStatus status = ::nv::vpi::detail::FillImageData(matPlanes, fmt, &imgData);
268  if (status != VPI_SUCCESS)
269  {
270  return status;
271  }
272 
273  return vpiImageCreateWrapper(&imgData, nullptr, flags, img);
274 }
275 
304 inline VPIStatus vpiImageSetWrappedOpenCVMatPlanes(VPIImage img, const std::vector<cv::Mat> &matPlanes)
305 {
306  VPIImageFormat fmt;
307  VPIStatus status = vpiImageGetFormat(img, &fmt);
308  if (status != VPI_SUCCESS)
309  {
310  return status;
311  }
312 
313  VPIImageData imgData = {};
314  status = ::nv::vpi::detail::FillImageData(matPlanes, fmt, &imgData);
315  if (status != VPI_SUCCESS)
316  {
317  return status;
318  }
319 
320  return vpiImageSetWrapper(img, &imgData);
321 }
322 
346 inline VPIStatus vpiImageDataExportOpenCVMat(const VPIImageData &imgData, cv::Mat *mat)
347 {
348  if (mat == NULL)
349  {
351  }
352 
353  return ::nv::vpi::detail::ExportOpenCVMat(imgData, *mat);
354 }
355 
375 inline VPIStatus vpiImageDataExportOpenCVMatPlanes(const VPIImageData &imgData, std::vector<cv::Mat> *matPlanes)
376 {
377  if (matPlanes == NULL)
378  {
380  }
381 
382  return ::nv::vpi::detail::ExportOpenCVMatPlanes(imgData, *matPlanes);
383 }
384 
405 inline VPIStatus vpiImageDataImportOpenCVMat(const cv::Mat &mat, VPIImageData *imgData)
406 {
407  if (imgData == NULL)
408  {
410  }
411 
412  return ::nv::vpi::detail::FillImageData(mat, imgData);
413 }
414 
440 inline VPIStatus vpiImageDataImportOpenCVMat(const cv::Mat &mat, VPIImageFormat fmt, VPIImageData *imgData)
441 {
442  if (imgData == NULL)
443  {
445  }
446 
447  return ::nv::vpi::detail::FillImageData(mat, fmt, imgData);
448 }
449 
480 inline VPIStatus vpiImageDataImportOpenCVMatPlanes(const std::vector<cv::Mat> &matPlanes, VPIImageFormat fmt,
481  VPIImageData *imgData)
482 {
483  if (imgData == NULL)
484  {
486  }
487 
488  return ::nv::vpi::detail::FillImageData(matPlanes, fmt, imgData);
489 }
490 
493 #endif // NV_VPI_OPENCV_INTEROP_HPP
Functions and structures for dealing with VPI images.
uint64_t VPIImageFormat
Pre-defined image formats.
Definition: ImageFormat.h:94
struct VPIImageImpl * VPIImage
A handle to an image.
Definition: Types.h:256
VPIStatus vpiImageCreateWrapper(const VPIImageData *data, const VPIImageWrapperParams *params, uint64_t flags, VPIImage *img)
Create an image object by wrapping an existing memory block.
VPIStatus vpiImageSetWrapper(VPIImage img, const VPIImageData *data)
Redefines the wrapped memory in an existing VPIImage wrapper.
VPIStatus vpiImageGetFormat(VPIImage img, VPIImageFormat *format)
Get the image format.
Stores information about image characteristics and content.
Definition: Image.h:234
VPIStatus vpiImageCreateWrapperOpenCVMatPlanes(const std::vector< cv::Mat > &matPlanes, VPIImageFormat fmt, uint64_t flags, VPIImage *img)
Wraps a vector of cv::Mats in a VPIImage, each cv::Mat representing one image plane.
VPIStatus vpiImageCreateWrapperOpenCVMat(const cv::Mat &mat, VPIImageFormat fmt, uint64_t flags, VPIImage *img)
Wraps a cv::Mat in an VPIImage with the given image format.
VPIStatus vpiImageDataExportOpenCVMat(const VPIImageData &imgData, cv::Mat *mat)
Fills an existing cv::Mat with data from VPIImageData coming from a locked VPIImage.
VPIStatus vpiImageSetWrappedOpenCVMat(VPIImage img, const cv::Mat &mat)
Redefines the wrapped cv::Mat of an existing VPIImage wrapper.
VPIStatus vpiImageSetWrappedOpenCVMatPlanes(VPIImage img, const std::vector< cv::Mat > &matPlanes)
Replaces the wrapped cv::Mat planes with new ones.
VPIStatus vpiImageDataExportOpenCVMatPlanes(const VPIImageData &imgData, std::vector< cv::Mat > *matPlanes)
Fills a vector of cv::Mat with all planes from VPIImageData.
VPIStatus vpiImageDataImportOpenCVMat(const cv::Mat &mat, VPIImageData *imgData)
Fills an existing VPIImageData with data from given cv::Mat.
VPIStatus vpiImageDataImportOpenCVMatPlanes(const std::vector< cv::Mat > &matPlanes, VPIImageFormat fmt, VPIImageData *imgData)
Fills a vector of cv::Mat describing all planes from given VPIImageData forcing the given image forma...
VPIStatus
Status codes.
Definition: Status.h:81
@ VPI_SUCCESS
Operation completed successfully.
Definition: Status.h:82
@ VPI_ERROR_INVALID_ARGUMENT
Invalid argument, either wrong range or value not accepted.
Definition: Status.h:84