Afficher IplImage dans un contrôle Windows
Désolé, le sujet à déjà été posé plusieur fois, mais je ne trouve pas de solution qui me convienne.
Je travail avec du C++ managé.
J'aimerais afficher une IplImage obtenue avec openCV dans un control Window.
Pour le moment j'utilise une PictureBox, mais suis pret a utilisé autre chose.
Sur les différente réponse des autres sujets, je bloque toujours a un moment donnée.
Solution 1: Utiliser cvShowImage
La il faut que je récupere le HWND de mon control, que je récupere le nom de la fenetre et cvShowImage devrait marcher.
Mon probleme a mon avis est de récupérer le HWND de mon control.
J'ai testé
Code:
1 2 3 4 5 6 7 8 9 10 11
|
IntPtr handle = pictureBoxG->Handle;
void* phandle = handle .ToPointer();
HWND hwd = static_cast<HWND>(phandle );
char *winName = (char *) cvGetWindowName(hwd );
cvShowImage(winName, imgAp3G);
où:
pictureBoxG est mon control : System::Windows::Forms::PictureBox
imgAp3G est mon IplImage |
Là le nom de ma fenetre est toujours une chiane de caractere vide.
Solution 2: créer un HBITMAP à partir de mon IplImage puis utliser la méthode FromHBitmap
Et la le souci c'est de créer le HBitmap.
J'ai essayé plusieurs solution mais rien n'y fait.
Et en plus je suis sur que quelqu'un a le bout de code qui va bien, lol.
J'ai testé
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
HBITMAP bmp = Convertion_BUFFER_HBITMAP ( imgAp3G->imageData, imgAp3G->width, imgAp3G->height);
pictureBoxG->Image->FromHbitmap(bmp);
HBITMAP Convertion_BUFFER_HBITMAP ( char * Buffer, long lx, long ly )
{
HBITMAP hBmp, hBmpOld;
HDC hDC, hDC_Bmp;
BITMAPINFO BitmapInfo;
if (Buffer==NULL) return NULL;
//Initialisation des données DI Bits
memset ( &BitmapInfo, 0, sizeof(BITMAPINFO) );
BitmapInfo.bmiHeader.biBitCount = 32;
BitmapInfo.bmiHeader.biClrImportant = 0;
BitmapInfo.bmiHeader.biClrUsed = 0;
BitmapInfo.bmiHeader.biCompression = BI_RGB;
BitmapInfo.bmiHeader.biHeight = ly;
BitmapInfo.bmiHeader.biWidth = lx;
BitmapInfo.bmiHeader.biPlanes = 1;
BitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
BitmapInfo.bmiHeader.biSizeImage = 0;
BitmapInfo.bmiHeader.biXPelsPerMeter = 0;
BitmapInfo.bmiHeader.biYPelsPerMeter = 0;
BitmapInfo.bmiColors[0].rgbRed = 0xFF;
BitmapInfo.bmiColors[0].rgbGreen = 0xFF;
BitmapInfo.bmiColors[0].rgbBlue = 0xFF;
BitmapInfo.bmiColors[0].rgbReserved = 0;
hDC = GetDC ( NULL );
hDC_Bmp = CreateCompatibleDC ( hDC );
hBmp = CreateCompatibleBitmap ( hDC, lx, abs(ly) );
hBmpOld = (HBITMAP)SelectObject ( hDC_Bmp, hBmp );
ReleaseDC ( NULL, hDC );
SetDIBits ( hDC_Bmp, hBmp, 0, abs(ly), Buffer, &BitmapInfo, DIB_RGB_COLORS );
//Libération des ressources
SelectObject ( hDC_Bmp, hBmpOld );
DeleteDC ( hDC_Bmp );
// free ( Buffer );
return hBmp;
} |
mais rien y fait.
J'ai aussi testé ca, mais là non plus pas grand chose
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
HBITMAP bmp = CreateBitmap(imgAp3G->width,imgAp3G->height,24,imgAp3G->depth);
SetBitmapBits(bmp, imgAp3G->imageSize, imgAp3G->imageData);
pictureBoxG->BackgroundImage->FromHbitmap(bmp);
HBITMAP 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;
} |
En fait je n'ai jamais rien eu d'afficher de mon control, mais pas de gresillement.
J'ai toujours l'impression que mon Bitmap est invalide du coup le pictureBox n'affiche rien.
Je me suis suis aussi inspiré de ce que faisait cvShowImage, toujours rien.
Je dois etre fatigué en ce moment car je n'arrive à rien du tout.
Please Help