cv::Mat 格式互转
// 转 Mat
ccPelBuffer_const<c_UInt8> trainimg = xPattern.trainImage();
const ccPelRoot<c_UInt8>* croot = trainimg.root();
// 零拷贝:Mat 直接包装 Cognex 的像素缓冲区
cv::Mat trainMat(croot->height(), croot->width(), CV_8UC1,
(void*)croot->pels(), croot->rowUpdate());
// Mat 转 ccPelbuffer
ccPelRoot<c_UInt8>* searchPelRoot = new ccPelRoot<c_UInt8>(searchimg.size().width,searchimg.size().height,
static_cast<int>(searchimg.step), searchimg.data);
ccPelBuffer<c_UInt8> searchImagePelBuf(searchPelRoot);
注意:这种方式创建的 Mat 不持有数据,trainimg/xPattern 的生命周期必须覆盖 trainMat 的使用。如需独立拷贝:
cv::Mat matCopy = trainMat.clone(); // 深拷贝,独立拥有数据