如何将cv :: Mat数据复制回sampleBuffer?
我的情景如下: 我从pixelBuffer创建一个cv :: Mat用于地标检测,并将地标添加到cv :: Mat图像数据。我想将此cv :: Mat复制到样本缓冲区中以显示地标。
这可能吗?
我用dlib实现了这一点,但需要知道如何使用cv :: mat:
char *baseBuffer = (char *)CVPixelBufferGetBaseAddress(imageBuffer);
img.reset();
long position = 0;
while (img.move_next()) {
dlib::bgr_pixel& pixel = img.element();
long bufferLocation = position * 4; //(row * width + column) * 4;
char b = baseBuffer[bufferLocation];
char g = baseBuffer[bufferLocation + 1];
char r = baseBuffer[bufferLocation + 2];
dlib::bgr_pixel newpixel(b, g, r);
pixel = newpixel;
position++;
}
答案 0 :(得分:3)
我正在回答我自己的问题。
首先,你需要访问cv :: mat Image的像素数据,我跟着这个伟大的solution
然后你需要从basebuffer开始将像素复制到缓冲区。以下代码可以帮助您实现这一目标:
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
char *baseBuffer = (char *)CVPixelBufferGetBaseAddress(imageBuffer);
long position = 0;
uint8_t* pixelPtr = (uint8_t*)targetImage.data;
int cn = targetImage.channels();
cv::Scalar_<uint8_t> rgbPixel;
for(int i = 0; i < targetImage.rows; i++)
{
for(int j = 0; j < targetImage.cols; j++)
{
long bufferLocation = position * 4;
rgbPixel.val[0] = pixelPtr[i*targetImage.cols*cn + j*cn + 0]; // B
rgbPixel.val[1] = pixelPtr[i*targetImage.cols*cn + j*cn + 1]; // G
rgbPixel.val[2] = pixelPtr[i*targetImage.cols*cn + j*cn + 2]; // R
baseBuffer[bufferLocation] = rgbPixel.val[2];
baseBuffer[bufferLocation + 1] = rgbPixel.val[1];
baseBuffer[bufferLocation + 2] = rgbPixel.val[0];
position++;
}
}
需要注意的一些事项
CVPixelBufferLockBaseAddress
和
操作前后CVPixelBufferUnlockBaseAddress
。我CV_8UC3
上执行此操作,您可能需要检查您的cv :: mat