图像处理---黑白化

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

核心对象:

None.gif CImagem_imageFile;

绘制图片:

None.gif void CFigureView::OnDraw(CDC * pDC)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifCFigureDoc
*pDoc=GetDocument();
InBlock.gifASSERT_VALID(pDoc);
InBlock.gif
if(!pDoc)
InBlock.gif
return;
InBlock.gif
InBlock.gif
//TODO:在此处为本机数据添加绘制代码
InBlock.gif
CBrushBackBrush;
InBlock.gifBackBrush.CreateSolidBrush(RGB(
255,255,255));
InBlock.gifCBrush
*pOldBrush=pDC->SelectObject(&BackBrush);
InBlock.gifCRectrect;
InBlock.gif
this->GetClientRect(&rect);
InBlock.gifpDC
->Rectangle(rect);//CRect(-1,-1,3000,3000));
InBlock.gif
pDC->SelectObject(pOldBrush);
InBlock.gif
if(!m_imageFile.IsNull())
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{//图片不为空
InBlock.gif
m_imageFile.StretchBlt(pDC->m_hDC,CRect(&m_rectShow),SRCCOPY);//复制图片到显示设备
ExpandedSubBlockEnd.gif
}

ExpandedBlockEnd.gif}

None.gif

打开图片:

None.gif void CFigureView::OnFileOpen()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {//打开图片文件
InBlock.gif
//TODO:在此添加命令处理程序代码
InBlock.gif
CStringstrFilter;
InBlock.gifCStringstrImageFileName;
InBlock.gifCSimpleArray
<GUID>aguidFileTypes;
InBlock.gifHRESULThResult;
InBlock.gifhResult
=m_imageFile.GetExporterFilterString(strFilter,aguidFileTypes);
InBlock.gif
if(FAILED(hResult))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifMessageBox(
"装入文件类型过滤器操作失败","消息提示",MB_OK);
InBlock.gif
return;
ExpandedSubBlockEnd.gif}

InBlock.gifstrFilter
="AllFile(*.*)|*.*|"+strFilter;
InBlock.gifCFileDialogdlg(TRUE,NULL,NULL,OFN_HIDEREADONLY,strFilter);
InBlock.gifhResult
=(int)dlg.DoModal();
InBlock.gif
if(hResult!=IDOK)
InBlock.gif
return;
InBlock.gifstrImageFileName.Format(dlg.GetFileName());
InBlock.gifm_imageFile.Destroy();
InBlock.gifhResult
=m_imageFile.Load(strImageFileName);
InBlock.gif
if(FAILED(hResult))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifMessageBox(
"装入图像文件操作失败","消息提示",MB_OK);
InBlock.gif
return;
ExpandedSubBlockEnd.gif}

InBlock.gifm_rectShow
=CRect(0,0,m_imageFile.GetWidth(),m_imageFile.GetHeight());//显示区域
InBlock.gif
SetScrollSizes(MM_TEXT,CSize(m_rectShow.Width(),m_rectShow.Height()));
InBlock.gifCWnd
*pWnd=AfxGetMainWnd();
InBlock.gifpWnd
->SetWindowTextA("当前正在打开的文件名称为:"+strImageFileName);
InBlock.gifInvalidate();
//刷新
ExpandedBlockEnd.gif
}

None.gif

进行黑白化处理:

None.gif void CFigureView::MakeBlackWhiteImage(CImage & pImage, int iType)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {//黑白化
InBlock.gif
CWaitCursorWaitCursor;
InBlock.gif
intHeight=pImage.GetHeight();//高度
InBlock.gif
intWidth=pImage.GetWidth();//宽度
InBlock.gif
if(!pImage.IsIndexed())//Indicatesthatabitmap'scolorsaremappedtoanindexedpalette
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{//没有使用调色板
InBlock.gif
for(intx=0;x<Width;x++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
for(inty=0;y<Height;y++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifCOLORREFpixel
=pImage.GetPixel(x,y);//Retrievesthecolorofthepixelatthelocationspecifiedbyxandy.
InBlock.gif
byter,g,b,Result;
InBlock.gifr
=GetRValue(pixel);
InBlock.gifg
=GetGValue(pixel);
InBlock.gifb
=GetBValue(pixel);
InBlock.gif
switch(iType)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
case0:
InBlock.gifResult
=((r+g+b)/3);
InBlock.gif
break;
InBlock.gif
case1:
InBlock.gifResult
=max(max(r,g),b);
InBlock.gif
break;
InBlock.gif
case2:
InBlock.gifResult
=(2.7*r+0.2*g+0.1*b);
InBlock.gif
break;
ExpandedSubBlockEnd.gif}

InBlock.gifpImage.SetPixel(x,y,RGB(Result,Result,Result));
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{//使用调色板
InBlock.gif
intMaxColors=pImage.GetMaxColorTableEntries();//Retrievesthemaximumnumberofentriesinthecolortable
InBlock.gif
RGBQUAD*ColorTable=newRGBQUAD[MaxColors];
InBlock.gif
//Retrievesred,green,blue(RGB)colorvaluesfromarangeofentriesinthepaletteoftheDIBsection
InBlock.gif
pImage.GetColorTable(0,MaxColors,ColorTable);
InBlock.gif
for(inti=0;i<MaxColors;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
byter,g,b,Result;
InBlock.gifr
=ColorTable[i].rgbRed;
InBlock.gifg
=ColorTable[i].rgbGreen;
InBlock.gifb
=ColorTable[i].rgbBlue;
InBlock.gif
switch(iType)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
case0:
InBlock.gifResult
=((r+g+b)/3);
InBlock.gif
break;
InBlock.gif
case1:
InBlock.gifResult
=max(max(r,g),b);
InBlock.gif
break;
InBlock.gif
case2:
InBlock.gifResult
=(2.7*r+0.2*g+0.1*b);
InBlock.gif
break;
ExpandedSubBlockEnd.gif}

InBlock.gifColorTable[i].rgbRed
=Result;
InBlock.gifColorTable[i].rgbGreen
=Result;
InBlock.gifColorTable[i].rgbBlue
=Result;
ExpandedSubBlockEnd.gif}

InBlock.gifpImage.SetColorTable(
0,MaxColors,ColorTable);
InBlock.gifdeleteColorTable;
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif
void CFigureView::OnPsBw()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {//图片黑白化
InBlock.gif
//TODO:在此添加命令处理程序代码
InBlock.gif
MakeBlackWhiteImage(m_imageFile,0);
InBlock.gif
InBlock.gifCFigureDoc
*pDoc=GetDocument();
InBlock.gifASSERT_VALID(pDoc);
InBlock.gif
if(!pDoc)
InBlock.gif
return;
InBlock.gifpDoc
->SetModifiedFlag(TRUE);//设置修改标志
InBlock.gif
Invalidate();
ExpandedBlockEnd.gif}

None.gif

处理效果:

2008030803.jpg

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值