How to convert rgb to grayscale....

Status
Not open for further replies.

voon

Newbie level 2
Joined
Dec 6, 2004
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
38
hi, there...i doing & rushing my final year project...i need to know how to convert rgb image to grayscale image in c++ MFC...below is my code...but..it can't work...why leh...tis code is in a button called Normalization...after the image display the image will be change to grayscale after i click the Normalization button...the load & display image i already done...so...it just the convert color haven't fix...so...can you help me to modify my code for grayscale...thanks a lot for helping me & replying..hope hear from you soon...if can't see my codes below..then..pls download the zip file i sent...tis zip file is my c++ MFC program...thanks...


void CFingerprintSystem_FASSDlg::OnNormalization()
{
// TODO: Add your control notification handler code here

CString m_sBitmap;

HBITMAP hGrayBitmap = (HBITMAP) ::LoadImage(AfxGetInstanceHandle(),
m_sBitmap, IMAGE_BITMAP, 100,140, LR_LOADFROMFILE |
LR_CREATEDIBSECTION);

BITMAP bm;

GetObject(hGrayBitmap, sizeof(BITMAP), (LPSTR)&bm);

typedef struct tagBITMAP
{
LONG bmType;
LONG bmWidth;
LONG bmHeight;
LONG bmWidthBytes;
WORD bmPlanes;
WORD bmBitsPixel;
LPVOID bmBits;
}
BITMAP, *PBITMAP;


if (hGrayBitmap)
{
CStatic *pImg = (CStatic*)GetDlgItem(IDC_STATIC_Processed);
BYTE * pImgByte = (BYTE *) bm.bmBits;



if(pImg==NULL)
{
AfxMessageBox("Fail to convert grayscale");
}
else
{

INT iWidthBytes = bm.bmWidth * 3;

for ( int y = 0; y < bm.bmHeight; y++)
{
for ( int x = 0; x < bm.bmWidth*3; x++)
{

unsigned char R = pImgByte[y*iWidthBytes+x+2];
unsigned char G = pImgByte[y*iWidthBytes+x+1];
unsigned char B = pImgByte[y*iWidthBytes+x];;

INT gray = ceil(0.3*R + 0.59*G + 0.11*B);

pImgByte[y*iWidthBytes+x+2] = gray;
pImgByte[y*iWidthBytes+x+1] = gray;
pImgByte[y*iWidthBytes+x] = gray;
}
}

pImg->SetBitmap(hGrayBitmap);

}
}
}
Code:
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…