问题描述
我要Gdiplus ::位图转换为CV ::地图格式。
我使用这个code要做到这一点:
I need to convert Gdiplus::Bitmap to cv::Map format.I'm using this code to do this:
Gdiplus::Bitmap* enhanced = ...; // some Bitmap
Gdiplus::BitmapData bmp_data = {};
Gdiplus::Rect rect(0, 0, enhanced->GetWidth(), enhanced->GetHeight());
enhanced->LockBits(&rect, Gdiplus::ImageLockModeRead, enhanced->GetPixelFormat(), &bmp_data);
Mat imageMap(enhanced->GetHeight(), enhanced->GetWidth(), CV_8UC3, bmp_data.Scan0, std::abs(bmp_data.Stride)); // construct Map from Bitmap data. The problem is probably here
cvNamedWindow("w", 1);
cvShowImage("w", &imageMap); // runtime error (access violation)
cvWaitKey(0);
我有一个运行时错误,因为的图像映射的不正确构造。我在做什么错在这里?我会gratefull你的解释。
I have an runtime error, as imageMap wasn't properly constructed. What am I doing wrong here? I will be gratefull for your explanation.
推荐答案
如果你从你的位图构建简历::垫,你将不得不使用
if you're constructing a cv::Mat from your Bitmap, you will have to use
cv::imshow("w", imageMap);
要画它。
再次,简历的地址::垫是不一样的作为的IplImage *通过cvShowImage()要求;
again, the address of a cv::Mat is not the same as an IplImage* required by cvShowImage();
(顺便说一句,你应该摆脱所有其他德precated C-API的调用,也。)
(btw, you should get rid of all other deprecated c-api calls, too.)
也小心一点,一个垫构造你的方式,拥有的借的指针像素。
also, be a bit careful, a Mat constructed the way you do, has a borrowed pointer to the pixels.
我不知道有关GDI +什么,但是如果指针超出范围或将失效,当你调用的增强型> UnlockRect(或它被称为),你需要做的。
i don't know anything about gdi+, but if that pointer goes out of scope or gets invalid when you call enhanced->UnlockRect (or what it was called), you will need to do
Mat safeImg = imageMap.clone();
要实现一个深的副本。
这篇关于位图转换为太的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!