演员表 cv::垫到 ipc矩阵<ipcRGB>

Cast cv::Mat to ipcMatrix<ipcRGB>

本文关键字:gt lt ipcRGB 矩阵 ipc cv 垫到      更新时间:2023-10-16

怀疑:

有没有一种简单的方法可以将cv::Mat投射到ipcMatrix<ipcRGB>

cv::Mat转换为另一种数据类型的秘诀是了解数据(像素)的存储和排序方式:

  • OpenCV将数据作为unsigned char数组存储在cv::Mat中,像素按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);