转换的秘诀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);
我是一名优秀的程序员,十分优秀!