图像处理---黑白化

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

核心对象:

None.gif    CImage m_imageFile; 

绘制图片:

None.gifvoid CFigureView::OnDraw(CDC* pDC)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    CFigureDoc
* pDoc = GetDocument();
InBlock.gif    ASSERT_VALID(pDoc);
InBlock.gif    
if (!pDoc)
InBlock.gif        
return;
InBlock.gif
InBlock.gif    
// TODO: 在此处为本机数据添加绘制代码
InBlock.gif
    CBrush BackBrush;
InBlock.gif    BackBrush.CreateSolidBrush(RGB(
255,255,255));
InBlock.gif    CBrush
* pOldBrush = pDC->SelectObject(&BackBrush);
InBlock.gif    CRect rect;
InBlock.gif    
this->GetClientRect(&rect);
InBlock.gif    pDC
->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.gifvoid CFigureView::OnFileOpen()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{//打开图片文件
InBlock.gif    
// TODO: 在此添加命令处理程序代码
InBlock.gif
    CString strFilter;
InBlock.gif    CString strImageFileName;
InBlock.gif    CSimpleArray
<GUID> aguidFileTypes;
InBlock.gif    HRESULT hResult;
InBlock.gif    hResult 
= m_imageFile.GetExporterFilterString(strFilter,aguidFileTypes);
InBlock.gif    
if(FAILED(hResult))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        MessageBox(
"装入文件类型过滤器操作失败","消息提示",MB_OK);
InBlock.gif        
return;
ExpandedSubBlockEnd.gif    }

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

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

None.gif

进行黑白化处理:

None.gifvoid CFigureView::MakeBlackWhiteImage(CImage& pImage, int iType)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{//黑白化
InBlock.gif
    CWaitCursor WaitCursor;
InBlock.gif    
int Height = pImage.GetHeight();//高度
InBlock.gif
    int Width = pImage.GetWidth();//宽度
InBlock.gif
    if(!pImage.IsIndexed())//Indicates that a bitmap's colors are mapped to an indexed palette
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{//没有使用调色板
InBlock.gif
        for(int x=0; x<Width; x++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for(int y=0; y<Height;y++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                COLORREF pixel
=pImage.GetPixel(x,y);//Retrieves the color of the pixel at the location specified by x and y.
InBlock.gif
                byte r,g,b,Result;
InBlock.gif                r 
= GetRValue(pixel);
InBlock.gif                g 
= GetGValue(pixel);
InBlock.gif                b 
= GetBValue(pixel);
InBlock.gif                
switch(iType)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                
case 0:
InBlock.gif                    Result 
= ((r+g+b)/3);
InBlock.gif                    
break;
InBlock.gif                
case 1:
InBlock.gif                    Result 
= max(max(r,g),b);
InBlock.gif                    
break;
InBlock.gif                
case 2:
InBlock.gif                    Result 
= (2.7*r+0.2*g+0.1*b);
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif                }

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

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{//使用调色板
InBlock.gif
        int MaxColors = pImage.GetMaxColorTableEntries();//Retrieves the maximum number of entries in the color table
InBlock.gif
        RGBQUAD* ColorTable = new RGBQUAD[MaxColors];
InBlock.gif        
//Retrieves red, green, blue (RGB) color values from a range of entries in the palette of the DIB section
InBlock.gif
        pImage.GetColorTable(0,MaxColors,ColorTable);
InBlock.gif        
for(int i=0;i<MaxColors;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
byte r,g,b,Result;
InBlock.gif            r 
= ColorTable[i].rgbRed;
InBlock.gif            g 
= ColorTable[i].rgbGreen;
InBlock.gif            b 
= ColorTable[i].rgbBlue;
InBlock.gif            
switch(iType)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif            
case 0:
InBlock.gif                Result 
= ((r+g+b)/3);
InBlock.gif                
break;
InBlock.gif            
case 1:
InBlock.gif                Result 
= max(max(r,g),b);
InBlock.gif                
break;
InBlock.gif            
case 2:
InBlock.gif                Result 
= (2.7*r+0.2*g+0.1*b);
InBlock.gif                
break;
ExpandedSubBlockEnd.gif            }

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

InBlock.gif        pImage.SetColorTable(
0,MaxColors,ColorTable);
InBlock.gif        delete ColorTable;
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.gif    CFigureDoc
* pDoc = GetDocument();
InBlock.gif    ASSERT_VALID(pDoc);
InBlock.gif    
if (!pDoc)
InBlock.gif        
return;
InBlock.gif    pDoc
->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、付费专栏及课程。

余额充值