C++ 中的位图操作在 Windows 上

发布于 2024-08-25 17:01:43 字数 381 浏览 4 评论 0原文

我自己有一个在 Windows 上用 C++ 编写的位图的句柄:

HBITMAP hBitmap;

在这个图像上我想做一些图像识别、模式分析之类的事情。在我在大学的学习中,我在 Matlab 中完成了这项工作,根据各个像素的位置很容易获得它们,但我不知道如何在 Windows 下的 C++ 中做到这一点 - 我还没有真正能够做到了解我到目前为止所读到的内容。我见过一些对漂亮的 Bitmap 类的引用,它可以让您 setPixel() 和 getPixel() 之类的东西,但我认为这是 .net 的。

我应该如何将 HBITMAP 变成我可以轻松使用的东西?我需要能够获取 RGBA 信息。是否有库允许我处理数据而无需了解 DC 和 BitBlt 之类的东西?

I have myself a handle to a bitmap, in C++, on Windows:

HBITMAP hBitmap;

On this image I want to do some Image Recognition, pattern analysis, that sort of thing. In my studies at University, I have done this in Matlab, it is quite easy to get at the individual pixels based on their position, but I have no idea how to do this in C++ under Windows - I haven't really been able to understand what I have read so far. I have seen some references to a nice looking Bitmap class that lets you setPixel() and getPixel() and that sort of thing, but I think this is with .net .

How should I go about turning my HBITMAP into something I can play with easily? I need to be able to get at the RGBA information. Are there libraries that allow me to work with the data without having to learn about DCs and BitBlt and that sort of thing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

失与倦" 2024-09-01 17:01:43

您可以使用 OpenCV 库作为完整的图像处理工具。

您还可以使用 MFC 的 CImage 或 VCL 的 TBitmap 只是从 HBITMAP 中提取像素值。

You can use OpenCV library as a full image processing tool.

You can also use MFC's CImage or VCL's TBitmap just to extract pixel values from HBITMAP.

何以畏孤独 2024-09-01 17:01:43
Gdiplus::Bitmap* pBitmap = Gdiplus::Bitmap::FromHBITMAP( hBitmap, NULL );
Gdiplus::Color pixel_color;
pBitmap->GetPixel( x, y, &pixel_color ); // read pixel at x,y into pixel_color
// ...
delete pBitmap; // do not forget to delete
Gdiplus::Bitmap* pBitmap = Gdiplus::Bitmap::FromHBITMAP( hBitmap, NULL );
Gdiplus::Color pixel_color;
pBitmap->GetPixel( x, y, &pixel_color ); // read pixel at x,y into pixel_color
// ...
delete pBitmap; // do not forget to delete
想挽留 2024-09-01 17:01:43

使用 GetPixel 尝试此操作来自 GDI 的强>:

COLORREF GetBitmapBixel(HBITMAP hBitmap, int xPos, int yPos)
{
  HDC hDC = GetDC(NULL);
  HDC hMemDC = CreateCompatibleDC(hDC);
  COLORREF pixelColor;
  HBITMAP hOld = (HBITMAP)SelectObject(hMemDC, hBitmap);

  pixelColor = ::GetPixel(hMemDC, xPos, yPos);

  SelectObject(hMemDC, hOld);
  DeleteDC(hMemDC);
  ReleaseDC(NULL, hDC);

  return pixelColor;
}

Try this using GetPixel from GDI:

COLORREF GetBitmapBixel(HBITMAP hBitmap, int xPos, int yPos)
{
  HDC hDC = GetDC(NULL);
  HDC hMemDC = CreateCompatibleDC(hDC);
  COLORREF pixelColor;
  HBITMAP hOld = (HBITMAP)SelectObject(hMemDC, hBitmap);

  pixelColor = ::GetPixel(hMemDC, xPos, yPos);

  SelectObject(hMemDC, hOld);
  DeleteDC(hMemDC);
  ReleaseDC(NULL, hDC);

  return pixelColor;
}
执手闯天涯 2024-09-01 17:01:43

使用:

DIBSECTION ds;
::GetObject(hbmp/*your HBITMAP*/, sizeof DIBSECTION, &ds);

您将在 ds.dsBm 中获得所需的所有内容(包括像素格式和像素缓冲区地址)。
请参阅文档

With:

DIBSECTION ds;
::GetObject(hbmp/*your HBITMAP*/, sizeof DIBSECTION, &ds);

you will get all you need (including pixel format and pixel buffer adress) in ds.dsBm.
see the doc

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文