Qt中QImage类封装了对于一般图像像素级的操作,图像显示则使用QPixmap。
获取图像的首地址:返回第一个像素数据的指针
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
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");
QPixmap转换为QImage
QPixmap pix(path);
QImage img = pix.toImage();
QImage转换为QPixmap
QImage img(path);
QPixmap pix = QPixmap::fromImage(img);
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();
}
}
QImage image;
image = QImage((const uchar*)SrcImage, ImgWidth, ImgHeight, QImage::Format_Indexed8).rgbSwapped();
ui->label_6->setPixmap(QPixmap::fromImage(image));
QImage image;
image = QImage((const uchar*)SrcImage, ImgWidth, ImgHeight, QImage::Format_RGB888).rgbSwapped();
ui->label_6->setPixmap(QPixmap::fromImage(image));
QImage不支持对16位的图像进行显示,必须转换为8位图像进行显示
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));