Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

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.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top