繁体   English   中英

在cv :: Mat中捕获部分屏幕

[英]Capturing part of the screen in cv::Mat

我尝试捕获屏幕的一部分并将其放入cv::Mat ,输出是相同的颜色(当我捕获白色背景时,它是白色的,当灰色时它是灰色的,因此您可以理解它在某种程度上确实起作用),但图像模糊,对齐且重复(重复相同的像素)。

例如,在我的Chrome浏览器中写下此问题后,我尝试了该程序,结果是: 模糊输出

您可以识别浏览器(URL栏,文本框和按钮等。但是图像本身远不能准确甚至接近,并且像素在不应该重复时是重复的(URL栏重复其本身,文本框等)。

问题中的代码未在我的计算机上编译。 但是我知道我使用的代码(不是OpenCV部分)可以在我的机器上工作(我已经使用它捕获了屏幕,并且当我使用libpng保存它时,它可以完美地工作)。

我的代码如下(此处未声明的变量在其他地方声明,并且我检查了其值是正确的,例如xwh等):

    HDC hdcSource = GetDC(NULL);
    HDC hdcMemory = CreateCompatibleDC(hdcSource);
    HBITMAP hBitmap = CreateCompatibleBitmap(hdcSource, w, h);
    HBITMAP hBitmapOld = (HBITMAP)SelectObject(hdcMemory, hBitmap);
    BITMAPINFOHEADER bmi = { 0 };
    bmi.biSize = sizeof(BITMAPINFOHEADER);
    bmi.biPlanes = 1;
    bmi.biBitCount = 24;

    bmi.biWidth = w;
    bmi.biHeight = -h;
    bmi.biCompression = BI_RGB;

    bmi.biSizeImage = ((bmi.biWidth * bmi.biBitCount + 31) & ~31) / 8 * bmi.biHeight<0 ? -bmi.biHeight : bmi.biHeight;
    bmi.biXPelsPerMeter = 0;
    bmi.biYPelsPerMeter = 0;
    bmi.biClrImportant = 0;
    bmi.biClrUsed = 256;

    if (!(BitBlt(hdcMemory, 0, 0, w, h, hdcSource, p1.x, p1.y, SRCCOPY)))
    {
        exit(1);
    }
    hBitmap = (HBITMAP)SelectObject(hdcMemory, hBitmapOld);
    if (!hBitmap)
    {
        exit(1);
    }

    my_pic.create(h, w, CV_8UC4);

    //StretchBlt(hdcSource, 0, 0, w, h, GetDC(NULL), 0, 0, w, h, SRCCOPY); //This line is what I saw in the other question but it didn't help

    if (!(GetDIBits(hdcSource, hBitmap, 0, h, my_pic.data, (BITMAPINFO*)&bmi, DIB_RGB_COLORS)))
    {
        exit(1);
    }
    DeleteDC(hdcSource);
    DeleteDC(hdcMemory);

您具有用于位图像素的RGB格式,但是您尝试使其适合CV_8UC4(四通道)矩阵。 尝试使用CV_8UC3矩阵类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM