图像处理---黑白化
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
CImagem_imageFile;
voidCFigureView::OnDraw(CDC*pDC)


{
CFigureDoc*pDoc=GetDocument();
ASSERT_VALID(pDoc);
if(!pDoc)
return;

//TODO:在此处为本机数据添加绘制代码
CBrushBackBrush;
BackBrush.CreateSolidBrush(RGB(255,255,255));
CBrush*pOldBrush=pDC->SelectObject(&BackBrush);
CRectrect;
this->GetClientRect(&rect);
pDC->Rectangle(rect);//CRect(-1,-1,3000,3000));
pDC->SelectObject(pOldBrush);
if(!m_imageFile.IsNull())


{//图片不为空
m_imageFile.StretchBlt(pDC->m_hDC,CRect(&m_rectShow),SRCCOPY);//复制图片到显示设备
}
}
voidCFigureView::OnFileOpen()


{//打开图片文件
//TODO:在此添加命令处理程序代码
CStringstrFilter;
CStringstrImageFileName;
CSimpleArray<GUID>aguidFileTypes;
HRESULThResult;
hResult=m_imageFile.GetExporterFilterString(strFilter,aguidFileTypes);
if(FAILED(hResult))


{
MessageBox("装入文件类型过滤器操作失败","消息提示",MB_OK);
return;
}
strFilter="AllFile(*.*)|*.*|"+strFilter;
CFileDialogdlg(TRUE,NULL,NULL,OFN_HIDEREADONLY,strFilter);
hResult=(int)dlg.DoModal();
if(hResult!=IDOK)
return;
strImageFileName.Format(dlg.GetFileName());
m_imageFile.Destroy();
hResult=m_imageFile.Load(strImageFileName);
if(FAILED(hResult))


{
MessageBox("装入图像文件操作失败","消息提示",MB_OK);
return;
}
m_rectShow=CRect(0,0,m_imageFile.GetWidth(),m_imageFile.GetHeight());//显示区域
SetScrollSizes(MM_TEXT,CSize(m_rectShow.Width(),m_rectShow.Height()));
CWnd*pWnd=AfxGetMainWnd();
pWnd->SetWindowTextA("当前正在打开的文件名称为:"+strImageFileName);
Invalidate();//刷新
}
voidCFigureView::MakeBlackWhiteImage(CImage&pImage,intiType)


{//黑白化
CWaitCursorWaitCursor;
intHeight=pImage.GetHeight();//高度
intWidth=pImage.GetWidth();//宽度
if(!pImage.IsIndexed())//Indicatesthatabitmap'scolorsaremappedtoanindexedpalette


{//没有使用调色板
for(intx=0;x<Width;x++)


{
for(inty=0;y<Height;y++)


{
COLORREFpixel=pImage.GetPixel(x,y);//Retrievesthecolorofthepixelatthelocationspecifiedbyxandy.
byter,g,b,Result;
r=GetRValue(pixel);
g=GetGValue(pixel);
b=GetBValue(pixel);
switch(iType)


{
case0:
Result=((r+g+b)/3);
break;
case1:
Result=max(max(r,g),b);
break;
case2:
Result=(2.7*r+0.2*g+0.1*b);
break;
}
pImage.SetPixel(x,y,RGB(Result,Result,Result));
}
}
}
else


{//使用调色板
intMaxColors=pImage.GetMaxColorTableEntries();//Retrievesthemaximumnumberofentriesinthecolortable
RGBQUAD*ColorTable=newRGBQUAD[MaxColors];
//Retrievesred,green,blue(RGB)colorvaluesfromarangeofentriesinthepaletteoftheDIBsection
pImage.GetColorTable(0,MaxColors,ColorTable);
for(inti=0;i<MaxColors;i++)


{
byter,g,b,Result;
r=ColorTable[i].rgbRed;
g=ColorTable[i].rgbGreen;
b=ColorTable[i].rgbBlue;
switch(iType)


{
case0:
Result=((r+g+b)/3);
break;
case1:
Result=max(max(r,g),b);
break;
case2:
Result=(2.7*r+0.2*g+0.1*b);
break;
}
ColorTable[i].rgbRed=Result;
ColorTable[i].rgbGreen=Result;
ColorTable[i].rgbBlue=Result;
}
pImage.SetColorTable(0,MaxColors,ColorTable);
deleteColorTable;
}
}

voidCFigureView::OnPsBw()


{//图片黑白化
//TODO:在此添加命令处理程序代码
MakeBlackWhiteImage(m_imageFile,0);

CFigureDoc*pDoc=GetDocument();
ASSERT_VALID(pDoc);
if(!pDoc)
return;
pDoc->SetModifiedFlag(TRUE);//设置修改标志
Invalidate();
}
核心对象:
绘制图片:
打开图片:
进行黑白化处理:
处理效果: