如何访问位图中的像素颜色
How to access pixel color within a bitmap?
我搜索了一下,知道我必须使用GetDIBits()
。我不知道该如何处理LPVOID lpvBits
out参数。
有人能给我解释一下吗?我需要获得二维矩阵形式的像素颜色信息,这样我就可以检索特定(x,y)坐标对的信息。
我正在使用Win32 API用C++编程。
首先需要一个位图并打开它
HBITMAP hBmp = (HBITMAP) LoadImage(GetModuleHandle(NULL), _T("test.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if(!hBmp) // failed to load bitmap
return false;
//getting the size of the picture
BITMAP bm;
GetObject(hBmp, sizeof(bm), &bm);
int width(bm.bmWidth),
height(bm.bmHeight);
//creating a bitmapheader for getting the dibits
BITMAPINFOHEADER bminfoheader;
::ZeroMemory(&bminfoheader, sizeof(BITMAPINFOHEADER));
bminfoheader.biSize = sizeof(BITMAPINFOHEADER);
bminfoheader.biWidth = width;
bminfoheader.biHeight = -height;
bminfoheader.biPlanes = 1;
bminfoheader.biBitCount = 32;
bminfoheader.biCompression = BI_RGB;
bminfoheader.biSizeImage = width * 4 * height;
bminfoheader.biClrUsed = 0;
bminfoheader.biClrImportant = 0;
//create a buffer and let the GetDIBits fill in the buffer
unsigned char* pPixels = new unsigned char[(width * 4 * height)];
if( !GetDIBits(CreateCompatibleDC(0), hBmp, 0, height, pPixels, (BITMAPINFO*) &bminfoheader, DIB_RGB_COLORS)) // load pixel info
{
//return if fails but first delete the resources
DeleteObject(hBmp);
delete [] pPixels; // delete the array of objects
return false;
}
int x, y; // fill the x and y coordinate
unsigned char r = pPixels[(width*y+x) * 4 + 2];
unsigned char g = pPixels[(width*y+x) * 4 + 1];
unsigned char b = pPixels[(width*y+x) * 4 + 0];
//clean up the bitmap and buffer unless you still need it
DeleteObject(hBmp);
delete [] pPixels; // delete the array of objects
简而言之,lpvBits-out参数是指向像素的指针但是如果它只有1个像素,我建议使用getpixel来
我不确定这是否是您想要的,但GetPixel几乎满足了您的需求。。。至少我可以从函数的描述中看出
相关文章:
- 如何在24位SDL_Surface上设置像素的颜色
- 访问和更改基于另一个图像的像素颜色-opencv c++
- 无法获取窗口 C++ 的像素颜色
- 如何使用stb_image将像素颜色数据写入 BMP 图像文件?
- 从SDL_Texture访问像素颜色
- 结构像素颜色值
- 将OpenCV中的像素颜色映射到预定的颜色列表中
- magick 获得像素颜色.PixelPacket似乎不再存在了
- 在数组中设置像素颜色
- 在 openCV 中更改单个像素颜色
- 获取图像的像素颜色
- 使用 BMP 作为高度图.如何访问像素颜色以设置高度
- 以最快的方式获取像素颜色
- glsl-获取像素颜色[像素着色器]
- 如何访问位图中的像素颜色
- 快板定义像素颜色
- 使用像素颜色的人工神经网络
- 使用magick++获取像素颜色
- FreeImage:获取像素颜色
- 从指定坐标[x, y]处的位图数据获取像素颜色