program konversi citra berwarna menjadi citra grayscale
DESCRIPTION
TRANSCRIPT
UJIAN TENGAH SEMESTERPROGRAM KONVERSI CITRA BERWARNA MENJADI
CITRA GRAYSCALE
D
I
S
U
S
U
N
OLEH
KELOMPOK III:1. LESMARDIN HASUGIAN (1011210)2. ISKA PALENTA PURBA (1011525)
DOSEN : INDRA SARKIS SIMAMORA, ST, M.KOM
PROGRAM STUDI TEKNIK INFORMATIKA
STMIK BUDIDARMA MEDAN
2013
PROGRAM KONVERSI CITRA BERWARNA MENJADI CITRA GRAY SCALE
Coding Untuk PCitra.cpp
// PCitraDlg.cpp : implementation file//
#include "stdafx.h"#include "PCitra.h"#include "PCitraDlg.h"
#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif
/////////////////////////////////////////////////////////////////////////////// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog{public:
CAboutDlg();
// Dialog Data//{{AFX_DATA(CAboutDlg)enum { IDD = IDD_ABOUTBOX };//}}AFX_DATA
// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CAboutDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV
support//}}AFX_VIRTUAL
// Implementation
protected://{{AFX_MSG(CAboutDlg)//}}AFX_MSGDECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD){
//{{AFX_DATA_INIT(CAboutDlg)//}}AFX_DATA_INIT
}// merubah data pixel ke RGBvoid WarnaToRGB(long int warna,int *Red, int *Green, int *Blue){*Red = warna & 0x000000FF;*Green = (warna & 0x0000FF00) >> 8;*Blue = (warna & 0x00FF0000) >> 16;
}//merubah RGB ke data pixellong int RGBToWarna(int Red, int Green, int Blue){return(Red+(Green<<8)+(Blue<<16));}void CAboutDlg::DoDataExchange(CDataExchange* pDX){
CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CAboutDlg)//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////// CPCitraDlg dialog
CPCitraDlg::CPCitraDlg(CWnd* pParent /*=NULL*/): CDialog(CPCitraDlg::IDD, pParent)
{//{{AFX_DATA_INIT(CPCitraDlg)m_label = _T("");//}}AFX_DATA_INIT// Note that LoadIcon does not require a subsequent DestroyIcon in
Win32m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CPCitraDlg::DoDataExchange(CDataExchange* pDX){
CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CPCitraDlg)DDX_Control(pDX, IDC_gray5, m_gray5);DDX_Control(pDX, IDC_gray4, m_gray4);DDX_Control(pDX, IDC_gray3, m_gray3);DDX_Control(pDX, IDC_gray2, m_gray2);DDX_Control(pDX, IDC_gray1, m_gray1);DDX_Control(pDX, IDC_Biner, m_biner);DDX_Control(pDX, IDC_Gray, m_btngray);DDX_Control(pDX, IDC_kata, m_kata);DDX_Control(pDX, IDC_picture, m_pic1);DDX_Control(pDX, IDC_BUTTON1, m_btn1);DDX_Text(pDX, IDC_Label, m_label);//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPCitraDlg, CDialog)//{{AFX_MSG_MAP(CPCitraDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BUTTON1, OnButton1)ON_BN_CLICKED(IDC_kata, Onkata)ON_BN_CLICKED(IDC_Gray, OnGray)ON_BN_CLICKED(IDC_Biner, OnBiner)
ON_BN_CLICKED(IDC_gray1, Ongray1)ON_BN_CLICKED(IDC_gray2, Ongray2)ON_BN_CLICKED(IDC_gray3, Ongray3)ON_BN_CLICKED(IDC_gray4, Ongray4)ON_BN_CLICKED(IDC_gray5, Ongray5)//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////// CPCitraDlg message handlers
BOOL CPCitraDlg::OnInitDialog(){
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != NULL){
CString strAboutMenu;strAboutMenu.LoadString(IDS_ABOUTBOX);if (!strAboutMenu.IsEmpty()){
pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX,
strAboutMenu);}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialogSetIcon(m_hIcon, TRUE); // Set big iconSetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control}
void CPCitraDlg::OnSysCommand(UINT nID, LPARAM lParam){
if ((nID & 0xFFF0) == IDM_ABOUTBOX){
CAboutDlg dlgAbout;dlgAbout.DoModal();
}else{
CDialog::OnSysCommand(nID, lParam);}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,// this is automatically done for you by the framework.
void CPCitraDlg::OnPaint() {
if (IsIconic()){
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangleint cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icondc.DrawIcon(x, y, m_hIcon);
}else{
CDialog::OnPaint();}
}
// The system calls this to obtain the cursor to display while the user drags// the minimized window.HCURSOR CPCitraDlg::OnQueryDragIcon(){
return (HCURSOR) m_hIcon;}
void CPCitraDlg::OnButton1() {
// TODO: Add your control notification handler code hereCDC* pDC = m_pic1.GetDC();//
CDC dcMem1;CRect rect;//kotak di pictureBITMAP bm;//HBITMAPhBitmap=(HBITMAP)::LoadImage(AfxGetInstanceHandle(),"123.bmp",IMAGE_BITMAP, 0, 0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);if(hBitmap){if(m_bmpBitmap.DeleteObject())m_bmpBitmap.Detach();m_bmpBitmap.Attach(hBitmap);}m_pic1.GetClientRect(rect);//m_bmpBitmap.GetBitmap(&bm);//dcMem1.CreateCompatibleDC(pDC);dcMem1.SelectObject(&m_bmpBitmap);pDC->StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem1,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);//}
void CPCitraDlg::Onkata()
{// TODO: Add your control notification handler code hereSetDlgItemText(IDC_Label,"Iska Palenta Purba, Lesmardin Hasugian");
}
void CPCitraDlg::OnGray() {
SetDlgItemText(IDC_grayscale,"x = (r + g + b) / 3 ");int i,j,red,green,blue,gray;long int warna,warnagray;CDC* pDC = m_pic1.GetDC();CDC dcMem1;CRect rect;BITMAP bm;HBITMAP
hBitmap=(HBITMAP)::LoadImage(AfxGetInstanceHandle(),"123.bmp",IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|
LR_CREATEDIBSECTION);if(hBitmap)
{if(m_bmpBitmap.DeleteObject())m_bmpBitmap.Detach();m_bmpBitmap.Attach(hBitmap);}
m_pic1.GetClientRect(rect);m_bmpBitmap.GetBitmap(&bm);dcMem1.CreateCompatibleDC(pDC);dcMem1.SelectObject(&m_bmpBitmap);for(i=0;i<bm.bmHeight;i++)for(j=0;j<bm.bmWidth;j++)
{warna=dcMem1.GetPixel(j,i);WarnaToRGB(warna,&red,&green,&blue);gray=int(red+green+blue)/3;warnagray=RGBToWarna(gray,gray,gray);dcMem1.SetPixel(j,i,warnagray);}
pDC->StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem1,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);//
}
void CPCitraDlg::OnBiner() {
// TODO: Add your control notification handler code hereSetDlgItemText(IDC_tobiner,"x = 0.42r + 0.32g + 0.28b");int i,j,red,green,blue,gray;
long int warna,warnagray,ratagray;CDC* pDC = m_pic1.GetDC();CDC dcMem1;CRect rect;BITMAP bm;HBITMAP hBitmap=(HBITMAP)::LoadImage(AfxGetInstanceHandle(),"123.bmp",IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|
LR_CREATEDIBSECTION);if(hBitmap){if(m_bmpBitmap.DeleteObject())m_bmpBitmap.Detach();m_bmpBitmap.Attach(hBitmap);}
m_pic1.GetClientRect(rect);m_bmpBitmap.GetBitmap(&bm);dcMem1.CreateCompatibleDC(pDC);dcMem1.SelectObject(&m_bmpBitmap);for(i=0;i<bm.bmHeight;i++)for(j=0;j<bm.bmWidth;j++)
{warna=dcMem1.GetPixel(j,i);WarnaToRGB(warna,&red,&green,&blue);gray=int(red+green+blue)/3;ratagray+=gray;}
ratagray=ratagray/(bm.bmHeight*bm.bmWidth);for(i=0;i<bm.bmHeight;i++)for(j=0;j<bm.bmWidth;j++)
{warna=dcMem1.GetPixel(j,i);WarnaToRGB(warna,&red,&green,&blue);gray=int(red+green+blue)/3;if(gray<128) gray=0;else gray=255;warnagray=RGBToWarna(gray,gray,gray);dcMem1.SetPixel(j,i,warnagray);}
pDC->StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem1,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);//
}
void CPCitraDlg::Ongray1() {
// TODO: Add your control notification handler code here//SetDlgItemText(IDC_Label,"Iska Palenta Purba, Lesmardin Hasugian");SetDlgItemText(IDC_thresholding1,"x = 0.5r + 0.2g + 0.3b");int i,j,red,green,blue,gray;
long int warna,warnagray;CDC* pDC = m_pic1.GetDC();CDC dcMem1;CRect rect;BITMAP bm;HBITMAP
hBitmap=(HBITMAP)::LoadImage(AfxGetInstanceHandle(),"123.bmp",IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|
LR_CREATEDIBSECTION);if(hBitmap)
{if(m_bmpBitmap.DeleteObject())m_bmpBitmap.Detach();m_bmpBitmap.Attach(hBitmap);}
m_pic1.GetClientRect(rect);m_bmpBitmap.GetBitmap(&bm);dcMem1.CreateCompatibleDC(pDC);dcMem1.SelectObject(&m_bmpBitmap);for(i=0;i<bm.bmHeight;i++)for(j=0;j<bm.bmWidth;j++)
{warna=dcMem1.GetPixel(j,i);WarnaToRGB(warna,&red,&green,&blue);gray=int((red*0.5)+(green*0.2)+(blue*0.3)); //Proses Pertamawarnagray=RGBToWarna(gray,gray,gray);dcMem1.SetPixel(j,i,warnagray);
}pDC->StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem1,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);//}
void CPCitraDlg::Ongray2() {
// TODO: Add your control notification handler code hereSetDlgItemText(IDC_thresholding2,"x = 0.2r + 0.5g + 0.3b");int i,j,red,green,blue,gray;
long int warna,warnagray;CDC* pDC = m_pic1.GetDC();CDC dcMem1;CRect rect;BITMAP bm;HBITMAP
hBitmap=(HBITMAP)::LoadImage(AfxGetInstanceHandle(),"123.bmp",IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|
LR_CREATEDIBSECTION);if(hBitmap)
{if(m_bmpBitmap.DeleteObject())m_bmpBitmap.Detach();m_bmpBitmap.Attach(hBitmap);}
m_pic1.GetClientRect(rect);m_bmpBitmap.GetBitmap(&bm);dcMem1.CreateCompatibleDC(pDC);dcMem1.SelectObject(&m_bmpBitmap);for(i=0;i<bm.bmHeight;i++)for(j=0;j<bm.bmWidth;j++)
{warna=dcMem1.GetPixel(j,i);WarnaToRGB(warna,&red,&green,&blue);gray=int((red*0.2)+(green*0.5)+(blue*0.3)); //Proses Pertamawarnagray=RGBToWarna(gray,gray,gray);dcMem1.SetPixel(j,i,warnagray);}
pDC->StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem1,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);//}
void CPCitraDlg::Ongray3() {
// TODO: Add your control notification handler code hereSetDlgItemText(IDC_thresholding3,"x = 0.2r + 0.2g + 0.5b");int i,j,red,green,blue,gray;
long int warna,warnagray;CDC* pDC = m_pic1.GetDC();CDC dcMem1;CRect rect;BITMAP bm;HBITMAP
hBitmap=(HBITMAP)::LoadImage(AfxGetInstanceHandle(),"123.bmp",IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|
LR_CREATEDIBSECTION);if(hBitmap)
{if(m_bmpBitmap.DeleteObject())m_bmpBitmap.Detach();m_bmpBitmap.Attach(hBitmap);
}m_pic1.GetClientRect(rect);m_bmpBitmap.GetBitmap(&bm);dcMem1.CreateCompatibleDC(pDC);dcMem1.SelectObject(&m_bmpBitmap);for(i=0;i<bm.bmHeight;i++)for(j=0;j<bm.bmWidth;j++)
{warna=dcMem1.GetPixel(j,i);WarnaToRGB(warna,&red,&green,&blue);gray=int((red*0.2)+(green*0.2)+(blue*0.5)); //Proses Pertamawarnagray=RGBToWarna(gray,gray,gray);dcMem1.SetPixel(j,i,warnagray);}
pDC->StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem1,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);//}
void CPCitraDlg::Ongray4() {
// TODO: Add your control notification handler code hereSetDlgItemText(IDC_thresholding4,"x = 0.5r + 0.5g + 0b");int i,j,red,green,blue,gray;
long int warna,warnagray;CDC* pDC = m_pic1.GetDC();CDC dcMem1;CRect rect;BITMAP bm;HBITMAP
hBitmap=(HBITMAP)::LoadImage(AfxGetInstanceHandle(),"123.bmp",IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|
LR_CREATEDIBSECTION);if(hBitmap)
{if(m_bmpBitmap.DeleteObject())m_bmpBitmap.Detach();m_bmpBitmap.Attach(hBitmap);}
m_pic1.GetClientRect(rect);m_bmpBitmap.GetBitmap(&bm);dcMem1.CreateCompatibleDC(pDC);dcMem1.SelectObject(&m_bmpBitmap);for(i=0;i<bm.bmHeight;i++)for(j=0;j<bm.bmWidth;j++)
{warna=dcMem1.GetPixel(j,i);WarnaToRGB(warna,&red,&green,&blue);gray=int((red*0.5)+(green*0.5)+(blue*0.0)); //Proses Pertamawarnagray=RGBToWarna(gray,gray,gray);dcMem1.SetPixel(j,i,warnagray);}
pDC->StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem1,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);//}
void CPCitraDlg::Ongray5() {
// TODO: Add your control notification handler code hereSetDlgItemText(IDC_thresholding5,"x = 0.5r + 0g + 0.5b");int i,j,red,green,blue,gray;
long int warna,warnagray;
CDC* pDC = m_pic1.GetDC();CDC dcMem1;CRect rect;BITMAP bm;HBITMAP
hBitmap=(HBITMAP)::LoadImage(AfxGetInstanceHandle(),"123.bmp",IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|
LR_CREATEDIBSECTION);if(hBitmap)
{if(m_bmpBitmap.DeleteObject())m_bmpBitmap.Detach();m_bmpBitmap.Attach(hBitmap);}
m_pic1.GetClientRect(rect);m_bmpBitmap.GetBitmap(&bm);dcMem1.CreateCompatibleDC(pDC);dcMem1.SelectObject(&m_bmpBitmap);for(i=0;i<bm.bmHeight;i++)for(j=0;j<bm.bmWidth;j++)
{warna=dcMem1.GetPixel(j,i);WarnaToRGB(warna,&red,&green,&blue);gray=int((red*0.5)+(green*0.0)+(blue*0.5)); //Proses Pertamawarnagray=RGBToWarna(gray,gray,gray);dcMem1.SetPixel(j,i,warnagray);}
pDC->StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem1,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);//}
Coding Untuk PCitra.h
// PCitra.h : main header file for the PCITRA application//
#if !defined(AFX_PCITRA_H__EE7756C6_BD89_461D_907B_0E8335848A1C__INCLUDED_)#define AFX_PCITRA_H__EE7756C6_BD89_461D_907B_0E8335848A1C__INCLUDED_
#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000
#ifndef __AFXWIN_H__#error include 'stdafx.h' before including this file for PCH
#endif
#include "resource.h" // main symbols
/////////////////////////////////////////////////////////////////////////////// CPCitraApp:// See PCitra.cpp for the implementation of this class//
class CPCitraApp : public CWinApp{public:
CPCitraApp();
// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CPCitraApp)public:virtual BOOL InitInstance();//}}AFX_VIRTUAL
// Implementation
//{{AFX_MSG(CPCitraApp)// NOTE - the ClassWizard will add and remove member functions
here.// DO NOT EDIT what you see in these blocks of generated
code !//}}AFX_MSGDECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_PCITRA_H__EE7756C6_BD89_461D_907B_0E8335848A1C__INCLUDED_)
Coding Untuk PCitraDlg.h
// PCitraDlg.h : header file//
#if !defined(AFX_PCITRADLG_H__3AA549B8_E430_4D3B_84C1_C757C58CDD31__INCLUDED_)#define AFX_PCITRADLG_H__3AA549B8_E430_4D3B_84C1_C757C58CDD31__INCLUDED_
#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000
/////////////////////////////////////////////////////////////////////////////// CPCitraDlg dialog
class CPCitraDlg : public CDialog{// Constructionpublic:
CPCitraDlg(CWnd* pParent = NULL); // standard constructorCBitmap m_bmpBitmap;// Dialog Data
//{{AFX_DATA(CPCitraDlg)
enum { IDD = IDD_PCITRA_DIALOG };CButton m_gray5;CButton m_gray4;CButton m_gray3;CButton m_gray2;CButton m_gray1;CButton m_biner;CButton m_btngray;CButton m_kata;CStatic m_pic1;CButton m_btn1;CString m_label;//}}AFX_DATA
// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CPCitraDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV
support//}}AFX_VIRTUAL
// Implementationprotected:
HICON m_hIcon;
// Generated message map functions//{{AFX_MSG(CPCitraDlg)virtual BOOL OnInitDialog();afx_msg void OnSysCommand(UINT nID, LPARAM lParam);afx_msg void OnPaint();afx_msg HCURSOR OnQueryDragIcon();afx_msg void OnButton1();afx_msg void Onkata();afx_msg void OnGray();afx_msg void OnBiner();afx_msg void Ongray1();afx_msg void Ongray2();afx_msg void Ongray3();afx_msg void Ongray4();afx_msg void Ongray5();//}}AFX_MSGDECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_PCITRADLG_H__3AA549B8_E430_4D3B_84C1_C757C58CDD31__INCLUDED_)
Outputnya adalah sebagai berikut :
Load gambar Grayscale
Biner Thresholding1
Thresholding2 Thresholding3
Thresholding4 Thresholding 5