将HBITMAP转换为cv :: Mat [英] Convert HBITMAP to cv::Mat

查看:1003
本文介绍了将HBITMAP转换为cv :: Mat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎不知道该怎么做,我在google上搜索并找到了两个代码示例,一个来自github屏幕捕获库,另一个来自发布组,两个似乎都不起作用.

I can't seem to figure out how to do this, I have searched on google and found two code examples, one from a github screen capture library and another on a posting group and neither of them seem to be working.

我有一个结构:

struct ClacksScreen
{
    HWND hDesktopWnd; 
    int width, height;
    RECT wr, cr;
    HDC hdcClacksScreen; // hardware ClacksScreen
    HDC hdcMemDC; // ClacksScreen in memory
    HBITMAP hbmClacksScreen; //hbitmap of the ClacksScreen
    BITMAP bmpClacksScreen;
    BITMAPINFOHEADER bi;
};

这已更新.我定义了一些函数,包括一个将位图写到磁盘的函数,该函数工作正常,捕获了屏幕,一个bmp被写到了磁盘,这就是我想要的函数.

This is updated. I have some functions defined, including one that writes the bitmap to disk, this works fine, the screen is captured and a bmp is written to disk and it's the one I wanted.

现在,我想将屏幕显示的HBITMAP直接转换为OpenCV2.1的cv :: Mat.

Now I want to convert the HBITMAP I take of the screen directly into a cv::Mat for OpenCV2.1.

这是可行的,只是图像是纯灰色的并且会崩溃.显然,对于c ++来说,我还是很漂亮的n00b,所以可能有些简单的事情让我不为所动.

It kind of works, except the image is pure grey and it crashes. Obviously I am still pretty n00b when it comes to c++ so there is probably something simple that I am just not groking.

static cv::Mat copyToCVMat(const ClacksScreen * s)
{
    cv::Mat image;
    image.create(s->bmpClacksScreen.bmWidth, s->bmpClacksScreen.bmHeight, CV_8UC4);
    GetDIBits(s->hdcMemDC, s->hbmClacksScreen, 0,
            (UINT)s->bmpClacksScreen.bmHeight,
            image.data,
            (BITMAPINFO *)&s->bi, DIB_RGB_COLORS);
    return image;
}

当我包装一个cv :: imwrite(image);在尝试捕获中,我得到一个错误的分配错误.显然,到现在为止,我们已经确定我不知道如何执行此操作,因此我们将不胜感激.

When I wrap a cv::imwrite(image); in a try catch, I get a bad allocation error. Obviously at this point we've established that I have no frelling clue how to do this, so any help would be appreciated.

更新

如果我运行以下代码:

try {
    cv::Mat screen = cv::imread("captureqwsx.jpg");
    if (!screen.data) {
        printf("no image data?");
    }
    cv::imwrite("out.jpg",screen);
} catch(std::exception e) {
    printf("Exception %s\n",e.what());
}

我得到输出:

没有图像数据?异常分配错误

no image data? Exception bad allocation

当我尝试运行高gui时,它与以前一样,写入磁盘的.jpg和.bmps都会出现问题,这些图像可以在图像查看器中查看,并且MS Paint可以正常显示.

When I try to run the high gui, it's the same as before, problem crops up for both .jpg and .bmps written to disk, which are viewable in image viewer and MS Paint fine.

我尝试使用完全不同的图像,一个网站上的.png,同样的问题.

I tried with a completely different image, a .png from a website, same issue.

那么我现在在做什么错了?

So what am I doing wrong at this point?

推荐答案

我编写了自己的函数来执行类似的操作,希望它可以为您提供帮助: 如何在OpenCV中捕获桌面(即,将位图转换为Mat)?

I wrote my own function to do something similar, hopefully it can help you: How to capture the desktop in OpenCV (ie. turn a bitmap into a Mat)?

这篇关于将HBITMAP转换为cv :: Mat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆