• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
增强的一亩三分地
增强的一亩三分地
博客园          联系   管理     
Qt下的图像操作——QImage

Qt下的图像操作——QImage

Qt为开发者提供了一套图像基础操作的接口——QImage,它无法和OpenCV这种专业图像处理库相比,但仍然为图像操作的基础开发提供了强大的支持。

打开图片文件

QImage image("<path/to/image/file>");

转换图片深度

QImage默认按8位深度读取图片,如果想正常读取更高位深度的图片,必须首先转换图片为目标深度图片,以16位深度灰度图为例

// QImage::Format_Grayscale16这个枚举变量在Qt 5.13及之后才支持
image.convertTo(QImage::Format_Grayscale16);

将图片像素信息写为CSV文件

QFile file("qtval.csv");
if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
	qDebug() << ("打开文件失败");
}
QTextStream txtOutput(&file);
for (int j = 0; j < image.height(); ++j) {
	quint16 *dst = reinterpret_cast<quint16*>(image.bits() + j * image.bytesPerLine());
	for (int i = 0; i < image.width(); ++i) {
		txtOutput << QString("%1").arg(dst[i]) << ",";
	}
	txtOutput << endl;
}
file.close();

一维数据转为QImage

QImage byte2img(void* data, int width, int height){
    return QImage(static_cast<const uchar*>(data), width, height,static_cast<int>(sizeof(unsigned short) * static_cast<unsigned int>(width)), QImage::Format_Grayscale16);
}

QImage剪切

QVector<unsigned short> Utility::parseThermalExtraData(const QImage& image,unsigned int x1,unsigned int x2,
                                    unsigned int y1,unsigned int y2){
    QVector<unsigned short> extraData;
    for(unsigned int y = y1;y<=y2;y++){
        quint16 *dst = reinterpret_cast<quint16*>(const_cast<uchar*>(image.bits() + y * static_cast<unsigned int>(image.bytesPerLine())));
        for(unsigned int x = x1;x<=x2;x++){
            extraData.append(dst[x]);
        }
    }
    return extraData;
}
posted on 2023-02-03 18:36  -leanard-  阅读(564)  评论(0)    收藏  举报  来源
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3