- 头文件
#include <stdio.h> #include <stdlib.h> #include <opencv2/opencv.hpp> #include "VX/vx.h" // 函数名称 #ifdef DEBUG #define DEBUG_FUNCTION_NAME(void) \ { \ printf("================================================================\n"); \ printf("%s\n", __func__); \ } #define DEBUG_LOG(format, ...) \ { \ printf("[%s:%d->%s] " format, __FILE__, __LINE__, __func__, ##__VA_ARGS__); \ } #else #define DEBUG_FUNCTION_NAME(void) #define DEBUG_LOG(format, ...) #endif static inline vx_image vxCvMatToVxImage(const vx_context context, const cv::Mat &cv_image) { DEBUG_FUNCTION_NAME(); // 图像类型 vx_df_image vx_image_type; switch (cv_image.type()) { case CV_8UC1: vx_image_type = VX_DF_IMAGE_U8; break; case CV_8UC3: vx_image_type = VX_DF_IMAGE_RGB; break; default: DEBUG_LOG("Format %d not supported\n", vx_image_type); return NULL; } // 图像属性 int width = cv_image.cols; int height = cv_image.rows; // 创建图像 vx_image image = vxCreateImage(context, width, height, vx_image_type); // 区域 vx_rectangle_t patch; patch.start_x = 0; patch.start_y = 0; patch.end_x = width; patch.end_y = height; vx_map_id map_id; vx_imagepatch_addressing_t addr; unsigned char *ptr; vx_status status = vxMapImagePatch(image, &patch, 0, &map_id, &addr, (void **)&ptr, VX_WRITE_ONLY, VX_MEMORY_TYPE_HOST, VX_NOGAP_X); if (status != VX_SUCCESS) { DEBUG_LOG("vxMapImagePatch returned error with code %d\n", status); return NULL; } if (addr.stride_x != 1 && addr.stride_x != 3) { DEBUG_LOG("addressing structure not supported, stride_x = %d\n", addr.stride_x); return NULL; } for (int y = 0; y < height; y++) { unsigned char *ptr_y = ptr + y * addr.stride_y; memcpy(ptr_y, cv_image.ptr(y), addr.stride_y); } status = vxUnmapImagePatch(image, map_id); if (status != VX_SUCCESS) { DEBUG_LOG("vxUnmapImagePatch failed...\n"); return NULL; } return image; } static inline int vxVxImageToCvMat(const vx_image image, cv::Mat &cv_image) { DEBUG_FUNCTION_NAME(); // 图像属性 vx_uint32 width = 0; vx_uint32 height = 0; ERROR_CHECK_STATUS(vxQueryImage(image, VX_IMAGE_WIDTH, &width, sizeof(width))); ERROR_CHECK_STATUS(vxQueryImage(image, VX_IMAGE_HEIGHT, &height, sizeof(height))); // 图像类型 vx_df_image image_type; ERROR_CHECK_STATUS(vxQueryImage(image, VX_IMAGE_FORMAT, &image_type, sizeof(image_type))); int cv_image_type; switch (image_type) { case VX_DF_IMAGE_U8: cv_image_type = CV_8UC1; break; case VX_DF_IMAGE_RGB: cv_image_type = CV_8UC3; break; default: DEBUG_LOG("Format %d not supported\n", cv_image_type); return -1; } // 区域 vx_rectangle_t patch = {0, 0, width, height}; vx_map_id map_id; vx_imagepatch_addressing_t addr; unsigned char *ptr; vx_status status = vxMapImagePatch(image, &patch, 0, &map_id, &addr, (void **)&ptr, VX_READ_ONLY, VX_MEMORY_TYPE_HOST, VX_NOGAP_X); if (status != VX_SUCCESS) { DEBUG_LOG("vxMapImagePatch returned error with code %d\n", status); return -1; } if (addr.stride_x != 1 && addr.stride_x != 3) { DEBUG_LOG("addressing structure not supported, stride_x = %d\n", addr.stride_x); return -1; } cv_image = cv::Mat(height, width, cv_image_type); for (int y = 0; y < height; y++) { unsigned char *ptr_y = ptr + y * addr.stride_y; memcpy(cv_image.ptr(y), ptr_y, addr.stride_y); } status = vxUnmapImagePatch(image, map_id); if (status != VX_SUCCESS) { DEBUG_LOG("vxUnmapImagePatch failed...\n"); return -1; } return 0; } static inline vx_image ReadImage(vx_context context, const char *file_name) { cv::Mat mat = cv::imread(file_name, cv::IMREAD_ANYCOLOR); return vxCvMatToVxImage(context, mat); } static inline void SaveImage(vx_image image, const char *file_name) { cv::Mat mat; int ret = vxVxImageToCvMat(image, mat); if (!ret) cv::imwrite(file_name, mat); } static inline void DisplayImage(vx_image image, const char *window_name, int time = 0) { cv::Mat mat; int ret = vxVxImageToCvMat(image, mat); if (!ret) { cv::namedWindow(window_name, cv::WINDOW_NORMAL); cv::resizeWindow(window_name, 800, 600); cv::imshow(window_name, mat); cv::waitKey(time); } }
- 测试
int main(int argc, char* argv[]) { vx_context context = vxCreateContext(); vx_image image = ReadImage(context, "./images/image.jpg"); DisplayImage(image, "test"); SaveImage(image, "test.png"); return EXIT_SUCCESS; }
- 效果
【OPENVX】vx_image 与 cv::Mat 相互转换
于 2022-07-04 20:11:54 首次发布