图像处理---黑白化
核心对象:
    CImage m_imageFile; 绘制图片:
void CFigureView::OnDraw(CDC* pDC)
{
    CFigureDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
        return;
    // TODO: 在此处为本机数据添加绘制代码
    CBrush BackBrush;
    BackBrush.CreateSolidBrush(RGB(255,255,255));
    CBrush* pOldBrush = pDC->SelectObject(&BackBrush);
    CRect rect;
    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);//复制图片到显示设备
    }
}
打开图片:
void CFigureView::OnFileOpen()
{//打开图片文件
    // TODO: 在此添加命令处理程序代码
    CString strFilter;
    CString strImageFileName;
    CSimpleArray<GUID> aguidFileTypes;
    HRESULT hResult;
    hResult = m_imageFile.GetExporterFilterString(strFilter,aguidFileTypes);
    if(FAILED(hResult))
    {
        MessageBox("装入文件类型过滤器操作失败","消息提示",MB_OK);
        return;
    }
    strFilter = "All File(*.*)|*.*|"+strFilter;
    CFileDialog dlg(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();//刷新
}
进行黑白化处理:
void CFigureView::MakeBlackWhiteImage(CImage& pImage, int iType)
{//黑白化
    CWaitCursor WaitCursor;
    int Height = pImage.GetHeight();//高度
    int Width = pImage.GetWidth();//宽度
    if(!pImage.IsIndexed())//Indicates that a bitmap's colors are mapped to an indexed palette
    {//没有使用调色板
        for(int x=0; x<Width; x++)
        {
            for(int y=0; y<Height;y++)
            {
                COLORREF pixel=pImage.GetPixel(x,y);//Retrieves the color of the pixel at the location specified by x and y.
                byte r,g,b,Result;
                r = GetRValue(pixel);
                g = GetGValue(pixel);
                b = GetBValue(pixel);
                switch(iType)
                {
                case 0:
                    Result = ((r+g+b)/3);
                    break;
                case 1:
                    Result = max(max(r,g),b);
                    break;
                case 2:
                    Result = (2.7*r+0.2*g+0.1*b);
                    break;
                }
                pImage.SetPixel(x,y,RGB(Result,Result,Result));
            }
        }
    }
    else
    {//使用调色板
        int MaxColors = pImage.GetMaxColorTableEntries();//Retrieves the maximum number of entries in the color table
        RGBQUAD* ColorTable = new RGBQUAD[MaxColors];
        //Retrieves red, green, blue (RGB) color values from a range of entries in the palette of the DIB section
        pImage.GetColorTable(0,MaxColors,ColorTable);
        for(int i=0;i<MaxColors;i++)
        {
            byte r,g,b,Result;
            r = ColorTable[i].rgbRed;
            g = ColorTable[i].rgbGreen;
            b = ColorTable[i].rgbBlue;
            switch(iType)
            {
            case 0:
                Result = ((r+g+b)/3);
                break;
            case 1:
                Result = max(max(r,g),b);
                break;
            case 2:
                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);
        delete ColorTable;
    }
}
void CFigureView::OnPsBw()
{//图片黑白化
    // TODO: 在此添加命令处理程序代码
    MakeBlackWhiteImage(m_imageFile,0);
    CFigureDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
        return;
    pDoc->SetModifiedFlag(TRUE);//设置修改标志
    Invalidate();
}
处理效果:

作者:洞庭散人
出处:http://phinecos.cnblogs.com/
本博客遵从Creative Commons Attribution 3.0 License,若用于非商业目的,您可以自由转载,但请保留原作者信息和文章链接URL。
posted on 2008-03-08 22:52 Phinecos(洞庭散人) 阅读(1647) 评论(1) 收藏 举报
                    
                

    
                
            
        
浙公网安备 33010602011771号