利用Opencv在PictureControl中显示照片

利用Opencv在PictureControl中显示IplImage格式的照片。

bool MyDlg::IfExistFile(CString strFilePath)
{
    CFile file;
    if (file.Open(strFilePath,CFile::modeRead))
    {
        file.Close();
        return true;
    }
    return false;
}

bool MyDlg::FillBlankToPicCtrl(const int& nID)
{
    CDC *pDC = GetDlgItem(nID)->GetDC();
    CRect rect;
    GetDlgItem(nID) ->GetClientRect( &rect );
    pDC->FillSolidRect(rect, GetSysColor(COLOR_ACTIVEBORDER));//填充当前背景色
    ReleaseDC(pDC);
    
    return true;
}

void MyDlg::ShowIplImage(IplImage* img, const int& nID)
{
    BITMAPINFO bifInfo;
    int nBpp = img ? (img->depth & 255) * img->nChannels : 0 ;
    FillBitmapInfo(&bifInfo, img->width, img->height, nBpp);
    ShowImageInWindow(img, GetDlgItem(nID), bifInfo);
}

void MyDlg::FillBitmapInfo( BITMAPINFO *bmi, int width, int height, int bpp)
{
    ASSERT( bmi && width > 0 && height > 0 &&
        (bpp == 8 || bpp == 24 || bpp == 32) ); BITMAPINFOHEADER* bmih = &(bmi->bmiHeader); memset( bmih, 0, sizeof(*bmih));
    bmih->biSize   = sizeof(BITMAPINFOHEADER);
    bmih->biWidth  = width;
    bmih->biHeight = -abs(height);
    bmih->biPlanes = 1;
    bmih->biBitCount = bpp;
    bmih->biCompression = BI_RGB;
    if( bpp == 8 )
    {
        RGBQUAD* palette = bmi->bmiColors;
        int i;
        for( i = 0; i < 256; i++ )
        {
            palette[i].rgbBlue = palette[i].rgbGreen = palette[i].rgbRed = (BYTE)i;
            palette[i].rgbReserved = 0;
        }
    }
}

void MyDlg::ShowImageInWindow(IplImage *pImg, CWnd *wnd, BITMAPINFO &bmi)
{
    CDC *pDC = wnd->GetDC();
    HDC  hDC = pDC->GetSafeHdc();
    CRect rect;
    wnd->GetClientRect(&rect);
    if(bmi.bmiHeader.biBitCount== 8)
    {
        CPalette pal;
        HPALETTE hpal=NULL;
        HPALETTE hOldPal=NULL;
        ::SetPaletteEntries(hpal,0,256,(LPPALETTEENTRY)bmi.bmiColors);
        hOldPal = ::SelectPalette(pDC->GetSafeHdc(), hpal, TRUE);
    }
    ::SetStretchBltMode(pDC->m_hDC, COLORONCOLOR);
    ::StretchDIBits(pDC->GetSafeHdc(),rect.left,rect.top,pImg->width,pImg->height,0,0,
        pImg->width,pImg->height,pImg->imageData,&bmi,DIB_RGB_COLORS,SRCCOPY);
    ReleaseDC(pDC);
}

 

posted @ 2015-02-09 09:09  秋月的私语  阅读(454)  评论(0编辑  收藏  举报