0

MFC GUI を使用している opencv を使用するカメラ アプリケーションがあります。画像処理用の Opencv モジュール用に CImage を IplImage に変換し、ウィンドウに再度表示するために CImage に戻す必要があります。

私はこのトピックで調査しましたが、十分な例と解決策がありません.誰でもいくつかの提案があります.Thank

これは私のコードです...

void CChildView::OnFileOpenimage(void)
{
    // TODO: Add your command handler code here
    CString strFilter;
    CSimpleArray<GUID> aguidFileTypes;
    HRESULT hResult;
hResult = imgOriginal.GetExporterFilterString(strFilter,aguidFileTypes);
if (FAILED(hResult)) {
    CString fmt;
    fmt.Format("GetExporterFilter failed:\n%x - %s", hResult, _com_error(hResult).ErrorMessage());
    ::AfxMessageBox(fmt);
    return;
}

CFileDialog dlg(TRUE, NULL, NULL, OFN_FILEMUSTEXIST, strFilter);
dlg.m_ofn.nFilterIndex = m_nFilterLoad;
hResult = (int)dlg.DoModal();
if(FAILED(hResult)) {
    return;
}

m_nFilterLoad = dlg.m_ofn.nFilterIndex;
imgOriginal.Destroy();
CString pathval =  dlg.GetPathName();
hResult = imgOriginal.Load(dlg.GetPathName());
if (FAILED(hResult)) {
    CString fmt;
    fmt.Format("Load image failed:\n%x - %s", hResult, _com_error(hResult).ErrorMessage());
    ::AfxMessageBox(fmt);
    return;
}
// IplImage *img from  imgOriginal; want to convert here for further processing.

m_nImageSize = SIZE_ORIGINAL;
Invalidate();
UpdateWindow();

}
4

3 に答える 3

1

私自身、かなり長い間この問題を抱えていました。ここでこのコードを見つけましたが、これは非常にうまく機能しました。「CImage to IplImage」を調べる代わりに、「HBITMAP to IplImage」を検索する必要があります。

IplImage* hBitmap2Ipl(HBITMAP hBmp, bool flip)
{
    BITMAP bmp;
    ::GetObject(hBmp,sizeof(BITMAP),&bmp);

    int    nChannels = bmp.bmBitsPixel == 1 ? 1 : bmp.bmBitsPixel/8;
    int    depth     = bmp.bmBitsPixel == 1 ? IPL_DEPTH_1U : IPL_DEPTH_8U;

    IplImage *img=cvCreateImageHeader(cvSize(bmp.bmWidth, bmp.bmHeight), depth, nChannels);

    img->imageData = (char*)malloc(bmp.bmHeight*bmp.bmWidth*nChannels*sizeof(char));
    memcpy(img->imageData,(char*)(bmp.bmBits),bmp.bmHeight*bmp.bmWidth*nChannels);
    return img;

}

使用法:

ATL::CImage image;
//whatever code you use to generate the image goes here
IplImage *convertedImage=hBitmap2Ipl(image.Detach());
image.Destroy();
于 2012-12-27T00:26:41.987 に答える