我试图将GDIPlus :: Bitmap转换为openCV Mat对象,但是我遇到了访问冲突问题,这意味着我'我没有做正确的事情,但我一遍又一遍地查看了代码,我认为它应该可以工作。将GDIPlus :: Bitmap转换为cv :: Mat(OpenCV C++接口)
有没有人看到一个明显的问题?
cv::Mat ConvertToOpenCV(Gdiplus::Bitmap &image) {
cv::Mat *retval = new cv::Mat(
image.GetWidth(), image.GetHeight(), CV_8UC3
);
Gdiplus::BitmapData source;
Gdiplus::Rect rect(0, 0, image.GetWidth(), image.GetHeight());
Gdiplus::Status status =
image.LockBits(&rect, Gdiplus::ImageLockModeRead, PixelFormat24bppRGB, &source);
if (status != Gdiplus::Ok) {
// Some error condition
return retval; // No image copied
}
BYTE *destination = (BYTE *)retval->data;
for (int y = 0; y != source.Height; ++y) {
BYTE *src = (BYTE *) source.Scan0 + y * source.Stride;
BYTE *dst = (BYTE *)(destination + y * retval->step);
memcpy(dst, src, 3 * source.Width); // Access Violation happens here
}
image.UnlockBits(&source);
return retval;
}
Doh!非常感谢您的关注,我甚至都没有想过看看!这是一个很好的测试用例,我需要添加到我的单元测试中。我相信我所有的测试图像都是正方形的,这会隐藏这种行为! 颜色顺序对我来说并不重要,因为我正在转换为灰度并对图像进行区分,所以我没有打扰它。 非常感谢您抓住我愚蠢的错误! – RussTheAerialist
我一定找到了它,因为我很习惯在自己的代码中找到类似的东西!当我使用MAX()时,我无法告诉你我使用了MIN()多少次。 :-) – SSteve