Socialabel

Friday, November 8, 2013

Konversi Qimage ke IplImage dan Iplimage ke Qimage

Cara konversi dari iplimage ke Qimage dan juga Cara konversi Qimage ke Iplimage pada openCV belum tersedia sehingga user harus mendefinisikan sendiri fungsi untuk melakukan konversi tersebut pada module yang digunakan, pada NamaModuleQt.h (ilustrasi) tambahkan baris berikut ini:
 IplImage* Qimage_Ke_IplImage(QImage *gambarQImage)  
 QImage* IplImage_ke_QImage(IplImage *gambarIPL)   

Selanjutnya pada NamaModuleQt.cpp tambahkan baris berikut ini:
 IplImage* NamaModuleQt::Qimage_Ke_IplImage(QImage *gambarQImage)  
 {  
   IplImage* gambarIPL;  
   //Ini Menggunakan Satu Channel  
   if(gambarQImage->isGrayscale())  
   {  
     gambarIPL = cvCreateImageHeader( cvSize(gambarQImage->width(), gambarQImage->height()), IPL_DEPTH_8U, 1);  
   }  
   else //Ini Menggunakan jika 3 Channel RGB  
   {  
     gambarIPL = cvCreateImageHeader( cvSize(gambarQImage->width(), gambarQImage->height()), IPL_DEPTH_8U, 3);  
   }  
   gambarIPL->imageData = (char*) gambarQImage->bits();  
   uchar* Databaru = (uchar*) malloc(sizeof(uchar) * gambarQImage->byteCount());  
   memcpy(Databaru, gambarQImage->bits(), gambarQImage->byteCount());  
   gambarIPL->imageData = (char*) Databaru;  
   return gambarIPL;  
 }  

 QImage* NamaModuleQt::IplImage_ke_QImage(IplImage *gambarIPL)  
 {  
   int h = gambarIPL->height; int w = gambarIPL->width;  
   int channels = gambarIPL->nChannels; char *data = gambarIPL->imageData;  
   QImage *gambarQImage = new QImage(w, h, QImage::Format_ARGB32);  

   for (int y = 0; y < h; y++, data += gambarIPL->widthStep)  
   {  
     for (int x = 0; x < w; x++)  
     {  
       char r, g, b, a = 0;  
       if (channels == 1)  
       {  r = data[x * channels]; g = data[x * channels]; b = data[x * channels]; }
       else if (channels == 3 || channels == 4)  
       {  r = data[x * channels + 2]; g = data[x * channels + 1]; b = data[x * channels]; }  
       if (channels == 4)  
       {  a = data[x * channels + 3]; gambarQImage->setPixel(x, y, qRgba(r, g, b, a)); }  
       else  
       {  gambarQImage->setPixel(x, y, qRgb(r, g, b)); }  
     }  
   }  
   return gambarQImage;  
 }  

Cara penggunaan, silahkan panggil fungsi tersebut:
 IplImage* ha=Qimage_Ke_IplImage(&polarImg); //polarImge = Qimage 
 cvShowImage("Heatmap",ha);  

Source: Klik

Keyword: konversi, Convert, IPlImage, Qimage, OpenCV, iplmage

No comments:

Post a Comment