QImage test2012::ImageCV2Qimg(IplImage* img)
{
assert(img!=NULL);
int h = img->height;
int w = img->width;
int channel = img->nChannels;
QImage qimg = QImage(w, h, QImage::Format_RGB32);
for(int i=0;i<h;i++)
{
for(int j=0;j<w;j++)
{
int r,g,b;
if(channel == 3)
{
b=(int)CV_IMAGE_ELEM(img,uchar,i,j*3+0);
g=(int)CV_IMAGE_ELEM(img,uchar,i,j*3+1);
r=(int)CV_IMAGE_ELEM(img,uchar,i,j*3+2);
}
else if(channel == 1)
{
b=(int)CV_IMAGE_ELEM(img,uchar,i,j);
g=b;
r=b;
}
qimg.setPixel(j,i,qRgb(r,g,b));
}
}
return qimg;
}
Qimage的存储顺序是RGB,而IplImage的存储顺序是BGR,所以我们要按顺序恢复。