Qt之QImage类

QT——QImage类

Qt中QImage类封装了对于一般图像像素级的操作,图像显示则使用QPixmap。

QImage获取图像的数据信息

获取图像的首地址:返回第一个像素数据的指针

const uchar *QImage::bits() const

获取图像的总字节数

int QImage::byteCount() const

获取图像每行字节数

int QImage::bytesPerLine() const  

还可以这样计算(width:图像宽度,img.depth是图图像深度):

int bytePerLine = (width * img.depth()  +  31) / 32 * 4;

位深:位深是指存储每个像素所用的位数.

QImage img;
int ImgDepth = img.depth()  #返回当前图像的位深.

QImage图像格式转换

由RGB格式转换为BGR格式

返回一个QImage

QImage::rgbSwapped() 

将图片中像素值中的红色和蓝色组件的值交换,将RGB图像转换为BGR图像

QImage image(fileName);
QImage bgr = image.rgbSwapped();
将彩色图转换为灰度图

返回一个QImage

QImage::convertToFormat()

灰度图的参数应该选择为QImage::Format_Grayscale8但是必须在Qt5.5以上版本才能支持

QImage image(fileName);
QImage gray = image.convertToFormat(QImage::Format_Grayscale8);
图像保存
bool QImage::save(const QString &fileName, const char *format = Q_NULLPTR, int quality = -1) const

format选择保存的格式,支持格式如下:

BMP(Windows Bitmap) 
GIF(Graphic Interchange Format (optional)) 
JPG(Joint Photographic Experts Group) 
JPEG(Joint Photographic Experts Group) 
PNG(Portable Network Graphics) 
PBM(Portable Bitmap) 
PGM(Portable Graymap) 
PPM(Portable Pixmap) 
XBM(X11 Bitmap) 
XPM(X11 Pixmap)

quality必须在0到100或-1范围内。
指定0来获得小的压缩文件,100用于大的未压缩文件,和-1(默认)使用默认设置。

QString imagePath = “image.bmp”;
QImage image;
image.save(imagePath,"BMP");

QImage图像显示在QLabel上

在这里插入图片描述

QPixmap与QImage之间的互相转换

QPixmap转换为QImage

QPixmap pix(path);
QImage img = pix.toImage();

QImage转换为QPixmap

QImage img(path);
QPixmap pix = QPixmap::fromImage(img);
Mat与QImage之间的互相转换

QImage转换为Mat

cv::Mat QImage2cvMat(QImage image)
{
    cv::Mat mat;
    qDebug() << image.format();
    switch(image.format())
    {
    case QImage::Format_ARGB32:
    case QImage::Format_RGB32:
    case QImage::Format_ARGB32_Premultiplied:
        mat = cv::Mat(image.height(), image.width(), CV_8UC4, (void*)image.constBits(), image.bytesPerLine());
        break;
    case QImage::Format_RGB888:
        mat = cv::Mat(image.height(), image.width(), CV_8UC3, (void*)image.constBits(), image.bytesPerLine());
        cv::cvtColor(mat, mat, CV_BGR2RGB);
        break;
    case QImage::Format_Indexed8:
        mat = cv::Mat(image.height(), image.width(), CV_8UC1, (void*)image.constBits(), image.bytesPerLine());
        break;
    }
    return mat;
}
 

Mat转换为QImage

QImage cvMat2QImage(const cv::Mat& mat)
{
    // 8-bits unsigned, NO. OF CHANNELS = 1
    if(mat.type() == CV_8UC1)
    {
        QImage image(mat.cols, mat.rows, QImage::Format_Indexed8);
        // Set the color table (used to translate colour indexes to qRgb values)
        image.setColorCount(256);
        for(int i = 0; i < 256; i++)
        {
            image.setColor(i, qRgb(i, i, i));
        }
        // Copy input Mat
        uchar *pSrc = mat.data;
        for(int row = 0; row < mat.rows; row ++)
        {
            uchar *pDest = image.scanLine(row);
            memcpy(pDest, pSrc, mat.cols);
            pSrc += mat.step;
        }
        return image;
    }
    // 8-bits unsigned, NO. OF CHANNELS = 3
    else if(mat.type() == CV_8UC3)
    {
        // Copy input Mat
        const uchar *pSrc = (const uchar*)mat.data;
        // Create QImage with same dimensions as input Mat
        QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
        return image.rgbSwapped();
    }
    else if(mat.type() == CV_8UC4)
    {
        qDebug() << "CV_8UC4";
        // Copy input Mat
        const uchar *pSrc = (const uchar*)mat.data;
        // Create QImage with same dimensions as input Mat
        QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32);
        return image.copy();
    }
    else
    {
        qDebug() << "ERROR: Mat could not be converted to QImage.";
        return QImage();
    }
}

8位的灰度图像进行显示QImage::Format_Indexed8
QImage image;
image = QImage((const uchar*)SrcImage, ImgWidth, ImgHeight, QImage::Format_Indexed8).rgbSwapped();
ui->label_6->setPixmap(QPixmap::fromImage(image));
三通道的彩色图进行显示QImage::Format_RGB888
QImage image;
image = QImage((const uchar*)SrcImage, ImgWidth, ImgHeight, QImage::Format_RGB888).rgbSwapped();
ui->label_6->setPixmap(QPixmap::fromImage(image));
16位的灰度图像进行显示

QImage不支持对16位的图像进行显示,必须转换为8位图像进行显示

图像适应QLabel进行显示
labelShow=new QLabel(this);
SrcImage = imread("D://lena.jpg");
image = QImage((const uchar*)SrcImage.data, SrcImage.cols, SrcImage.rows, QImage::Format_RGB888).rgbSwapped();
image = image.scaled(labelShow->size(),Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
labelShow->setScaledContents(true);
labelShow->setPixmap(QPixmap::fromImage(image));
出现图像歪斜的问题

对于上述方法,有些图像可以显示正常,但是有些图像出现歪斜的状态,应该是没有达到4字节对齐的状态

将mat转QImage的代码改成如下代码:

image = QImage((const uchar*)SrcImage.data, SrcImage.cols, SrcImage.rows, SrcImage.cols*SrcImage.channels(),QImage::Format_RGB888).rgbSwapped();
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值