天天看點

圖像處理---黑白化

核心對象:

     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);

    strFilter = "All File(*.*)|*.*|"+strFilter;

    CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY,strFilter);

    hResult = (int)dlg.DoModal();

    if(hResult != IDOK) 

    strImageFileName.Format(dlg.GetFileName());

    m_imageFile.Destroy();

    hResult = m_imageFile.Load(strImageFileName);

        MessageBox("裝入圖像檔案操作失敗","消息提示",MB_OK);

    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);

                case 2:

                    Result = (2.7*r+0.2*g+0.1*b);

                }

                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);

            case 2:

                Result = (2.7*r+0.2*g+0.1*b);

            ColorTable[i].rgbRed = Result;

            ColorTable[i].rgbGreen = Result;

            ColorTable[i].rgbBlue = Result;

        pImage.SetColorTable(0,MaxColors,ColorTable);

        delete ColorTable;

void CFigureView::OnPsBw()

{//圖檔黑白化

    MakeBlackWhiteImage(m_imageFile,0);

    pDoc->SetModifiedFlag(TRUE);//設定修改标志

    Invalidate();

處理效果:

2008030803.jpg

本文轉自Phinecos(洞庭散人)部落格園部落格,原文連結:http://www.cnblogs.com/phinecos/archive/2008/03/08/1096896.html,如需轉載請自行聯系原作者

繼續閱讀