【QT】处理图像数据

1.图像数据读取/保存
QImage类,可以通过文件或者数据流获取图像数据。

通过文件读取:

QImage::QImage(const QString &fileName, const char *format = Q_NULLPTR)

支持以下格式:

FormatDescriptionQt’s support
BMPWindows BitmapRead/write
GIFGraphic Interchange Format (optional)Read
JPGJoint Photographic Experts GroupRead/write
JPEGJoint Photographic Experts GroupRead/write
PNGPortable Network GraphicsRead/write
PBMPortable BitmapRead
PGMPortable GraymapRead
PPMPortable PixmapRead/write
XBMX11 BitmapRead/write
XPMX11 PixmapRead/write

通过数据流读取:

QImage::QImage(uchar *data, int width, int height, Format format, QImageCleanupFunction cleanupFunction = Q_NULLPTR, void *cleanupInfo = Q_NULLPTR) //连续内存的读写版本

QImage::QImage(const uchar *data, int width, int height, Format format, QImageCleanupFunction cleanupFunction = Q_NULLPTR, void *cleanupInfo = Q_NULLPTR) //连续内存的只读版本

QImage::QImage(uchar *data, int width, int height, int bytesPerLine, Format format, QImageCleanupFunction cleanupFunction = Q_NULLPTR, void *cleanupInfo = Q_NULLPTR) //非连续内存的读写版本

QImage::QImage(const uchar *data, int width, int height, int bytesPerLine, Format format, QImageCleanupFunction cleanupFunction = Q_NULLPTR, void *cleanupInfo = Q_NULLPTR) //非连续内存的只读版本

这里的数据流指的是图像数据以二进制流的形式存放在内存中。
注意,QImage只支持8位深的单通道灰度图像,此时需要设置colortabel或者将单通道转3通到,对于彩色图像则可以直接创建。
QImage内部数据按照行4字节对齐,即图像一行的字节数必须整除4,如果图像实际宽度w能整除4,则可以采用连续内存的构造函数创建QImage,如果图像实际宽度不能整除4,则必须采用非连续内存的构造函数创建QImage,此时需要指定每行的实际字节数
示例:对于w* h=1000 * 1000的图像,由于1000/4=250,可以采用连续内存的构造函数

//imgData的格式是RGB888
QImage qimage(imgData,1000,1000,QImage::Format_RGB888);

对于w* h=1050 * 1000的图像,由于1050/4=262.5,必须采用非连续内存的构造函数

//imgData的格式是RGB888
QImage qimage(imgData,1050,1000,1050*3,QImage::Format_RGB888);

在创建QImage时,构造函数自动在每一行后面补零,由于w=1050,每行字节数=3150,需要补2个字节,即3152,实际qimage占用内存为3152*1000,比原来多了2000个字节。

需要注意,以下接口并非针对图像数据的数据流,而是图像文件的数据流:

bool QImage::loadFromData(const uchar *data, int len, const char *format = Q_NULLPTR)

举个例子,如.bmp的数据流,不仅包括图像数据本身,还包括bmp文件头

注意,QImage的拷贝构造函数是浅拷贝:

QImage::QImage(const QImage &image)

另外,opencv Mat与QImage的相互转换

2.图像的roi区域

QImage QImage::copy(const QRect &rectangle = QRect()) const

3.图像放缩

QImage QImage::scaled ( const QSize & size,Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio, Qt::TransformationModetransformMode = Qt::FastTransformation ) const

其中,enum Qt::AspectRatioMode包括:
Qt::IgnoreAspectRatio : The size is scaled freely. The aspect ratio is not preserved.
Qt::KeepAspectRatio : The size is scaled to a rectangle as large as possible inside a given rectangle, preserving the aspect ratio.(维持长宽比)
Qt::KeepAspectRatioByExpanding : The size is scaled to a rectangle as small as possible outside a given rectangle, preserving the aspect ratio.
enum Qt::TransformationMode包括:
Qt::FastTransformation:The transformation is performed quickly, with no smoothing.(最近邻)
Qt::SmoothTransformation:The resulting image is transformed using bilinear filtering.(双线性插值)
示例:

QImage* imgScaled = new QImage;
*imgScaled=img->scaled(width,
height,
Qt::KeepAspectRatio);
ui->label->setPixmap(QPixmap::fromImage(*imgScaled));

3.图像旋转
首先,获得几何变换矩阵:

QMatrix &QMatrix::rotate(qreal degrees)

然后,几何变换函数:

QImage QImage::transformed(const QMatrix &matrix, Qt::TransformationMode mode = Qt::FastTransformation) const

示例:

QImage* imgRatate = new QImage;
QMatrix matrix;
matrix.rotate(270);
*imgRotate = img->transformed(matrix);
ui->label->setPixmap(QPixmap::fromImage(*imgRotate));

  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值