anjinkristou

Convect QImage to IplImage

May 11th, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. static IplImage* qImage2IplImage(const QImage& qImage)
  2.   {
  3.     int width = qImage.width();
  4.     int height = qImage.height();
  5.  
  6.     // Creates a iplImage with 3 channels
  7.     IplImage *img = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
  8.     char * imgBuffer = img->imageData;
  9.  
  10.     //Remove alpha channel
  11.     int jump = (qImage.hasAlphaChannel()) ? 4 : 3;
  12.  
  13.     for (int y=0;y<img->height;y++){
  14.       QByteArray a((const char*)qImage.scanLine(y), qImage.bytesPerLine());
  15.       for (int i=0; i<a.size(); i+=jump){
  16.           //Swap from RGB to BGR
  17.           imgBuffer[2] = a[i];
  18.           imgBuffer[1] = a[i+1];
  19.           imgBuffer[0] = a[i+2];
  20.           imgBuffer+=3;
  21.       }
  22.   }
  23.  
  24.   return img;
  25.   }
Advertisement
Add Comment
Please, Sign In to add comment