• 04/03/2020
  • Public Content
Contents

Example Interoperability with OpenCV

Unlike APIs that use dedicated address space and/or special data layouts (e.g. compressed OpenGL* textures), regular OpenCV data objects like
cv::Mat
reside in the conventional system memory. That is, the memory can be actually shared with OpenVX* and only data ownership to be transferred:
//Capture from default webcam cv::VideoCapture inp; inp.open(0); cv::Mat matBGR, mat RGB; //read first frame so the ‘matRGB’ allocates data, etc inp.read(matBGR); //convert the format, as OpenCV’s default is BGR cv::cvtColor(matBGR, matRGB, cv::COLOR_BGR2RGB); // wrap into the OpenVX vx_imagepatch_addressing_t addr; addr.dim_x = matRGB.cols; addr.dim_y = matRGB.rows; addr.stride_x = matRGB.elemSize(); addr.stride_y = matRGB.step; void *ptr[] = { matRGB.data }; vx_image image=vxCreateImageFromHandle(ctx,…,&addr,ptr, VX_MEMORY_TYPE_HOST); //Capture frames while(1) { // Per spec, mapping of the image created from handle should return // same address (and same memory layout) void*p = NULL; vx_imagepatch_addressing_t addr; vx_rectangle_t rect = { 0, 0, matRGB.cols, matRGB.rows }; //Request pointer to the system mem vx_map_id map_id; vxMapImagePatch(image, &rect, 0, &map_id, &addr, &p, VX_WRITE_ONLY ); //Read the next image inp.read(matBGR); cv::cvtColor(matBGR, matRGB, cv::COLOR_BGR2RGB); // Notify the OpenVX we are done vxUnmapImagePatch(image, map_id); //Do processing with OpenVX ... }
Notice that original
cv::Mat
cannot be used simultaneously by the application and OpenVX.
Other important caveats:
  • Make sure that OpenCV
    cv::Mat
    object is not destroyed (or resized, etc.) before work on the corresponding
    vx_image
    object finishes.
  • Notice that by default the OpenCV uses BGR color space for color images, while the OpenVX supports only RGB channel order, so you might need format conversion, see example above.
Refer to the samples description in the OpenVX Sample Applications section for OpenCV capturing, OpenVX* processing, and OpenCV rendering.

Product and Performance Information

1

Intel's compilers may or may not optimize to the same degree for non-Intel microprocessors for optimizations that are not unique to Intel microprocessors. These optimizations include SSE2, SSE3, and SSSE3 instruction sets and other optimizations. Intel does not guarantee the availability, functionality, or effectiveness of any optimization on microprocessors not manufactured by Intel. Microprocessor-dependent optimizations in this product are intended for use with Intel microprocessors. Certain optimizations not specific to Intel microarchitecture are reserved for Intel microprocessors. Please refer to the applicable product User and Reference Guides for more information regarding the specific instruction sets covered by this notice.

Notice revision #20110804