Note that there are some explanatory texts on larger screens.

plurals
  1. POError when converting Bitmap to Iplimage
    primarykey
    data
    text
    <p>I have a .net bitmap that must be converted to Open Cv image </p> <p>On the .net side I am doing the following :</p> <pre><code> b = new Bitmap(@"C:\Projects\samples\alfarok.jpg"); b = b.Clone(new Rectangle(0, 0, b.Width, b.Height), PixelFormat.Format24bppRgb); Rectangle rect = new Rectangle(0, 0, b.Width, b.Height); BitmapData bmpData = b.LockBits(rect, ImageLockMode.ReadWrite, b.PixelFormat); IntPtr ptr = bmpData.Scan0; int stride = bmpData.Stride; int numBytes = b.Width * b.Height * 3; byte[] rgbValues = new byte[numBytes]; for (int r = 0; r &lt; bmpData.Height; ++r) Marshal.Copy(new IntPtr((int)ptr + stride * r), rgbValues, bmpData.Width * 3 * r, bmpData.Width * 3); wrapper.DetectDocumentPatter(b.GetHbitmap(), rgbValues,b.Width,b.Height, numBytes); </code></pre> <p>In umanaged I am receiving an HBITMAP handle and byte array. I'm trying to build the DIB image first, but an exception is usually thrown: <code>std::bad_alloc at memory location 0x004ffb6c</code>.</p> <pre><code>BITMAP bmp; HDC hdc = GetDC(NULL); GetObject(b,sizeof(tagBITMAP),&amp;bmp); long dwcBihSize = sizeof(BITMAPINFOHEADER); long dwSize = dwcBihSize +(2&gt;&gt;bmp.bmBitsPixel) * sizeof(RGBQUAD) + ((bmp.bmBitsPixel * width) * height); LPBITMAPINFO pDIB =(LPBITMAPINFO) new BYTE[dwSize]; // check this ! pDIB-&gt;bmiHeader.biSize = dwcBihSize; pDIB-&gt;bmiHeader.biWidth = width; pDIB-&gt;bmiHeader.biHeight = height; pDIB-&gt;bmiHeader.biBitCount = bmp.bmBitsPixel; pDIB-&gt;bmiHeader.biPlanes = 1; pDIB-&gt;bmiHeader.biCompression = BI_RGB; pDIB-&gt;bmiHeader.biXPelsPerMeter = 1000 ; pDIB-&gt;bmiHeader.biYPelsPerMeter = 1000; pDIB-&gt;bmiHeader.biClrUsed = 0; pDIB-&gt;bmiHeader.biClrImportant = 0; LPRGBQUAD lpColors =(LPRGBQUAD)(pDIB+pDIB-&gt;bmiHeader.biSize); SetDIBColorTable(hdc,0,2&gt;&gt;bmp.bmBitsPixel * sizeof(RGBQUAD),lpColors); int dibCols = 2&gt;&gt;pDIB-&gt;bmiHeader.biBitCount; void* bitArray = pDIB + pDIB-&gt;bmiHeader.biSize+dibCols *sizeof(RGBQUAD); for(int i=0;i&lt;dibCols;i++) { lpColors[i].rgbRed=0; lpColors[i].rgbBlue=0; lpColors[i].rgbGreen=0; lpColors[i].rgbReserved=0; } SetDIBColorTable(hdc,0,dibCols,lpColors); int depth =bmp.bmBitsPixel == 1 ? IPL_DEPTH_1U : IPL_DEPTH_8U; int nChannels = 3; //IplImage* image= cvCreateImage(cvSize(width,height),depth,3); GetDIBits(hdc,b,0,height,bitArray,pDIB,DIB_RGB_COLORS); //image-&gt;imageData = (char*)bitArray; //memcpy(image-&gt;imageData,(char*)(bmp.bmBits),bmp.bmHeight*bmp.bmWidth*3); //cvSaveImage("c:\img.jpg",image); </code></pre> <p>Am I doing things right, or is there a more suitable approach? Why might the error <code>std::bad_alloc at memory location 0x004ffb6c</code> be thrown?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload