Re: Convert IplImage to Device Independent Bitmap (aka DIB)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Re: Convert IplImage to Device Independent Bitmap (aka DIB)

johnarg15
I am new to OpenCV and want to use your code for IplImage to DIB
Conversion.

Could you please explain the following:
1. Variable type for m_bmp
2. Sample example usage for IplImage to DIB conversion
3. Is IDC_SHOW for picture box in MFC ?

Thanking in advance.


--- In [hidden email], "Lim Yeen Fei" <cloud_roti_canai@...>
wrote:
>
> this is the code i used in my project, you can adapt it for your
> application, keep in mind there are several features/variables u
might

> wan to remove (ie the ROI rect drawing)
>
>
> HBITMAP CXMLDlg::CreateBitmap(int w,int h,WORD bpp,int Size)
> {
> HDC         hDC = ::CreateCompatibleDC(0);
>     BYTE        tmp[sizeof(BITMAPINFO)+255*4];
>     BITMAPINFO *bmi = (BITMAPINFO*)tmp;
>     HBITMAP     hBmp;
>      int i;
>     memset(bmi,0,sizeof(BITMAPINFO));
>     bmi->bmiHeader.biSize     = sizeof(BITMAPINFOHEADER);
>     bmi->bmiHeader.biWidth    = w;
>     bmi->bmiHeader.biHeight   = h;
>     bmi->bmiHeader.biPlanes   = Size;
>     bmi->bmiHeader.biBitCount = bpp;
>     bmi->bmiHeader.biCompression = BI_RGB;
>     bmi->bmiHeader.biSizeImage  = w*h*1;
>     bmi->bmiHeader.biClrImportant  =0 ;
>     switch(bpp)
>     {
>         case 8 :
>            for(i=0 ; i < 256 ; i++)
>            {
>                 bmi->bmiColors[i].rgbBlue = i;
>                 bmi->bmiColors[i].rgbGreen= i;
>                 bmi->bmiColors[i].rgbRed= i;
>            }
>            break;
>       case 32:
>       case 24:
>        ((DWORD*) bmi->bmiColors)[0] = 0x00FF0000; /* red mask  */
>        ((DWORD*) bmi->bmiColors)[1] = 0x0000FF00; /* green mask */
>        ((DWORD*) bmi->bmiColors)[2] = 0x000000FF; /* blue mask  */
>        break;
> }
>     hBmp = ::CreateDIBSection(hDC,bmi,DIB_RGB_COLORS,NULL,0,0);
>
>     ::DeleteDC(hDC);
>     return hBmp;
> }
>
> int CXMLDlg::ChangePreview(IplImage* img)
> {
> if(m_bmp) //if bitmap allocated before
> { //release previous bitmap
> ::DeleteObject(m_bmp);
> }
> m_bmp = CreateBitmap(img->width,img->height,img->nChannels *
> img->depth,1);
> SetBitmapBits(m_bmp,img->imageSize,img->imageData);
>
> int  h,w;
> RECT r;
> GetDlgItem(IDC_SHOW)->GetWindowRect(&r);
>     //ScreenToClient(&r);
> w = r.right  - r.left;
> h = r.bottom - r.top;
> if (w < h)
> m_fScaleRatio =(double) w / img->width;
> else if (w >= h)
> m_fScaleRatio =(double) h / img->height;
>
> return 1;
> }
>
> void CXMLDlg::OnPaint()
> {
> CPaintDC dc(this); // device context for local painting
>
> // TODO: Add your message handler code here
> if (!m_bmp)
>         return;
>     BITMAP bmp;
>     RECT r;
>     CDC  tmp;
>     int  w,h;
>     GetDlgItem(IDC_SHOW)->GetWindowRect(&r);
>     ScreenToClient(&r);
>     tmp.CreateCompatibleDC(0);
>     CBitmap *old = tmp.SelectObject(CBitmap::FromHandle(m_bmp));
>     ::GetObject(m_bmp,sizeof(BITMAP),&bmp);
>     w = r.right  - r.left;
>     h = r.bottom - r.top;
>     /* Make sure to use the stretching mode best for color
pictures */
>     dc.SetStretchBltMode(COLORONCOLOR);
>    
> //dc->StretchBlt
(r.left,r.top,w,h,&tmp,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);

> dc.StretchBlt(9,9,(int)(bmp.bmWidth * m_fScaleRatio)-1,
> (int)(bmp.bmHeight *
> m_fScaleRatio)-1,&tmp,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);
>     tmp.SelectObject(old);
>
> CGdiClass::DrawRect(&dc,m_rcROI,RGB(0,255,0));
> // Do not call CDialogBar::OnPaint() for painting messages
> }
>
> --- In [hidden email], "francesco\.bruno\@...\.it"
> <francesco.bruno@> wrote:
> >
> > Hi all,
> >
> > do you have any source code to convert an IplImage in a DIB?
> >
> > I saw an API like iplConvertToDIB but I didn't understand wich
> header file to include.
> >
> > Thanks in advance
> > Regards
> > - FB
> >
>