• 04/03/2020
  • Public Content

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
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
cannot be used simultaneously by the application and OpenVX.
Other important caveats:
  • Make sure that OpenCV
    object is not destroyed (or resized, etc.) before work on the corresponding
    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


Performance varies by use, configuration and other factors. Learn more at www.Intel.com/PerformanceIndex.