图像的灰度处理.docx
- 文档编号:30486330
- 上传时间:2023-08-15
- 格式:DOCX
- 页数:16
- 大小:18KB
图像的灰度处理.docx
《图像的灰度处理.docx》由会员分享,可在线阅读,更多相关《图像的灰度处理.docx(16页珍藏版)》请在冰豆网上搜索。
图像的灰度处理
图像的灰度处理
一、关键技术
灰度处理。
要将一个图片灰度处理,必须取出图片中每一个像素的灰度值,再将此点的颜色的红、绿、蓝成分都设置为灰度值。
(灰度值=红色×0.30+绿色×0.59+蓝色×0.11)取得某像素颜色可以通过API函数GetPixel完成;设置某点的颜色可以通过SetPixel完成,CDC类对它们进行了重新封装,原型如下:
(1)GetPixel
该函数用于取得某像素的颜色。
语法:
COLORREFGetPixel
(
intXPos,//X坐标
intnYPos//Y坐标
);
(2)SetPixel
该函数用于设置某像素的颜色。
语法:
COLORREFSetPixel
(
intx,//X坐标
inty,//Y坐标
COLORREFcrColor//要设置的颜色
);
怎样将一个颜色值中的红、绿、蓝成分取出呢?
将COLORREF转换成16进制看一下:
charcColor[6];
COLORREFcolor=RGB(128,96,64)
itoa(color,cColor,16);
此时,cColor地址中存储内容为0F406080,40就是10进制的64;60就是10进制的96;80就是10进制的128。
从上面的内容可以看出,颜色的存储格式实际是将红、绿、蓝成份按照B、R、G的格式存储的,根据这个规则,就可以取出某颜色的红、绿、蓝成份。
注:
这里使用的是BMP图像(因为它在MFC中显示简单些0_0)。
二关键代码
#if!
defined(AFX_GRAYPIC_H__219DC38F_FF6A_4047_88CC_CD183559D512__INCLUDED_)
#defineAFX_GRAYPIC_H__219DC38F_FF6A_4047_88CC_CD183559D512__INCLUDED_
#if_MSC_VER>1000
#pragmaonce
#endif//_MSC_VER>1000
#ifndef__AFXWIN_H__
#errorinclude'stdafx.h'beforeincludingthisfileforPCH
#endif
#include"resource.h"//mainsymbols
/////////////////////////////////////////////////////////////////////////////
//CGrayPicApp:
//SeeGrayPic.cppfortheimplementationofthisclass
//
classCGrayPicApp:
publicCWinApp
{
public:
CGrayPicApp();
//Overrides
//ClassWizardgeneratedvirtualfunctionoverrides
//{{AFX_VIRTUAL(CGrayPicApp)
public:
virtualBOOLInitInstance();
//}}AFX_VIRTUAL
//Implementation
//{{AFX_MSG(CGrayPicApp)
//NOTE-theClassWizardwilladdandremovememberfunctionshere.
//DONOTEDITwhatyouseeintheseblocksofgeneratedcode!
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
//MicrosoftVisualC++willinsertadditionaldeclarationsimmediatelybeforethepreviousline.
#endif//!
defined(AFX_GRAYPIC_H__219DC38F_FF6A_4047_88CC_CD183559D512__INCLUDED_)
//GrayPic.cpp:
Definestheclassbehaviorsfortheapplication.
//
#include"stdafx.h"
#include"GrayPic.h"
#include"GrayPicDlg.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
//CGrayPicApp
BEGIN_MESSAGE_MAP(CGrayPicApp,CWinApp)
//{{AFX_MSG_MAP(CGrayPicApp)
//NOTE-theClassWizardwilladdandremovemappingmacroshere.
//DONOTEDITwhatyouseeintheseblocksofgeneratedcode!
//}}AFX_MSG
ON_COMMAND(ID_HELP,CWinApp:
:
OnHelp)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//CGrayPicAppconstruction
CGrayPicApp:
:
CGrayPicApp()
{
//TODO:
addconstructioncodehere,
//PlaceallsignificantinitializationinInitInstance
}
/////////////////////////////////////////////////////////////////////////////
//TheoneandonlyCGrayPicAppobject
CGrayPicApptheApp;
/////////////////////////////////////////////////////////////////////////////
//CGrayPicAppinitialization
BOOLCGrayPicApp:
:
InitInstance()
{
AfxEnableControlContainer();
//Standardinitialization
//Ifyouarenotusingthesefeaturesandwishtoreducethesize
//ofyourfinalexecutable,youshouldremovefromthefollowing
//thespecificinitializationroutinesyoudonotneed.
#ifdef_AFXDLL
Enable3dControls();//CallthiswhenusingMFCinasharedDLL
#else
Enable3dControlsStatic();//CallthiswhenlinkingtoMFCstatically
#endif
CGrayPicDlgdlg;
m_pMainWnd=&dlg;
intnResponse=dlg.DoModal();
if(nResponse==IDOK)
{
//TODO:
Placecodeheretohandlewhenthedialogis
//dismissedwithOK
}
elseif(nResponse==IDCANCEL)
{
//TODO:
Placecodeheretohandlewhenthedialogis
//dismissedwithCancel
}
//Sincethedialoghasbeenclosed,returnFALSEsothatweexitthe
//application,ratherthanstarttheapplication'smessagepump.
returnFALSE;
}
//GrayPicDlg.h:
headerfile
#if!
defined(AFX_GRAYPICDLG_H__A7E96313_B9AE_44C5_9660_EB23633241E7__INCLUDED_)
#defineAFX_GRAYPICDLG_H__A7E96313_B9AE_44C5_9660_EB23633241E7__INCLUDED_
#if_MSC_VER>1000
#pragmaonce
#endif//_MSC_VER>1000
/////////////////////////////////////////////////////////////////////////////
//CGrayPicDlgdialog
classCGrayPicDlg:
publicCDialog
{
//Construction
public:
intXToTen(charchr);
intXToI(char*xNumber);
voidGetRGB(COLORREFcolor,int&R,int&G,int&B);
CGrayPicDlg(CWnd*pParent=NULL);//standardconstructor
//DialogData
//{{AFX_DATA(CGrayPicDlg)
enum{IDD=IDD_GRAYPIC_DIALOG};
CStaticm_StaGray;
//}}AFX_DATA
//ClassWizardgeneratedvirtualfunctionoverrides
//{{AFX_VIRTUAL(CGrayPicDlg)
protected:
virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDVsupport
//}}AFX_VIRTUAL
//Implementation
protected:
HICONm_hIcon;
//Generatedmessagemapfunctions
//{{AFX_MSG(CGrayPicDlg)
virtualBOOLOnInitDialog();
afx_msgvoidOnPaint();
afx_msgHCURSOROnQueryDragIcon();
virtualvoidOnOK();
afx_msgvoidOnTimer(UINTnIDEvent);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
//MicrosoftVisualC++willinsertadditionaldeclarationsimmediatelybeforethepreviousline.
#endif//!
defined(AFX_GRAYPICDLG_H__A7E96313_B9AE_44C5_9660_EB23633241E7__INCLUDED_)
//GrayPicDlg.cpp:
implementationfile
//
#include"stdafx.h"
#include"GrayPic.h"
#include"GrayPicDlg.h"
#include
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
//CGrayPicDlgdialog
CGrayPicDlg:
:
CGrayPicDlg(CWnd*pParent/*=NULL*/)
:
CDialog(CGrayPicDlg:
:
IDD,pParent)
{
//{{AFX_DATA_INIT(CGrayPicDlg)
//NOTE:
theClassWizardwilladdmemberinitializationhere
//}}AFX_DATA_INIT
//NotethatLoadIcondoesnotrequireasubsequentDestroyIconinWin32
m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
voidCGrayPicDlg:
:
DoDataExchange(CDataExchange*pDX)
{
CDialog:
:
DoDataExchange(pDX);
//{{AFX_DATA_MAP(CGrayPicDlg)
DDX_Control(pDX,IDC_STAGRAY,m_StaGray);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CGrayPicDlg,CDialog)
//{{AFX_MSG_MAP(CGrayPicDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//CGrayPicDlgmessagehandlers
BOOLCGrayPicDlg:
:
OnInitDialog()
{
CDialog:
:
OnInitDialog();
//Settheiconforthisdialog.Theframeworkdoesthisautomatically
//whentheapplication'smainwindowisnotadialog
SetIcon(m_hIcon,TRUE);//Setbigicon
SetIcon(m_hIcon,FALSE);//Setsmallicon
this->SetTimer(1,500,NULL);
//TODO:
Addextrainitializationhere
returnTRUE;//returnTRUEunlessyousetthefocustoacontrol
}
//Ifyouaddaminimizebuttontoyourdialog,youwillneedthecodebelow
//todrawtheicon.ForMFCapplicationsusingthedocument/viewmodel,
//thisisautomaticallydoneforyoubytheframework.
voidCGrayPicDlg:
:
OnPaint()
{
CPaintDCdc(this);
//Thesystemcallsthistoobtainthecursortodisplaywhiletheuserdrags
//theminimizedwindow.
}
HCURSORCGrayPicDlg:
:
OnQueryDragIcon()
{
return(HCURSOR)m_hIcon;
}
voidCGrayPicDlg:
:
OnOK()
{
//CDialog:
:
OnOK();
}
voidCGrayPicDlg:
:
GetRGB(COLORREFcolor,int&R,int&G,int&B)
{
charcRGB[6];
itoa(color,cRGB,16);
charcR[2],cG[2],cB[2];
if(strlen(cRGB)==5)
{
strcpy(cB,cRGB);
strcpy(cG,&cRGB[1]);
strcpy(cR,&cRGB[3]);
cB[1]='\0';
cG[2]='\0';
}
else
{
strcpy(cB,cRGB);
strcpy(cG,&cRGB[2]);
strcpy(cR,&cRGB[4]);
cB[2]='\0';
cG[2]='\0';
}
R=XToI(cR);
G=XToI(cG);
B=XToI(cB);
}
intCGrayPicDlg:
:
XToI(char*xNumber)
{
charchr;
intnNumber=0,nValue=0,nTmp=0;
for(inti=strlen(xNumber);i>0;i--)
{
chr=xNumber[strlen(xNumber)-i];
nNumber=XToTen(chr);
intcc=pow(16,i-1);
nTmp=nNumber*pow(16,i-1);
nValue=nValue+nTmp;
}
returnnValue;
}
intCGrayPicDlg:
:
XToTen(charchr)
{
inti;
if(chr=='0')
i=0;
if(chr=='1')
i=1;
if(chr=='2')
i=2;
if(chr=='3')
i=3;
if(chr=='4')
i=4;
if(chr=='5')
i=5;
if(chr=='6')
i=6;
if(chr=='7')
i=7;
if(chr=='8')
i=8;
if(chr=='9')
i=9;
if(chr=='a'||chr=='A')
i=10;
if(chr=='b'||chr=='B')
i=11;
if(chr=='c'||chr=='C')
i=12;
if(chr=='d'||chr=='D')
i=13;
if(chr=='e'||chr=='E')
i=14;
if(chr=='f'||chr=='F')
i=15;
returni;
}
//DELvoidCGrayPicDlg:
:
OnButton3()
//DEL{
//DEL}
voidCGrayPicDlg:
:
OnTimer(UINTnIDEvent)
{
this->KillTimer
(1);
CDC*pDC=this->m_StaGray.GetDC();
CDCMemDC;
CBitmapbmp;
bmp.LoadBitmap(IDB_BITMAP1);
BITMAPBmpStruct;
MemDC.CreateCompatibleDC(pDC);
MemDC.SelectObject(&bmp);
bmp.GetBitmap(&BmpStruct);
COLORREFcolor;
intnR,nG,nB;
floatfGray;
for(intx=0;x { for(inty=0;y { color=MemDC.GetPixel(x,y); GetRGB(color,nR,nG,nB); fGray=(float)nR*0.30+(float)nG*0.59+(float)nB*0.11; pDC->SetPixel(x,y,RGB(fGray,fGray,fGray)); } } CDialog: : OnTimer(nIDEvent); }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 图像 灰度 处理