VPI - Vision Programming Interface

1.1 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 
101 inline VPIStatus vpiImageCreateOpenCVMatWrapper(const cv::Mat &mat, VPIImageFormat fmt, uint32_t flags, VPIImage *img)
102 {
103  VPIImageData imgData = {};
104  VPIStatus status = ::nv::vpi::detail::FillImageData(mat, fmt, &imgData);
105  if (status != VPI_SUCCESS)
106  {
107  return status;
108  }
109 
110  return vpiImageCreateHostMemWrapper(&imgData, flags, img);
111 }
112 
133 inline VPIStatus vpiImageCreateOpenCVMatWrapper(const cv::Mat &mat, uint32_t flags, VPIImage *img)
134 {
135  VPIImageData imgData = {};
136  VPIStatus status = ::nv::vpi::detail::FillImageData(mat, &imgData);
137  if (status != VPI_SUCCESS)
138  {
139  return status;
140  }
141 
142  return vpiImageCreateHostMemWrapper(&imgData, flags, img);
143 }
144 
163 inline VPIStatus vpiImageSetWrappedOpenCVMat(VPIImage img, const cv::Mat &mat)
164 {
165  VPIImageFormat fmt;
166  VPIStatus status = vpiImageGetFormat(img, &fmt);
167  if (status != VPI_SUCCESS)
168  {
169  return status;
170  }
171 
172  VPIImageData imgData = {};
173  status = ::nv::vpi::detail::FillImageData(mat, fmt, &imgData);
174  if (status != VPI_SUCCESS)
175  {
176  return status;
177  }
178 
179  return vpiImageSetWrappedHostMem(img, &imgData);
180 }
181 
201 inline VPIStatus vpiImageCreateOpenCVMatPlanesWrapper(const std::vector<cv::Mat> &matPlanes, VPIImageFormat fmt,
202  uint32_t flags, VPIImage *img)
203 {
204  VPIImageData imgData = {};
205  VPIStatus status = ::nv::vpi::detail::FillImageData(matPlanes, fmt, &imgData);
206  if (status != VPI_SUCCESS)
207  {
208  return status;
209  }
210 
211  return vpiImageCreateHostMemWrapper(&imgData, flags, img);
212 }
213 
234 inline VPIStatus vpiImageSetWrappedOpenCVMatPlanes(VPIImage img, const std::vector<cv::Mat> &matPlanes)
235 {
236  VPIImageFormat fmt;
237  VPIStatus status = vpiImageGetFormat(img, &fmt);
238  if (status != VPI_SUCCESS)
239  {
240  return status;
241  }
242 
243  VPIImageData imgData = {};
244  status = ::nv::vpi::detail::FillImageData(matPlanes, fmt, &imgData);
245  if (status != VPI_SUCCESS)
246  {
247  return status;
248  }
249 
250  return vpiImageSetWrappedHostMem(img, &imgData);
251 }
252 
269 inline VPIStatus vpiImageDataExportOpenCVMat(const VPIImageData &imgData, cv::Mat *mat)
270 {
271  if (mat == NULL)
272  {
274  }
275 
276  return ::nv::vpi::detail::ExportOpenCVMat(imgData, *mat);
277 }
278 
290 inline VPIStatus vpiImageDataExportOpenCVMatPlanes(const VPIImageData &imgData, std::vector<cv::Mat> *matPlanes)
291 {
292  if (matPlanes == NULL)
293  {
295  }
296 
297  return ::nv::vpi::detail::ExportOpenCVMatPlanes(imgData, *matPlanes);
298 }
299 
312 inline VPIStatus vpiImageDataImportOpenCVMat(const cv::Mat &mat, VPIImageData *imgData)
313 {
314  if (imgData == NULL)
315  {
317  }
318 
319  return ::nv::vpi::detail::FillImageData(mat, imgData);
320 }
321 
338 inline VPIStatus vpiImageDataImportOpenCVMat(const cv::Mat &mat, VPIImageFormat fmt, VPIImageData *imgData)
339 {
340  if (imgData == NULL)
341  {
343  }
344 
345  return ::nv::vpi::detail::FillImageData(mat, fmt, imgData);
346 }
347 
367 inline VPIStatus vpiImageDataImportOpenCVMatPlanes(const std::vector<cv::Mat> &matPlanes, VPIImageFormat fmt,
368  VPIImageData *imgData)
369 {
370  if (imgData == NULL)
371  {
373  }
374 
375  return ::nv::vpi::detail::FillImageData(matPlanes, fmt, imgData);
376 }
377 
380 #endif // NV_VPI_OPENCV_INTEROP_HPP
Functions and structures for dealing with VPI images.
VPIStatus vpiImageSetWrappedHostMem(VPIImage img, const VPIImageData *hostData)
Redefines the wrapped host memory in an existing VPIImage wrapper.
VPIStatus vpiImageCreateHostMemWrapper(const VPIImageData *hostData, uint32_t flags, VPIImage *img)
Create an image object by wrapping around an existing host memory block.
VPIImageFormat
Pre-defined image formats.
Definition: ImageFormat.h:99
struct VPIImageImpl * VPIImage
A handle to an image.
Definition: Types.h:215
VPIStatus vpiImageGetFormat(VPIImage img, VPIImageFormat *format)
Get the image format.
Stores information about image characteristics and content.
Definition: Image.h:159
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 vpiImageCreateOpenCVMatPlanesWrapper(const std::vector< cv::Mat > &matPlanes, VPIImageFormat fmt, uint32_t flags, VPIImage *img)
Wraps a vector of cv::Mat in a VPIImage, each cv::Mat representing one image plane.
VPIStatus vpiImageDataImportOpenCVMat(const cv::Mat &mat, VPIImageData *imgData)
Fills an existing VPIImageData with data from given cv::Mat.
VPIStatus vpiImageCreateOpenCVMatWrapper(const cv::Mat &mat, VPIImageFormat fmt, uint32_t flags, VPIImage *img)
Wraps a cv::Mat in an VPIImage with the given image format.
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