如何将OpenCV Mat图像转换为GDI位图 [英] How to convert an opencv mat image to gdi bitmap
问题描述
我想将openCV Mat文件转换为GDI + BitMap图像.我找不到有关如何执行此操作的任何信息?
I want to convert an openCV Mat file into a GDI+ BitMap image. I cannot find any information on how to do this?
我认为没有这样做的直接方法,但是我希望它不涉及将其写入文件并回读(
I think there is no straight way of doing this, but I hope that it is not involved of writing it to a file and reading back (http://opencv-users.1802565.n2.nabble.com/Convert-IplImage-to-Bitmap-td3784378.html )
我需要写这样的东西:
#include <windows.h>
#include <gdiplus.h>
#include <stdio.h>
using namespace Gdiplus;
Bitmap GetBitMap(cv::Mat inputImage)
{
Bitmap bitmap;
// convert inputImage to bitmap??
return bitmap;
}
推荐答案
According to the documentation, it appears that Bitmap
has a constructor taking a pre-allocated memory buffer. Using this constructor, your function might look like this:
Bitmap GetBitMap(cv::Mat inputImage)
{
cv::Size size = inputImage.size();
Bitmap bitmap(size.width, size.height, inputImage.step1(), PixelFormat24bppRGB, inputImage.data);
return bitmap;
}
请注意,从函数返回的Bitmap
不会管理初始化它的内存.因此,请确保图像数据在Bitmap
的生命周期内保持活动状态.
Be advised that the Bitmap
returned from your function does not manage the memory you initialize it with. Thus, make sure the image data is being kept alive for the lifetime of the Bitmap
.
这篇关于如何将OpenCV Mat图像转换为GDI位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!