1,317
社区成员
发帖
与我相关
我的任务
分享// 位图图像转灰度
void __fastcall CrnBitmap2GrayScale(Graphics::TBitmap *bmp)
{
BYTE btGray;
bmp->PixelFormat = pf24bit;
for(int y=0; y<bmp->Height; y++)
{
BYTE *pLine = (BYTE *)bmp->ScanLine[y];
for(int x=0; x<bmp->Width; x++)
{
// 平均值灰度
// btGray = (pLine[0] + pLine[1] + pLine[2]) / 3;
// 最大值
// btGray = Max(pLine[2], Max(pLine[0], pLine[1]));
// YUV值
btGray = 0.30 * pLine[0] + 0.59 * pLine[1] + 0.11 * pLine[2];
// 63 63 72 75 6E 2E 63 6F 6D
pLine[0] = btGray;
pLine[1] = btGray;
pLine[2] = btGray;
pLine += 3;
}
}
}
//注释掉那部分就不会有问题了
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Graphics::TBitmap *pBitmap = new Graphics::TBitmap();
//This example shows drawing directly to the Bitmap
Byte *ptr;
try
{
pBitmap->LoadFromFile("C:\\bb.bmp");
/*
for (int y = 0; y < pBitmap->Height; y++)
{
ptr = (Byte *)pBitmap->ScanLine[y];
for (int x = 0; x < pBitmap->Width; x++)
ptr[x] = (Byte)y;
}
*/
Canvas->Draw(10,10,pBitmap);
}
catch (...)
{
ShowMessage("Could not load or alter bitmap");
}
delete pBitmap;
}