gpt4 book ai didi

c++ - 将 cv::Mat 转换为 ipcMatrix

转载 作者:太空宇宙 更新时间:2023-11-03 22:30:20 25 4
gpt4 key购买 nike

疑问:

有没有简单的方法来转换cv::Mat ipcMatrix<ipcRGB>

最佳答案

转换的秘诀cv::Mat到另一种数据类型,就是理解数据(像素)是如何存储和排序的:

  • OpenCV 将数据存储在 cv::Mat 中作为 unsigned char 的数组,像素按照BGR顺序存储;
  • ipcMatrix<ipcRGB>工作方式几乎相同,除了像素存储为 RGB;

就是说,要转换 cv::Mat进入ipcMatrix<ipcRGB>您需要做的就是:

// Load input image
cv::Mat mat_input = cv::imread("input.jpg");

// Convert a BGR Mat into RGB:
cv::Mat mat_rgb;
cv::cvtColor(mat_input, mat_rgb, cv::COLOR_BGR2RGB);

// Copy the pixels from the Mat to another memory location:
int data_sz = mat_rgb.rows * mat_rgb.cols * mat_rgb.channels();
unsigned char* pixels = new unsigned char[data_sz];
memcpy(pixels, mat_rgb.data, data_sz);

// And finally, use the constructor of ipcMatrix<> to declare the new object correctly:
ipcMatrix<ipcRGB> input = ipcMatrix<ipcRGB>(mat_rgb.cols, mat_rgb.rows, (ipcRGB*)pixels);

关于c++ - 将 cv::Mat 转换为 ipcMatrix<ipcRGB>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27135112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com