100分求助,用GetCurrentBuffer(&cbBuffer,(long*)pBuffer)截取了一帧视频,对pBuffer操作出现问题?谢谢
用GetCurrentBuffer(&cbBuffer,(long*)pBuffer)截取了一帧视频(24位RGB彩色),对pBuffer(BYTE型)操作出现问题。
我想直接对pBuffer处理,把这帧视频中的需要的一部分存取为灰度图像,得到的图像时好时坏
我用的方法如下:
int left,right,top,bottom;//需得到的图像在视频中的位置,其中视频分辨率为320x240
int w=right-left+1;
int h=bottom-top+1;
int Width = 320;//视频宽
int Heigth = 240;//视频高
BYTE* pGray = new BYTE[w*h];//需存取的灰度图像
long nPixel=0;
for (int p=Heigth-bottom-1;p<Heigth-top;p++)
{
for (int q=left;q<right+1;q++)
{
pGray[nPixel] = pBuffer[3*(p*Width+q)+1];//存取RGB中的G为灰度图象
nPixel++;
}
}
SavePhoto(pGray);//当SavePhoto存取整帧视频时没有问题
最可气的是存储结果时好时坏,不敢肯定for语句是否有错,请高手赐教,问题解决马上送分,谢谢!!!
其中 SavePhoto保存为bmp灰度图像
void SavePhoto (CString& str,BYTE* pBuffer,int width,int heigth)
{
HANDLE hf = CreateFile(str,GENERIC_WRITE, FILE_SHARE_READ,
NULL, CREATE_ALWAYS, NULL, NULL);
//照片为8位灰度图像
long cbGray=width*heigth;
// Write out the file header
BITMAPFILEHEADER bfh;
memset( &bfh, 0, sizeof( bfh ) );
bfh.bfType = 'MB';
bfh.bfSize = sizeof( bfh ) + cbGray + sizeof( BITMAPINFOHEADER )
+256*sizeof(RGBQUAD);
bfh.bfOffBits = sizeof(BITMAPINFOHEADER)+sizeof( BITMAPFILEHEADER )
+256*sizeof(RGBQUAD);
DWORD Written = 0;
WriteFile( hf, &bfh, sizeof( bfh ), &Written, NULL );
// Write the bitmap format
BITMAPINFOHEADER bih;
memset( &bih, 0, sizeof( bih ) );
bih.biSize = sizeof( bih );
bih.biWidth = width;
bih.biHeight = heigth;
bih.biPlanes = 1;
bih.biBitCount = 8;
WriteFile( hf, &bih, sizeof( bih ), &Written, NULL );
//构造一个颜色表
RGBQUAD lp[256];
for(int i=0;i<256;i++)
{
lp[i].rgbBlue=(BYTE)i;
lp[i].rgbGreen=(BYTE)i;
lp[i].rgbRed=(BYTE)i;
lp[i].rgbReserved=0;
}
WriteFile( hf, &lp, 256*sizeof(RGBQUAD), &Written, NULL );
// Write the bitmap bits
WriteFile( hf, pBuffer, cbGray, &Written, NULL );
CloseHandle(hf);
}