2009년 12월 20일 일요일

QT4 with OpenCV, How to convert IplImage to QImage

void Canvas::setImage(const IplImage *newImage)
{
    uchar *qImageBuffer = NULL;

    int width = newImage->width;
    int widthStep = newImage->widthStep;
    int height = newImage->height;
    if(width!=old_w||height!=old_h||widthStep!=old_ws) {
        imageq = QImage();
    }
    qImageBuffer = (uchar *) malloc(width*height*4*sizeof(uchar));
    uchar *QImagePtr = qImageBuffer;
    int  linestep;
    const uchar *iplImagePtr = (const uchar *) newImage->imageData;
    if (newImage->origin==0) {
        linestep = widthStep-3*width;
    } else { // If image got origin from bottom-left
        linestep = (widthStep+(3*width))*-1;
        iplImagePtr += widthStep * (height-1);
    }
    for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                // We cannot help but copy manually.
                QImagePtr[0] = iplImagePtr[0]; // B
                QImagePtr[1] = iplImagePtr[1]; // G
                QImagePtr[2] = iplImagePtr[2]; // R
                QImagePtr[3] = 0xff; // Alpha
                QImagePtr += 4;
                iplImagePtr += 3;
            }
            iplImagePtr += linestep;
        }
    QImage *tempimg;
    tempimg = new QImage(qImageBuffer, width, height,QImage::Format_RGB32);
    imageq = tempimg->copy(0,0,width,height);
    free(qImageBuffer);
}

댓글 없음:

댓글 쓰기