Qt中用OpenCV2.4读取并把图像显示在QLabel中 (Ubuntu12.04)

近来用Qt及OpenCV处理图像,要在OpenCV中读取并显示图像在QLabel中,代码如下:


void CenterWindow::open()
{
    QString fileName = QFileDialog::getOpenFileName(this,tr("Open Image"),".",
                                                    tr("Image Files(*.png *.jpg *.jpeg *.bmp *.txt)"));
    if(fileName.isEmpty()){return;}

    QImage* img = new QImage();

    if(!(img->load(fileName)))
    {
        QString errorMessage = "The file: \""+ fileName + "\" is not a image file!";
        QMessageBox::information(NULL,
                                 "FileError",
                                 errorMessage
                                 );

        delete img;
        return;
    }
    image = cv::imread(fileName.toAscii().data());
    resultImage = cv::imread(fileName.toAscii().data());
    originImageLabel->setPixmap(QPixmap::fromImage(*img));
    originImageLabel->resize(originImageLabel->pixmap()->size());
}

变量定义如下,在.h文件中:


private:
    Mat image;   //the image variable
    Mat resultImage;
    QLabel      *originImageLabel;

结果如下(当然你得先安装好OpenCV2.4.1及Qt)


Qt中用OpenCV2.4读取并把图像显示在QLabel中 (Ubuntu12.04)_第1张图片


你可能感兴趣的:(image,ubuntu,File,null,delete,qt)